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

Update the documentation of custom modifiers to specify the need of quotes in return type #305

Open
sharadregoti opened this issue Dec 23, 2022 · 0 comments

Comments

@sharadregoti
Copy link

Can we update the documentation of custom modifiers, to clearly specify that return type string should have quotes. If it doesn't have quotes we get empty value as result

package main

import (
	"strings"

	"github.com/tidwall/gjson"
)

const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`

func main() {
	gjson.AddModifier("case", func(json, arg string) string {
		if arg == "upper" {
			return strings.ToUpper(json)
		}
		if arg == "lower" {
			return strings.ToLower(json)
		}
		return json
	})
	gjson.AddModifier("age", func(json, arg string) string {
		if arg == "0" {
                        // Quotes is necessary here
			return `"test"`
		}
		return json
	})

	value2 := gjson.Get(json, "name.last|@case:upper")
	println(value2.String())
	value := gjson.Get(json, "name.last|@age:0")
	println(value.String())
}

I am thinking of adding this as note below custom modifier doc section

Please note that returned string type should have double quotes in it. 
	gjson.AddModifier("age", func(json, arg string) string {
		if arg == "custom-arg" {
                        // Quotes is necessary here
			return `"test"`
		}
		return json
	})
@sharadregoti sharadregoti changed the title Update the documentation of custom modifiers Update the documentation of custom modifiers to specify the need of quotes in return type Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant