Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from lyft/jsonpb-unmarshaler-options
Browse files Browse the repository at this point in the history
This resolves an issue we were seeing where backwards compatibility of new versions of protobuf messages with new fields were failing.
  • Loading branch information
wild-endeavor authored Sep 9, 2019
2 parents edc5ed4 + c62e844 commit 99d622c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion go/tasks/v1/utils/marshal_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package utils
import (
"encoding/json"
"fmt"
"strings"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
structpb "github.com/golang/protobuf/ptypes/struct"
)

var jsonPbMarshaler = jsonpb.Marshaler{}
var jsonPbUnmarshaler = &jsonpb.Unmarshaler{
AllowUnknownFields: true,
}

func UnmarshalStruct(structObj *structpb.Struct, msg proto.Message) error {
if structObj == nil {
Expand All @@ -21,7 +25,7 @@ func UnmarshalStruct(structObj *structpb.Struct, msg proto.Message) error {
return err
}

if err = jsonpb.UnmarshalString(jsonObj, msg); err != nil {
if err = jsonPbUnmarshaler.Unmarshal(strings.NewReader(jsonObj), msg); err != nil {
return err
}

Expand Down
28 changes: 28 additions & 0 deletions go/tasks/v1/utils/marshal_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

import (
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/plugins"
"github.com/stretchr/testify/assert"
"testing"
)

func TestBackwardsCompatibility(t *testing.T) {
sidecarProtoMessage := plugins.SidecarJob{
PrimaryContainerName: "primary",
}

sidecarStruct, err := MarshalObjToStruct(sidecarProtoMessage)
assert.NoError(t, err)

// Set a new field in the struct to mimic what happens when we add new fields to protobuf messages
sidecarStruct.Fields["hello"] = &structpb.Value{
Kind: &structpb.Value_StringValue{
StringValue: "world",
},
}

newSidecarJob := plugins.SidecarJob{}
err = UnmarshalStruct(sidecarStruct, &newSidecarJob)
assert.NoError(t, err)
}

0 comments on commit 99d622c

Please sign in to comment.