Skip to content

Commit

Permalink
[*] refactor: 实现 Caseless 功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-M-C committed Apr 26, 2021
1 parent aa3f096 commit ba6fa8a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
4 changes: 4 additions & 0 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (v *V) getFromObjectChildren(key string) (child *V, exist bool) {
return child, true
}

if v.children.lowerCaseKeys == nil {
return nil, false
}

lowerCaseKey := strings.ToLower(key)
keys, exist := v.children.lowerCaseKeys[lowerCaseKey]
if !exist {
Expand Down
2 changes: 1 addition & 1 deletion get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestGet(t *testing.T) {
check(t, err, "GetInt32", i == -1234)
}
{
i, err := o.GetInt32("data", "negATive") // caseless
i, err := o.Caseless().GetInt32("data", "negATive") // caseless
check(t, err, "GetInt32_caseless", i == -1234)
}
{
Expand Down
2 changes: 1 addition & 1 deletion insert_append_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestDelete(t *testing.T) {
return
}

_, err = o.Get("object")
_, err = o.Caseless().Get("object")
if err != nil {
t.Errorf("unexpected error: %v", err)
return
Expand Down
45 changes: 41 additions & 4 deletions jsonvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type children struct {

// As official json package supports caseless key accessing, I decide to do it as well
lowerCaseKeys map[string]map[string]struct{}
caseless bool
}

func new(t jsonparser.ValueType) *V {
Expand All @@ -92,10 +93,9 @@ func new(t jsonparser.ValueType) *V {

func newObject() *V {
v := new(jsonparser.Object)
v.children = children{
object: make(map[string]*V),
lowerCaseKeys: make(map[string]map[string]struct{}),
}
v.children.object = make(map[string]*V)
v.children.caseless = false
v.children.lowerCaseKeys = nil
return v
}

Expand All @@ -106,6 +106,9 @@ func newArray() *V {
}

func (v *V) addCaselessKey(k string) {
if v.children.lowerCaseKeys == nil {
return
}
lowerK := strings.ToLower(k)
keys, exist := v.children.lowerCaseKeys[lowerK]
if !exist {
Expand All @@ -116,6 +119,9 @@ func (v *V) addCaselessKey(k string) {
}

func (v *V) delCaselessKey(k string) {
if v.children.lowerCaseKeys == nil {
return
}
lowerK := strings.ToLower(k)
keys, exist := v.children.lowerCaseKeys[lowerK]
if !exist {
Expand Down Expand Up @@ -912,3 +918,34 @@ func (v *V) bufArrChildren(buf *bytes.Buffer) {
})
buf.WriteByte(']')
}

// Caseless mark current value to be caseless mode
func (v *V) Caseless() *V {
if v.children.caseless {
return v
}

v.children.caseless = true

switch v.valueType {
default:
return v

case jsonparser.Array:
for _, child := range v.children.array {
child.Caseless()
}
return v

case jsonparser.Object:
if v.children.lowerCaseKeys == nil {
v.children.lowerCaseKeys = make(map[string]map[string]struct{}, len(v.children.object))
for k, child := range v.children.object {
child.Caseless()
v.addCaselessKey(k)
}
}
}

return v
}

0 comments on commit ba6fa8a

Please sign in to comment.