Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add IsCaseInsensitive #55

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package query

import "context"

var caseInsensitiveKey = struct{}{}

func withCaseInsensitive(ctx context.Context, b bool) context.Context {
return context.WithValue(ctx, caseInsensitiveKey, b)
}

// IsCaseInsensitive reports whether case-insensitive querying is enabled or not.
func IsCaseInsensitive(ctx context.Context) bool {
if b, ok := ctx.Value(caseInsensitiveKey).(bool); ok {
return b
}
return false
}
14 changes: 14 additions & 0 deletions key.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package query

import (
"context"
"reflect"
"strings"
)
Expand All @@ -13,6 +14,14 @@ type KeyExtractor interface {
ExtractByKey(key string) (interface{}, bool)
}

// KeyExtractorContext is the interface that wraps the ExtractByKey method.
//
// ExtractByKey extracts the value by key.
// It reports whether the key is found and returns the found value.
type KeyExtractorContext interface {
ExtractByKey(ctx context.Context, key string) (any, bool)
}

// Key represents an extractor to access the value by key.
type Key struct {
key string
Expand All @@ -28,6 +37,11 @@ type Key struct {
// If v implements the KeyExtractor interface, this method extracts by calling v.ExtractByKey.
func (e *Key) Extract(v reflect.Value) (reflect.Value, bool) {
if v.IsValid() {
if i, ok := v.Interface().(KeyExtractorContext); ok {
ctx := withCaseInsensitive(context.Background(), e.caseInsensitive)
x, ok := i.ExtractByKey(ctx, e.key)
return reflect.ValueOf(x), ok
}
if i, ok := v.Interface().(KeyExtractor); ok {
x, ok := i.ExtractByKey(e.key)
return reflect.ValueOf(x), ok
Expand Down
41 changes: 41 additions & 0 deletions key_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package query

import (
"context"
"net/http"
"reflect"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -19,6 +21,27 @@ func (f *keyExtractor) ExtractByKey(_ string) (interface{}, bool) {
return nil, false
}

type keyExtractorContext struct {
v map[string]any
}

func (f *keyExtractorContext) ExtractByKey(ctx context.Context, name string) (interface{}, bool) {
if f.v != nil {
if v, ok := f.v[name]; ok {
return v, true
}
if IsCaseInsensitive(ctx) {
name = strings.ToLower(name)
for k, v := range f.v {
if strings.ToLower(k) == name {
return v, true
}
}
}
}
return nil, false
}

type testTags struct {
FooBar string `json:"foo_bar" yaml:"fooBar,omitempty"`
AnonymousField
Expand Down Expand Up @@ -160,6 +183,16 @@ func TestKey_Extract(t *testing.T) {
v: &keyExtractor{v: "value"},
expect: "value",
},
"key extractor context": {
key: "key",
caseInsensitive: true,
v: &keyExtractorContext{
v: map[string]any{
"KEY": "value",
},
},
expect: "value",
},
}
for name, test := range tests {
test := test
Expand Down Expand Up @@ -249,6 +282,14 @@ func TestKey_Extract(t *testing.T) {
},
},
},
"key extractor context (case sensitive)": {
key: "key",
v: &keyExtractorContext{
v: map[string]any{
"KEY": "value",
},
},
},
}
for name, test := range tests {
test := test
Expand Down
Loading