Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
patch: include a json merge patch field for UPDATE operations (#22)
* proto: add patch field * patch: include a json merge patch field * patch: add basic tests * patch: cover some happy cases
- Loading branch information
Showing
8 changed files
with
253 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package pqstream | ||
|
||
import ( | ||
"bytes" | ||
|
||
jsonpatch "github.com/evanphx/json-patch" | ||
"github.com/golang/protobuf/jsonpb" | ||
ptypes_struct "github.com/golang/protobuf/ptypes/struct" | ||
) | ||
|
||
func generatePatch(a, b *ptypes_struct.Struct) (*ptypes_struct.Struct, error) { | ||
abytes := &bytes.Buffer{} | ||
bbytes := &bytes.Buffer{} | ||
m := &jsonpb.Marshaler{} | ||
|
||
if a != nil { | ||
if err := m.Marshal(abytes, a); err != nil { | ||
return nil, err | ||
} | ||
} | ||
if b != nil { | ||
if err := m.Marshal(bbytes, b); err != nil { | ||
return nil, err | ||
} | ||
} | ||
if abytes.Len() == 0 { | ||
abytes.Write([]byte("{}")) | ||
} | ||
if bbytes.Len() == 0 { | ||
bbytes.Write([]byte("{}")) | ||
} | ||
p, err := jsonpatch.CreateMergePatch(abytes.Bytes(), bbytes.Bytes()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
r := &ptypes_struct.Struct{} | ||
rbytes := bytes.NewReader(p) | ||
err = (&jsonpb.Unmarshaler{}).Unmarshal(rbytes, r) | ||
return r, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package pqstream | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang/protobuf/jsonpb" | ||
ptypes_struct "github.com/golang/protobuf/ptypes/struct" | ||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func Test_generatePatch(t *testing.T) { | ||
type args struct { | ||
a *ptypes_struct.Struct | ||
b *ptypes_struct.Struct | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantJson string | ||
wantErr bool | ||
}{ | ||
{"nils", args{nil, nil}, "{}", false}, | ||
{"empties", args{&ptypes_struct.Struct{}, &ptypes_struct.Struct{}}, "{}", false}, | ||
{"basic", args{&ptypes_struct.Struct{}, &ptypes_struct.Struct{ | ||
map[string]*ptypes_struct.Value{ | ||
"foo": &ptypes_struct.Value{ | ||
Kind: &ptypes_struct.Value_StringValue{ | ||
StringValue: "bar", | ||
}, | ||
}, | ||
}, | ||
}}, `{"foo":"bar"}`, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := generatePatch(tt.args.a, tt.args.b) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("generatePatch() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
gotJson, err := (&jsonpb.Marshaler{}).MarshalToString(got) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if !cmp.Equal(gotJson, tt.wantJson) { | ||
t.Errorf("generatePatch() = %v, want %v\n%s", gotJson, tt.wantJson, cmp.Diff(gotJson, tt.wantJson)) | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.