|
| 1 | +package jsonpointer_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/go-openapi/jsonpointer" |
| 7 | +) |
| 8 | + |
| 9 | +var ( |
| 10 | + _ jsonpointer.JSONPointable = CustomDoc{} |
| 11 | + _ jsonpointer.JSONSetable = &CustomDoc{} |
| 12 | +) |
| 13 | + |
| 14 | +// CustomDoc accepts 2 preset properties "propA" and "propB", plus any number of extra properties. |
| 15 | +// |
| 16 | +// All values are strings. |
| 17 | +type CustomDoc struct { |
| 18 | + a string |
| 19 | + b string |
| 20 | + c map[string]string |
| 21 | +} |
| 22 | + |
| 23 | +// JSONLookup implements [jsonpointer.JSONPointable]. |
| 24 | +func (d CustomDoc) JSONLookup(key string) (any, error) { |
| 25 | + switch key { |
| 26 | + case "propA": |
| 27 | + return d.a, nil |
| 28 | + case "propB": |
| 29 | + return d.b, nil |
| 30 | + default: |
| 31 | + if len(d.c) == 0 { |
| 32 | + return nil, fmt.Errorf("key %q not found: %w", key, ErrExampleIface) |
| 33 | + } |
| 34 | + extra, ok := d.c[key] |
| 35 | + if !ok { |
| 36 | + return nil, fmt.Errorf("key %q not found: %w", key, ErrExampleIface) |
| 37 | + } |
| 38 | + |
| 39 | + return extra, nil |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// JSONSet implements [jsonpointer.JSONSetable]. |
| 44 | +func (d *CustomDoc) JSONSet(key string, value any) error { |
| 45 | + asString, ok := value.(string) |
| 46 | + if !ok { |
| 47 | + return fmt.Errorf("a CustomDoc only access strings as values, but got %T: %w", value, ErrExampleIface) |
| 48 | + } |
| 49 | + |
| 50 | + switch key { |
| 51 | + case "propA": |
| 52 | + d.a = asString |
| 53 | + |
| 54 | + return nil |
| 55 | + case "propB": |
| 56 | + d.b = asString |
| 57 | + |
| 58 | + return nil |
| 59 | + default: |
| 60 | + if len(d.c) == 0 { |
| 61 | + d.c = make(map[string]string) |
| 62 | + } |
| 63 | + d.c[key] = asString |
| 64 | + |
| 65 | + return nil |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func Example_iface() { |
| 70 | + doc := CustomDoc{ |
| 71 | + a: "initial value for a", |
| 72 | + b: "initial value for b", |
| 73 | + // no extra values |
| 74 | + } |
| 75 | + |
| 76 | + pointerA, err := jsonpointer.New("/propA") |
| 77 | + if err != nil { |
| 78 | + fmt.Println(err) |
| 79 | + |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + // get the initial value for a |
| 84 | + propA, kind, err := pointerA.Get(doc) |
| 85 | + if err != nil { |
| 86 | + fmt.Println(err) |
| 87 | + |
| 88 | + return |
| 89 | + } |
| 90 | + fmt.Printf("propA (%v): %v\n", kind, propA) |
| 91 | + |
| 92 | + pointerB, err := jsonpointer.New("/propB") |
| 93 | + if err != nil { |
| 94 | + fmt.Println(err) |
| 95 | + |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + // get the initial value for b |
| 100 | + propB, kind, err := pointerB.Get(doc) |
| 101 | + if err != nil { |
| 102 | + fmt.Println(err) |
| 103 | + |
| 104 | + return |
| 105 | + } |
| 106 | + fmt.Printf("propB (%v): %v\n", kind, propB) |
| 107 | + |
| 108 | + pointerC, err := jsonpointer.New("/extra") |
| 109 | + if err != nil { |
| 110 | + fmt.Println(err) |
| 111 | + |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + // not found yet |
| 116 | + _, _, err = pointerC.Get(doc) |
| 117 | + fmt.Printf("propC: %v\n", err) |
| 118 | + |
| 119 | + _, err = pointerA.Set(&doc, "new value for a") // doc is updated in place |
| 120 | + if err != nil { |
| 121 | + fmt.Println(err) |
| 122 | + |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + _, err = pointerB.Set(&doc, "new value for b") |
| 127 | + if err != nil { |
| 128 | + fmt.Println(err) |
| 129 | + |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + _, err = pointerC.Set(&doc, "new extra value") |
| 134 | + if err != nil { |
| 135 | + fmt.Println(err) |
| 136 | + |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + fmt.Printf("updated doc: %v", doc) |
| 141 | + |
| 142 | + // output: |
| 143 | + // propA (string): initial value for a |
| 144 | + // propB (string): initial value for b |
| 145 | + // propC: key "extra" not found: example error |
| 146 | + // updated doc: {new value for a new value for b map[extra:new extra value]} |
| 147 | +} |
0 commit comments