mirror of https://github.com/tidwall/gjson.git
sync map added and remove modifier functionality added
This commit is contained in:
parent
c2bc5a409a
commit
88b0f02427
55
gjson.go
55
gjson.go
|
@ -4,6 +4,7 @@ package gjson
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf16"
|
"unicode/utf16"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
@ -957,7 +958,7 @@ func isDotPiperChar(s string) bool {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_, ok := modifiers[s[1:i]]
|
_, ok := modifiers.Load(s[1:i])
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
return c == '[' || c == '{'
|
return c == '[' || c == '{'
|
||||||
|
@ -2752,7 +2753,8 @@ func execModifier(json, path string) (pathOut, res string, ok bool) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if fn, ok := modifiers[name]; ok {
|
if fn, ok := modifiers.Load(name); ok {
|
||||||
|
function := fn.(func(json, arg string) string)
|
||||||
var args string
|
var args string
|
||||||
if hasArgs {
|
if hasArgs {
|
||||||
var parsedArgs bool
|
var parsedArgs bool
|
||||||
|
@ -2783,7 +2785,7 @@ func execModifier(json, path string) (pathOut, res string, ok bool) {
|
||||||
pathOut = pathOut[i:]
|
pathOut = pathOut[i:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return pathOut, fn(json, args), true
|
return pathOut, function(json, args), true
|
||||||
}
|
}
|
||||||
return pathOut, res, false
|
return pathOut, res, false
|
||||||
}
|
}
|
||||||
|
@ -2800,39 +2802,50 @@ func unwrap(json string) string {
|
||||||
// DisableModifiers will disable the modifier syntax
|
// DisableModifiers will disable the modifier syntax
|
||||||
var DisableModifiers = false
|
var DisableModifiers = false
|
||||||
|
|
||||||
var modifiers map[string]func(json, arg string) string
|
var modifiers sync.Map
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
modifiers = map[string]func(json, arg string) string{
|
modifiers.Store("pretty", modPretty)
|
||||||
"pretty": modPretty,
|
modifiers.Store("ugly", modUgly)
|
||||||
"ugly": modUgly,
|
modifiers.Store("reverse", modReverse)
|
||||||
"reverse": modReverse,
|
modifiers.Store("this", modThis)
|
||||||
"this": modThis,
|
modifiers.Store("flatten", modFlatten)
|
||||||
"flatten": modFlatten,
|
modifiers.Store("join", modJoin)
|
||||||
"join": modJoin,
|
modifiers.Store("valid", modValid)
|
||||||
"valid": modValid,
|
modifiers.Store("keys", modKeys)
|
||||||
"keys": modKeys,
|
modifiers.Store("values", modValues)
|
||||||
"values": modValues,
|
modifiers.Store("tostr", modToStr)
|
||||||
"tostr": modToStr,
|
modifiers.Store("fromstr", modFromStr)
|
||||||
"fromstr": modFromStr,
|
modifiers.Store("group", modGroup)
|
||||||
"group": modGroup,
|
modifiers.Store("dig", modDig)
|
||||||
"dig": modDig,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddModifier binds a custom modifier command to the GJSON syntax.
|
// AddModifier binds a custom modifier command to the GJSON syntax.
|
||||||
// This operation is not thread safe and should be executed prior to
|
// This operation is not thread safe and should be executed prior to
|
||||||
// using all other gjson function.
|
// using all other gjson function.
|
||||||
func AddModifier(name string, fn func(json, arg string) string) {
|
func AddModifier(name string, fn func(json, arg string) string) {
|
||||||
modifiers[name] = fn
|
modifiers.Store(name, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ModifierExists returns true when the specified modifier exists.
|
// ModifierExists returns true when the specified modifier exists.
|
||||||
func ModifierExists(name string, fn func(json, arg string) string) bool {
|
func ModifierExists(name string, fn func(json, arg string) string) bool {
|
||||||
_, ok := modifiers[name]
|
_, ok := modifiers.Load(name)
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteModifier delete a custom modifier
|
||||||
|
func DeleteModifier(name string) {
|
||||||
|
modifiers.Delete(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteModifierIfExists delete a custom modifier if it is existing
|
||||||
|
func DeleteModifierIfExists(name string) {
|
||||||
|
_, ok := modifiers.Load(name)
|
||||||
|
if ok {
|
||||||
|
modifiers.Delete(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// cleanWS remove any non-whitespace from string
|
// cleanWS remove any non-whitespace from string
|
||||||
func cleanWS(s string) string {
|
func cleanWS(s string) string {
|
||||||
for i := 0; i < len(s); i++ {
|
for i := 0; i < len(s); i++ {
|
||||||
|
|
Loading…
Reference in New Issue