Skip to content

Commit

Permalink
fix(query): do not extract by the inline field name
Browse files Browse the repository at this point in the history
  • Loading branch information
zoncoen committed Aug 17, 2021
1 parent 912c596 commit 875d27b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
26 changes: 26 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,32 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

github.com/sergi/go-diff
https://github.com/sergi/go-diff
----------------------------------------------------------------
Copyright (c) 2012-2016 The go-diff Authors. All rights reserved.

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/spf13/cobra
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/lestrrat-go/backoff v1.0.1
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/pkg/errors v0.9.1
github.com/sergi/go-diff v1.2.0
github.com/spf13/cobra v0.0.7
github.com/spf13/pflag v1.0.5 // indirect
github.com/zoncoen/query-go v1.0.4
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
Expand Down Expand Up @@ -260,10 +262,12 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
6 changes: 5 additions & 1 deletion query/extractor/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (e *keyExtractor) extract(v reflect.Value) (reflect.Value, bool) {
}
default:
inlines := []int{}
FIELD_LOOP:
for i := 0; i < v.Type().NumField(); i++ {
field := v.Type().FieldByIndex([]int{i})
if !v.Field(i).CanInterface() {
Expand All @@ -64,9 +65,12 @@ func (e *keyExtractor) extract(v reflect.Value) (reflect.Value, bool) {
for _, opt := range strs[1:] {
if opt == "inline" {
inlines = append(inlines, i)
continue FIELD_LOOP // Do not extract by the inline field name.
}
}
name = strs[0]
if strs[0] != "" {
name = strs[0]
}
}
if field.Anonymous {
if len(inlines) == 0 || inlines[len(inlines)-1] != i {
Expand Down
8 changes: 6 additions & 2 deletions query/extractor/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
type testStruct struct {
A string
B string `yaml:"2"`
C string `yaml:"3,omitempty"`
Inline inlineStruct `yaml:",inline"`

AnonymousStruct

unexported struct{}
}

Expand Down Expand Up @@ -74,6 +73,11 @@ func TestKey_Extract(t *testing.T) {
v: testStruct{B: "BBB"},
expect: "BBB",
},
"struct field tag w/ option": {
key: "3",
v: testStruct{C: "C"},
expect: "C",
},
"inline struct": {
key: "c",
v: testStruct{Inline: inlineStruct{C: "CCC"}},
Expand Down
12 changes: 7 additions & 5 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
"testing"

"github.com/goccy/go-yaml"
"github.com/sergi/go-diff/diffmatchpatch"
"google.golang.org/grpc"

"github.com/zoncoen/scenarigo"
"github.com/zoncoen/scenarigo/context"
"github.com/zoncoen/scenarigo/internal/testutil"
"github.com/zoncoen/scenarigo/reporter"
"github.com/zoncoen/scenarigo/testdata/gen/pb/test"
"google.golang.org/grpc"
)

func TestE2E(t *testing.T) {
Expand Down Expand Up @@ -79,10 +81,10 @@ func TestE2E(t *testing.T) {
t.Fatal(err)
}

if expect, got := string(stdout), testutil.ResetDuration(b.String()); expect != got {
t.Logf("===== expect =====\n%s", expect)
t.Logf("===== got =====\n%s", got)
t.FailNow()
if got, expect := testutil.ResetDuration(b.String()), string(stdout); got != expect {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(expect, got, false)
t.Errorf("stdout differs:\n%s", dmp.DiffPrettyText(diffs))
}
})
}
Expand Down

0 comments on commit 875d27b

Please sign in to comment.