Skip to content

Commit

Permalink
fix: fixed bad assumption from previous refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Filip Gschwandtner <[email protected]>
  • Loading branch information
fgschwan committed Nov 3, 2020
1 parent d148d10 commit 73d31a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
27 changes: 24 additions & 3 deletions pkg/models/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,21 @@ func UnmarshalItemUsingModelRegistry(item *api.Item, modelRegistry Registry) (pr
if err != nil {
return nil, err
}
return unmarshalItemDataAny(item.GetData().GetAny(), modelRegistry.MessageTypeRegistry()) // msgTypeResolver *protoregistry.Types

// unmarshal item's inner data
// (we must distinguish between model registries due to different go types produced by using different
// model registry. The LocalRegistry use cases need go types as generated from models, but
// the RemoteRegistry can't produce such go typed instances (we know the name of go type, but can't
// produce it from remote information) so dynamic proto message must be enough (*dynamicpb.Message))
if _, ok := modelRegistry.(*LocalRegistry); ok {
return unmarshalItemDataAnyOfLocalModel(item.GetData().GetAny())
}
return unmarshalItemDataAnyOfRemoteModel(item.GetData().GetAny(), modelRegistry.MessageTypeRegistry())
}

// unmarshalItemDataAny unmarshalls the generic data part of api.Item
func unmarshalItemDataAny(itemAny *any.Any, msgTypeResolver *protoregistry.Types) (proto.Message, error) {
// unmarshalItemDataAnyOfRemoteModel unmarshalls the generic data part of api.Item that has remote model.
// The unmarshalled proto.Message will have dynamic type (*dynamicpb.Message).
func unmarshalItemDataAnyOfRemoteModel(itemAny *any.Any, msgTypeResolver *protoregistry.Types) (proto.Message, error) {
msg, err := anypb.UnmarshalNew(itemAny, protoV2.UnmarshalOptions{
Resolver: msgTypeResolver,
})
Expand All @@ -93,6 +103,17 @@ func unmarshalItemDataAny(itemAny *any.Any, msgTypeResolver *protoregistry.Types
return proto.MessageV1(msg), nil
}

// unmarshalItemDataAnyOfLocalModel unmarshalls the generic data part of api.Item that has local model.
// The unmarshalled proto.Message will have the go type of model generated go structures (that is due to
// go type registering in init() method of generated go structures file).
func unmarshalItemDataAnyOfLocalModel(itemAny *any.Any) (proto.Message, error) {
var any types.DynamicAny // local
if err := types.UnmarshalAny(itemAny, &any); err != nil {
return nil, err
}
return any.Message, nil
}

// GetModelForItem returns model for given item.
func GetModelForItem(item *api.Item) (KnownModel, error) {
return GetModelFromModelRegistryForItem(item, DefaultRegistry)
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func GetKeyUsingModelRegistry(message proto.Message, modelRegistry Registry) (st
model, err := GetModelFromRegistryFor(message, modelRegistry)
if err != nil {
return "", errors.Errorf("can't find known model "+
"for message due to: %v (message = %+v)", err, message)
"for message (while getting key for model) due to: %v (message = %+v)", err, message)
}

// compute Item.ID.Name
Expand Down

0 comments on commit 73d31a2

Please sign in to comment.