MCPcopy Index your code
hub / github.com/go-openapi/jsonpointer / ExampleUseGoNameProvider

Function ExampleUseGoNameProvider

examples_test.go:196–234  ·  view source on GitHub ↗

ExampleUseGoNameProvider contrasts the two [NameProvider] implementations shipped by [github.com/go-openapi/jsonpointer/jsonname]: - the default provider requires a `json` struct tag to expose a field; - the Go-name provider follows encoding/json conventions and accepts exported untagged fields and

()

Source from the content-addressed store, hash-verified

194// - the Go-name provider follows encoding/json conventions and accepts
195// exported untagged fields and promoted embedded fields as well.
196func ExampleUseGoNameProvider() {
197 type Embedded struct {
198 Nested string // untagged: promoted only by the Go-name provider
199 }
200 type Doc struct {
201 Embedded // untagged embedded: promoted only by the Go-name provider
202
203 Tagged string `json:"tagged"`
204 Untagged string // no tag: visible only to the Go-name provider
205 }
206
207 doc := Doc{
208 Embedded: Embedded{Nested: "promoted"},
209 Tagged: "hit",
210 Untagged: "hidden-by-default",
211 }
212
213 for _, path := range []string{"/tagged", "/Untagged", "/Nested"} {
214 p, err := New(path)
215 if err != nil {
216 fmt.Println(err)
217
218 return
219 }
220
221 // Default provider: only the tagged field resolves.
222 defV, _, defErr := p.Get(doc)
223 // Go-name provider: untagged and promoted fields resolve too.
224 goV, _, goErr := p.Get(doc, WithNameProvider(jsonname.NewGoNameProvider()))
225
226 fmt.Printf("%s -> default=%v (err=%v) | goname=%v (err=%v)\n",
227 path, defV, defErr != nil, goV, goErr != nil)
228 }
229
230 // Output:
231 // /tagged -> default=hit (err=false) | goname=hit (err=false)
232 // /Untagged -> default=<nil> (err=true) | goname=hidden-by-default (err=false)
233 // /Nested -> default=<nil> (err=true) | goname=promoted (err=false)
234}

Callers

nothing calls this directly

Calls 4

NewGoNameProviderFunction · 0.92
NewFunction · 0.85
WithNameProviderFunction · 0.85
GetMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…