Skip to content

Commit

Permalink
feat(grpc): enable querying by protobuf struct tag
Browse files Browse the repository at this point in the history
  • Loading branch information
zoncoen committed Jan 25, 2024
1 parent 2c24a11 commit c15dbbc
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
27 changes: 27 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -2817,6 +2817,33 @@ THE SOFTWARE.

================================================================

github.com/zoncoen/query-go/extractor/protobuf
https://github.com/zoncoen/query-go/extractor/protobuf
----------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2018 Kenta Mori

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

================================================================

github.com/zoncoen/query-go/extractor/yaml
https://github.com/zoncoen/query-go/extractor/yaml
----------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/spf13/cobra v1.8.0
github.com/vmware-tanzu/carvel-ytt v0.45.4
github.com/zoncoen/query-go v1.3.1
github.com/zoncoen/query-go/extractor/protobuf v0.1.3
github.com/zoncoen/query-go/extractor/yaml v0.2.1
golang.org/x/mod v0.14.0
golang.org/x/sync v0.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/vmware-tanzu/carvel-ytt v0.45.4/go.mod h1:oHqFBnn/JvqaUjcQo9T/a/WPUP1
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/zoncoen/query-go v1.3.1 h1:7JZuqOKFNkLanCVvm3ufH5XMKuiCd+aks6txzhmy1ic=
github.com/zoncoen/query-go v1.3.1/go.mod h1:rbLi2EwarQtEJ5cNg9LFmAnleihBLsQSc1ow7vA8nms=
github.com/zoncoen/query-go/extractor/protobuf v0.1.3 h1:4ErHfkmAwAcDhrG43G92VpqQgA0uBS4d7zdHEClx3Ew=
github.com/zoncoen/query-go/extractor/protobuf v0.1.3/go.mod h1:NAzgdT3PpwhnzMPFtOSPC2ESsW/KguK23eYiWllMQGw=
github.com/zoncoen/query-go/extractor/yaml v0.2.1 h1:eLLx27en7aHF75RxArM446Y1JNGYasTcY3tCjVUOPJE=
github.com/zoncoen/query-go/extractor/yaml v0.2.1/go.mod h1:bhwuZIYGnkm+IOdy6pTgL1oTb+cjHvLTCM0Nc4EWjvE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
10 changes: 10 additions & 0 deletions protocol/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"

"github.com/goccy/go-yaml"
"github.com/zoncoen/query-go"
protobufextractor "github.com/zoncoen/query-go/extractor/protobuf"

"github.com/zoncoen/scenarigo/protocol"
)
Expand Down Expand Up @@ -63,3 +65,11 @@ func (p *GRPC) UnmarshalExpect(b []byte) (protocol.AssertionBuilder, error) {

return &e, nil
}

// QueryOptions implements the QueryOptionsProvider interface.
func (p *GRPC) QueryOptions() []query.Option {
return []query.Option{
query.CustomExtractFunc(protobufextractor.ExtractFunc()),
query.CustomIsInlineStructFieldFunc(protobufextractor.OneofIsInlineStructFieldFunc()),
}
}
47 changes: 47 additions & 0 deletions protocol/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/zoncoen/scenarigo/internal/queryutil"
)

func TestGRPC_UnmarshalRequest(t *testing.T) {
Expand Down Expand Up @@ -153,3 +154,49 @@ message: test`),
}
})
}

func TestGRPC_QueryOptions(t *testing.T) {
p := &GRPC{}
queryutil.AppendOptions(p.QueryOptions()...)
got, err := queryutil.New().Key("b").Key("bar_value").Extract(&OneofMessage{
Value: &OneofMessage_B_{
B: &OneofMessage_B{
BarValue: "yyy",
},
},
})
if err != nil {
t.Fatalf("failed to extract: %s", err)
}
if diff := cmp.Diff("yyy", got); diff != "" {
t.Errorf("request differs (-want +got):\n%s", diff)
}
}

type OneofMessage struct {
Value isOneofMessage_Value `protobuf_oneof:"value"`
}

type isOneofMessage_Value interface {
isOneofMessage_Value()
}

type OneofMessage_A_ struct {
A *OneofMessage_A `protobuf:"bytes,1,opt,name=a,proto3,oneof"`
}

type OneofMessage_B_ struct {
B *OneofMessage_B `protobuf:"bytes,2,opt,name=b,proto3,oneof"`
}

func (*OneofMessage_A_) isOneofMessage_Value() {}

func (*OneofMessage_B_) isOneofMessage_Value() {}

type OneofMessage_A struct {
FooValue string `protobuf:"bytes,1,opt,name=foo_value,json=fooValue,proto3" json:"foo_value,omitempty"`
}

type OneofMessage_B struct {
BarValue string `protobuf:"bytes,1,opt,name=bar_value,json=barValue,proto3" json:"bar_value,omitempty"`
}

0 comments on commit c15dbbc

Please sign in to comment.