Skip to content

Commit

Permalink
Merge pull request #59 from zoncoen/yaml
Browse files Browse the repository at this point in the history
change(extractor/yaml): implement KeyExtractorContext interface
  • Loading branch information
zoncoen authored Jan 25, 2024
2 parents b19b282 + e26c5a4 commit e94950d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
3 changes: 2 additions & 1 deletion extractor/yaml/example_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func ExampleMapSliceExtractFunc() {
}

q := query.New(
query.CustomExtractFunc(yamlextractor.MapSliceExtractFunc(true)),
query.CaseInsensitive(),
query.CustomExtractFunc(yamlextractor.MapSliceExtractFunc()),
).Index(0).Key("FOO")
got, err := q.Extract(v)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion extractor/yaml/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/goccy/go-yaml v1.11.2
github.com/zoncoen/query-go v1.2.1
github.com/zoncoen/query-go v1.3.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions extractor/yaml/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/zoncoen/query-go v1.2.1 h1:enpkODFhqsfHKK8QlwQfdMmdl0wLGLi4Qro31MsZ4Lo=
github.com/zoncoen/query-go v1.2.1/go.mod h1:4ZoOJb0AeccYENjjd5HrkJEsj1WyE6oHxSw3roqCj3M=
github.com/zoncoen/query-go v1.3.0 h1:wL5e3jxP1l3mMueyrsnj7KOtAvSe1JNzW1FaLhD5rsQ=
github.com/zoncoen/query-go v1.3.0/go.mod h1:rbLi2EwarQtEJ5cNg9LFmAnleihBLsQSc1ow7vA8nms=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
14 changes: 7 additions & 7 deletions extractor/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Package yaml provides a function to extract values from yaml.MapSlice.
package yaml

import (
"context"
"reflect"
"strings"

Expand All @@ -15,7 +16,7 @@ import (
var mapSliceType = reflect.TypeOf(yaml.MapSlice{})

// MapSliceExtractFunc is a function for query.CustomExtractFunc option to extract values from yaml.MapSlice.
func MapSliceExtractFunc(caseInsensitive bool) func(query.ExtractFunc) query.ExtractFunc {
func MapSliceExtractFunc() func(query.ExtractFunc) query.ExtractFunc {
return func(f query.ExtractFunc) query.ExtractFunc {
return func(in reflect.Value) (reflect.Value, bool) {
v := in
Expand All @@ -36,7 +37,6 @@ func MapSliceExtractFunc(caseInsensitive bool) func(query.ExtractFunc) query.Ext
if ok {
return f(reflect.ValueOf(&keyExtractor{
v: s,
caseInsensitive: caseInsensitive,
}))
}
}
Expand All @@ -49,18 +49,18 @@ func MapSliceExtractFunc(caseInsensitive bool) func(query.ExtractFunc) query.Ext

type keyExtractor struct {
v yaml.MapSlice
caseInsensitive bool
}

// ExtractByKey implements the query.KeyExtractor interface.
func (e *keyExtractor) ExtractByKey(key string) (interface{}, bool) {
if e.caseInsensitive {
// ExtractByKey implements the query.KeyExtractorContext interface.
func (e *keyExtractor) ExtractByKey(ctx context.Context, key string) (interface{}, bool) {
ci := query.IsCaseInsensitive(ctx)
if ci {
key = strings.ToLower(key)
}
for _, i := range e.v {
k, ok := i.Key.(string)
if ok {
if e.caseInsensitive {
if ci {
k = strings.ToLower(k)
}
if key == k {
Expand Down
17 changes: 9 additions & 8 deletions extractor/yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestMapSliceExtractFunc(t *testing.T) {
}{
"yaml.MapSlice": {
query: query.New(
query.CustomExtractFunc(MapSliceExtractFunc(false)),
query.CustomExtractFunc(MapSliceExtractFunc()),
).Key("foo"),
v: yaml.MapSlice{
yaml.MapItem{
Expand All @@ -30,7 +30,8 @@ func TestMapSliceExtractFunc(t *testing.T) {
},
"yaml.MapSlice (case-insensitive)": {
query: query.New(
query.CustomExtractFunc(MapSliceExtractFunc(true)),
query.CaseInsensitive(),
query.CustomExtractFunc(MapSliceExtractFunc()),
).Key("foo"),
v: yaml.MapSlice{
yaml.MapItem{
Expand All @@ -42,7 +43,7 @@ func TestMapSliceExtractFunc(t *testing.T) {
},
"[]interface{yaml.MapSlice}": {
query: query.New(
query.CustomExtractFunc(MapSliceExtractFunc(false)),
query.CustomExtractFunc(MapSliceExtractFunc()),
).Index(0).Key("foo"),
v: []interface{}{
yaml.MapSlice{
Expand All @@ -56,7 +57,7 @@ func TestMapSliceExtractFunc(t *testing.T) {
},
"*yaml.MapSlice": {
query: query.New(
query.CustomExtractFunc(MapSliceExtractFunc(false)),
query.CustomExtractFunc(MapSliceExtractFunc()),
).Key("foo"),
v: &yaml.MapSlice{
yaml.MapItem{
Expand All @@ -68,15 +69,15 @@ func TestMapSliceExtractFunc(t *testing.T) {
},
"not yaml.MapSlice": {
query: query.New(
query.CustomExtractFunc(MapSliceExtractFunc(false)),
query.CustomExtractFunc(MapSliceExtractFunc()),
).Index(1),
v: []interface{}{1, 2},
expect: 2,
},
"not slice": {
query: query.New(
query.CaseInsensitive(),
query.CustomExtractFunc(MapSliceExtractFunc(false)),
query.CustomExtractFunc(MapSliceExtractFunc()),
).Key("foo"),
v: map[string]string{
"foo": "aaa",
Expand Down Expand Up @@ -105,7 +106,7 @@ func TestMapSliceExtractFunc(t *testing.T) {
}{
"yaml.MapSlice": {
query: query.New(
query.CustomExtractFunc(MapSliceExtractFunc(false)),
query.CustomExtractFunc(MapSliceExtractFunc()),
).Key("foo"),
v: yaml.MapSlice{
yaml.MapItem{
Expand All @@ -117,7 +118,7 @@ func TestMapSliceExtractFunc(t *testing.T) {
},
"not yaml.MapSlice": {
query: query.New(
query.CustomExtractFunc(MapSliceExtractFunc(false)),
query.CustomExtractFunc(MapSliceExtractFunc()),
).Key("bar"),
v: []int{},
expect: `".bar" not found`,
Expand Down

0 comments on commit e94950d

Please sign in to comment.