From 73d31a282c75afb79b21d03f5f046f8d7627ae13 Mon Sep 17 00:00:00 2001 From: Filip Gschwandtner Date: Tue, 3 Nov 2020 12:14:12 +0100 Subject: [PATCH] fix: fixed bad assumption from previous refactoring Signed-off-by: Filip Gschwandtner --- pkg/models/item.go | 27 ++++++++++++++++++++++++--- pkg/models/models.go | 2 +- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pkg/models/item.go b/pkg/models/item.go index 64ff697878..170d8caa2e 100644 --- a/pkg/models/item.go +++ b/pkg/models/item.go @@ -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, }) @@ -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) diff --git a/pkg/models/models.go b/pkg/models/models.go index c7647f1ca6..ee2ace18f4 100644 --- a/pkg/models/models.go +++ b/pkg/models/models.go @@ -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