Skip to content

Commit ae73433

Browse files
committed
chore: wired the local jsonname package & relinted
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 5667aac commit ae73433

20 files changed

Lines changed: 189 additions & 195 deletions

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ linters:
44
disable:
55
- depguard
66
- funlen
7+
- goconst
78
- godox
9+
- gomodguard
10+
- gomodguard_v2
811
- exhaustruct
912
- nlreturn
1013
- nonamedreturns

dash_token_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"github.com/go-openapi/testify/v2/require"
1212
)
1313

14-
// RFC 6901 §4: the "-" token refers to the (nonexistent) element after the
15-
// last array element. It is always an error on Get/Offset, valid only as
16-
// the terminal token of a Set against a slice (append, per RFC 6902).
14+
// RFC 6901 §4: the "-" token refers to the (nonexistent) element after the last array element.
15+
// It is always an error on Get/Offset, valid only as the terminal token of a Set against a slice
16+
// (append, per RFC 6902).
1717

1818
func TestDashToken_GetAlwaysErrors(t *testing.T) {
1919
t.Parallel()
@@ -56,7 +56,8 @@ func TestDashToken_GetAlwaysErrors(t *testing.T) {
5656
})
5757

5858
t.Run("dash on map key is a regular lookup, not an error", func(t *testing.T) {
59-
// "-" is only special for arrays. A literal "-" key in a map is fine.
59+
// "-" is only special for arrays.
60+
// A literal "-" key in a map is fine.
6061
doc := map[string]any{"-": 42}
6162
p, err := New("/-")
6263
require.NoError(t, err)
@@ -208,8 +209,8 @@ func (d *dashSetter) JSONSet(key string, value any) error {
208209
func TestDashToken_JSONSetableReceivesRawDash(t *testing.T) {
209210
t.Parallel()
210211

211-
// When the terminal parent implements JSONSetable, the dash token is
212-
// passed through verbatim. Semantics are the user type's responsibility.
212+
// When the terminal parent implements JSONSetable, the dash token is passed through verbatim.
213+
// Semantics are the user type's responsibility.
213214
ds := &dashSetter{}
214215
p, err := New("/-")
215216
require.NoError(t, err)

errors.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ const (
2121
// ErrUnsupportedValueType indicates that a value of the wrong type is being set.
2222
ErrUnsupportedValueType pointerError = "only structs, pointers, maps and slices are supported for setting values"
2323

24-
// ErrDashToken indicates use of the RFC 6901 "-" reference token
25-
// in a context where it cannot be resolved.
24+
// ErrDashToken indicates use of the RFC 6901 "-" reference token in a context where it cannot be
25+
// resolved.
2626
//
27-
// Per RFC 6901 §4 the "-" token refers to the (nonexistent) element
28-
// after the last array element. It may only be used as the terminal
29-
// token of a [Pointer.Set] against a slice, where it means "append".
30-
// Any other use (get, offset, intermediate traversal, non-slice target)
31-
// is an error condition that wraps this sentinel.
27+
// Per RFC 6901 §4 the "-" token refers to the (nonexistent) element after the last array element.
28+
// It may only be used as the terminal token of a [Pointer.Set] against a slice, where it means
29+
// "append".
30+
//
31+
// Any other use (get, offset, intermediate traversal, non-slice target) is an error condition that
32+
// wraps this sentinel.
3233
ErrDashToken pointerError = `the "-" array token cannot be resolved here` //nolint:gosec // G101 false positive: this is a JSON Pointer reference token, not a credential.
3334
)
3435

examples_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"errors"
99
"fmt"
1010

11-
"github.com/go-openapi/swag/jsonname"
11+
"github.com/go-openapi/jsonpointer/jsonname"
1212
)
1313

1414
var ErrExampleStruct = errors.New("example error")
@@ -60,7 +60,7 @@ func ExampleNew() {
6060
// key contains "/"
6161
fmt.Printf("pointer to key %q: %q\n", Unescape("foo~1"), escaped1.String())
6262

63-
// output:
63+
// Output:
6464
// empty pointer: ""
6565
// pointer to object key: "/foo"
6666
// pointer to array element: "/foo/1"
@@ -132,10 +132,10 @@ func ExamplePointer_Set() {
132132
// doc: jsonpointer.exampleDocument{Foo:[]string{"bar", "hey my"}}
133133
}
134134

135-
// ExamplePointer_Set_append demonstrates the RFC 6901 "-" token as an
136-
// append operation on a slice. On nested slices reached through an
137-
// addressable parent (map entry, pointer to struct, ...), the append is
138-
// performed in place and the returned document is the same reference.
135+
// ExamplePointer_Set_append demonstrates the RFC 6901 "-" token as an append operation on a slice.
136+
//
137+
// On nested slices reached through an addressable parent (map entry, pointer to struct, ...), the
138+
// append is performed in place and the returned document is the same reference.
139139
func ExamplePointer_Set_append() {
140140
doc := map[string]any{"foo": []any{"bar"}}
141141

@@ -154,15 +154,14 @@ func ExamplePointer_Set_append() {
154154

155155
fmt.Printf("doc: %v\n", doc["foo"])
156156

157-
// Output:
158-
// doc: [bar baz]
157+
// Output: doc: [bar baz]
159158
}
160159

161-
// ExamplePointer_Set_appendTopLevelSlice shows the one case where the
162-
// returned document is load-bearing: appending to a top-level slice
163-
// passed by value. The library cannot rebind the slice header in the
164-
// caller's variable, so callers must use the returned document (or pass
165-
// *[]T to get in-place rebind).
160+
// ExamplePointer_Set_appendTopLevelSlice shows the one case where the returned document is
161+
// load-bearing: appending to a top-level slice passed by value.
162+
//
163+
// The library cannot rebind the slice header in the caller's variable, so callers must use the
164+
// returned document (or pass *[]T to get in-place rebind).
166165
func ExamplePointer_Set_appendTopLevelSlice() {
167166
doc := []int{1, 2}
168167

@@ -188,8 +187,8 @@ func ExamplePointer_Set_appendTopLevelSlice() {
188187
// returned: [1 2 3]
189188
}
190189

191-
// ExampleUseGoNameProvider contrasts the two [NameProvider] implementations
192-
// shipped by [github.com/go-openapi/swag/jsonname]:
190+
// ExampleUseGoNameProvider contrasts the two [NameProvider] implementations shipped by
191+
// [github.com/go-openapi/jsonpointer/jsonname]:
193192
//
194193
// - the default provider requires a `json` struct tag to expose a field;
195194
// - the Go-name provider follows encoding/json conventions and accepts

go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
module github.com/go-openapi/jsonpointer
22

3-
require (
4-
github.com/go-openapi/swag/jsonname v0.26.1
5-
github.com/go-openapi/testify/v2 v2.6.0
6-
)
3+
require github.com/go-openapi/testify/v2 v2.6.0
74

85
go 1.25.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
github.com/go-openapi/swag/jsonname v0.26.1 h1:VReupaV6WxlAsCn0e4DUfgV6bPmINnPpyJDLqSfNPcE=
2-
github.com/go-openapi/swag/jsonname v0.26.1/go.mod h1:OvdW6BoWoj33pTfi7x9vFrgmT+fk7aw0BRwvCE0YOuc=
31
github.com/go-openapi/testify/v2 v2.6.0 h1:5PKH2HE7YJ/LuRPQGvSxBRlFXNQhSetBLlGAgUEu3ug=
42
github.com/go-openapi/testify/v2 v2.6.0/go.mod h1:SgsVHtfooshd0tublTtJ50FPKhujf47YRqauXXOUxfw=

iface_example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func Example_iface() {
142142

143143
fmt.Printf("updated doc: %v", doc)
144144

145-
// output:
145+
// Output:
146146
// propA (string): initial value for a
147147
// propB (string): initial value for b
148148
// propC: key "extra" not found: example error

ifaces.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,42 @@ package jsonpointer
55

66
import "reflect"
77

8-
// JSONPointable is an interface for structs to implement,
9-
// when they need to customize the json pointer process or want to avoid the use of reflection.
8+
// JSONPointable is an interface for structs to implement, when they need to customize the json
9+
// pointer process or want to avoid the use of reflection.
1010
type JSONPointable interface {
1111
// JSONLookup returns a value pointed at this (unescaped) key.
1212
JSONLookup(key string) (any, error)
1313
}
1414

15-
// JSONSetable is an interface for structs to implement,
16-
// when they need to customize the json pointer process or want to avoid the use of reflection.
15+
// JSONSetable is an interface for structs to implement, when they need to customize the json
16+
// pointer process or want to avoid the use of reflection.
1717
//
1818
// # Handling of the RFC 6901 "-" token
1919
//
20-
// When a type implementing JSONSetable is the terminal parent of a [Pointer.Set]
21-
// call, the library passes the raw reference token to JSONSet without
22-
// interpretation. In particular, the RFC 6901 "-" token (which conventionally
23-
// means "append" for arrays, per RFC 6902) is forwarded verbatim as the key
24-
// argument. Implementations that model an array-like container are expected
25-
// to give "-" the append semantics; implementations that do not should return
26-
// an error wrapping [ErrDashToken] (or [ErrPointer]) for clarity.
20+
// When a type implementing JSONSetable is the terminal parent of a [Pointer.Set] call, the library
21+
// passes the raw reference token to JSONSet without interpretation.
2722
//
28-
// Implementations are responsible for any in-place mutation: the library does
29-
// not attempt to rebind the result of JSONSet into a parent container.
23+
// In particular, the RFC 6901 "-" token (which conventionally means "append" for arrays, per RFC
24+
// 6902) is forwarded verbatim as the key argument.
25+
//
26+
// Implementations that model an array-like container are expected to give "-" the append semantics;
27+
// implementations that do not should return an error wrapping [ErrDashToken] (or [ErrPointer]) for
28+
// clarity.
29+
//
30+
// Implementations are responsible for any in-place mutation: the library does not attempt to rebind
31+
// the result of JSONSet into a parent container.
3032
type JSONSetable interface {
3133
// JSONSet sets the value pointed at the (unescaped) key.
3234
//
33-
// The key may be the RFC 6901 "-" token when the pointer targets a
34-
// slice-like member; see the interface documentation for details.
35+
// The key may be the RFC 6901 "-" token when the pointer targets a slice-like member; see the
36+
// interface documentation for details.
3537
JSONSet(key string, value any) error
3638
}
3739

3840
// NameProvider knows how to resolve go struct fields into json names.
3941
//
40-
// The default provider is brought by [github.com/go-openapi/swag/jsonname.DefaultJSONNameProvider].
42+
// The default provider is brought by
43+
// [github.com/go-openapi/jsonpointer/jsonname.DefaultJSONNameProvider].
4144
type NameProvider interface {
4245
// GetGoName gets the go name for a json property name
4346
GetGoName(subject any, name string) (string, bool)

jsonname/go.mod

Lines changed: 0 additions & 5 deletions
This file was deleted.

jsonname/go.sum

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)