Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add typeurl package for TypeUrl handling for external types #1133

Merged
merged 5 commits into from
Jul 6, 2017

Conversation

crosbymichael
Copy link
Member

@crosbymichael crosbymichael commented Jul 5, 2017

This adds a typeurl package for the marshal/unmarshal of Any types with proper TypeUrl fields set for the data that they contain.

Closes #1091
Fixes #1052

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
@@ -0,0 +1,99 @@
package typeurl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be a move of events/convert.go.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

typeurl/types.go Outdated
"github.com/gogo/protobuf/types"
)

const Namespace = "types.containerd.io"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefix? Such that it reads a typeurl.Prefix.

typeurl/types.go Outdated
}

// TypeUrl returns the type url for a registred type
func TypeUrl(v interface{}) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeURL

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

types.Any.TypeUrl would disagree ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is autogenerated code.

typeurl/types.go Outdated
defer mu.Unlock()
u, ok := registry[tryDereference(v)]
if !ok {
return "", ErrNotExists
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fallback to checking protobuf registration.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See events/convert.go and proto.MessageName(pb). I think you can dispatch on proto.Message.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
@codecov-io
Copy link

codecov-io commented Jul 5, 2017

Codecov Report

Merging #1133 into master will decrease coverage by 2.56%.
The diff coverage is 61.05%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1133      +/-   ##
==========================================
- Coverage   34.26%   31.69%   -2.57%     
==========================================
  Files          28       27       -1     
  Lines        2335     2174     -161     
==========================================
- Hits          800      689     -111     
+ Misses       1364     1337      -27     
+ Partials      171      148      -23
Impacted Files Coverage Δ
process.go 0% <0%> (ø) ⬆️
task.go 0% <0%> (ø) ⬆️
spec_unix.go 61.9% <0%> (+1.15%) ⬆️
container_unix.go 0% <0%> (ø) ⬆️
client.go 2.67% <100%> (+1.89%) ⬆️
typeurl/types.go 79.1% <79.1%> (ø)
snapshot/storage/metastore.go
snapshot/storage/bolt.go

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8e1a04f...4b9a8ee. Read the comment docs.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>

var de events.DynamicEvent
if err := events.UnmarshalEvent(env.Event, &de); err != nil {
v, err := typeurl.UnmarshalAny(env.Event)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back and forth on dynamic vs allocated UnmarshalAny and in certain cases, the dynamic approach avoids a serialization if you use the Is function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

task.go Outdated

if e.ID == t.containerID && e.Pid == t.pid {
return e.ExitStatus, nil
v, err := typeurl.UnmarshalAny(evt.Event)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Is here prevent unmarshaling from happening.

typeurl/types.go Outdated
mu sync.Mutex
registry = make(map[reflect.Type]string)
ErrRegistered = errors.New("typeurl: type already registred")
ErrNotExists = errors.New("typeurl: type is not registered")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use errdefs.ErrAlreadyExists and errdefs.ErrNotFound.

@stevvooe
Copy link
Member

stevvooe commented Jul 5, 2017

LGTM after a few nits.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
if err != nil {
return nil, err
}
v := reflect.New(t.t).Interface()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inside of EmptyAny, they always do reflect.New(t.t.Elem()).Interface(), which indicates they always work with pointer types for registration. We can enforce a pointer type at registration to prevent the wrong type from being registered.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to panic when you try to get the type of something that is a non-pointer

@stevvooe
Copy link
Member

stevvooe commented Jul 6, 2017

LGTM

Copy link
Member

@estesp estesp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with minor test name typo--can be fixed in another PR if desired.

registry = make(map[reflect.Type]string)
}

func TestRegsiterPointerGetPointer(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very minor nit: TestRegisterPointerGetPointer (vs. RegsiterPointer..)

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
@crosbymichael
Copy link
Member Author

@estesp fixed the typo

@estesp
Copy link
Member

estesp commented Jul 6, 2017

LGTM

@estesp estesp merged commit e283b38 into containerd:master Jul 6, 2017
@crosbymichael crosbymichael deleted the register branch July 6, 2017 20:23
ianlewis pushed a commit to ianlewis/containerd that referenced this pull request Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants