Skip to content

Commit c576085

Browse files
committed
Revert "Use pointers for Hints and HintSet"
This reverts commit fe1b071.
1 parent 1d0cdc8 commit c576085

File tree

7 files changed

+33
-59
lines changed

7 files changed

+33
-59
lines changed

examples/infer_multiple_string_rows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"encoding/json"
55

6-
jtdinfer "github.com/bombsimon/jtd-infer-go"
6+
"github.com/bombsimon/jtd-infer-go"
77
)
88

99
func main() {
@@ -12,7 +12,7 @@ func main() {
1212
`{"name":"Jane", "age": 48, "something_nullable": null}`,
1313
}
1414
schema := jtdinfer.
15-
InferStrings(rows, jtdinfer.NewHints()).
15+
InferStrings(rows, jtdinfer.WithoutHints()).
1616
IntoSchema()
1717

1818
j, _ := json.MarshalIndent(schema, "", " ")

examples/infer_simple_value.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package main
33
import (
44
"encoding/json"
55

6-
jtdinfer "github.com/bombsimon/jtd-infer-go"
6+
"github.com/bombsimon/jtd-infer-go"
77
)
88

99
func main() {
1010
schema := jtdinfer.
11-
NewInferrer(jtdinfer.NewHints()).
11+
NewInferrer(jtdinfer.WithoutHints()).
1212
Infer("my-string").
1313
IntoSchema()
1414

examples/infer_with_hints.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"encoding/json"
55

6-
jtdinfer "github.com/bombsimon/jtd-infer-go"
6+
"github.com/bombsimon/jtd-infer-go"
77
)
88

99
func main() {
@@ -23,7 +23,7 @@ func main() {
2323
"discriminator":[{"type":"s", "value":"foo"},{"type":"n", "value":3.14}]
2424
}`,
2525
}
26-
hints := &jtdinfer.Hints{
26+
hints := jtdinfer.Hints{
2727
DefaultNumType: jtdinfer.NumTypeUint32,
2828
Enums: jtdinfer.NewHintSet().Add([]string{"work", "department"}),
2929
Values: jtdinfer.NewHintSet().Add([]string{"values"}),

hints.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@ const Wildcard = "-"
77
// values and discriminators.
88
type Hints struct {
99
DefaultNumType NumType
10-
Enums *HintSet
11-
Values *HintSet
12-
Discriminator *HintSet
10+
Enums HintSet
11+
Values HintSet
12+
Discriminator HintSet
1313
}
1414

15-
// NewHints creates a new empty non nil `Hints`.
16-
func NewHints() *Hints {
17-
return &Hints{
18-
Enums: NewHintSet(),
19-
Values: NewHintSet(),
20-
Discriminator: NewHintSet(),
21-
}
15+
// WithoutHints is a shorthand to return empty hints.
16+
func WithoutHints() Hints {
17+
return Hints{}
2218
}
2319

2420
// SubHints will return the sub hints for all hint sets for the passed key.
25-
func (h Hints) SubHints(key string) *Hints {
26-
return &Hints{
21+
func (h Hints) SubHints(key string) Hints {
22+
return Hints{
2723
DefaultNumType: h.DefaultNumType,
2824
Enums: h.Enums.SubHints(key),
2925
Values: h.Values.SubHints(key),
@@ -32,18 +28,18 @@ func (h Hints) SubHints(key string) *Hints {
3228
}
3329

3430
// IsEnumActive checks if the enum hint set is active.
35-
func (h *Hints) IsEnumActive() bool {
31+
func (h Hints) IsEnumActive() bool {
3632
return h.Enums.IsActive()
3733
}
3834

3935
// IsValuesActive checks if the values hint set is active.
40-
func (h *Hints) IsValuesActive() bool {
36+
func (h Hints) IsValuesActive() bool {
4137
return h.Values.IsActive()
4238
}
4339

4440
// PeekActiveDiscriminator will peek the currently active discriminator, if any.
4541
// The returned boolean tells if there is an active discriminator.
46-
func (h *Hints) PeekActiveDiscriminator() (string, bool) {
42+
func (h Hints) PeekActiveDiscriminator() (string, bool) {
4743
return h.Discriminator.PeekActive()
4844
}
4945

@@ -53,21 +49,21 @@ type HintSet struct {
5349
}
5450

5551
// NewHintSet creates a new empty `HintSet`.
56-
func NewHintSet() *HintSet {
57-
return &HintSet{
52+
func NewHintSet() HintSet {
53+
return HintSet{
5854
Values: [][]string{},
5955
}
6056
}
6157

6258
// Add will add a path (slice) to the `HintSet`.
63-
func (h *HintSet) Add(v []string) *HintSet {
59+
func (h HintSet) Add(v []string) HintSet {
6460
h.Values = append(h.Values, v)
6561
return h
6662
}
6763

6864
// SubHints will filter all the current sets and keep those who's first element
6965
// matches the passed key or wildcard.
70-
func (h *HintSet) SubHints(key string) *HintSet {
66+
func (h HintSet) SubHints(key string) HintSet {
7167
filteredValues := [][]string{}
7268

7369
for _, values := range h.Values {
@@ -81,13 +77,13 @@ func (h *HintSet) SubHints(key string) *HintSet {
8177
}
8278
}
8379

84-
return &HintSet{
80+
return HintSet{
8581
Values: filteredValues,
8682
}
8783
}
8884

8985
// IsActive returns true if any set in the hint set his active.
90-
func (h *HintSet) IsActive() bool {
86+
func (h HintSet) IsActive() bool {
9187
for _, valueList := range h.Values {
9288
if len(valueList) == 0 {
9389
return true
@@ -99,7 +95,7 @@ func (h *HintSet) IsActive() bool {
9995

10096
// PeekActive returns the currently active value if any. The returned boolean
10197
// tells if a value was found.
102-
func (h *HintSet) PeekActive() (string, bool) {
98+
func (h HintSet) PeekActive() (string, bool) {
10399
for _, values := range h.Values {
104100
if len(values) != 1 {
105101
continue

inferred_schema.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ func NewInferredSchema() *InferredSchema {
6464
// https://github.com/jsontypedef/json-typedef-infer/blob/master/src/inferred_schema.rs.
6565
// Since we don't have enums of this kind in Go we're using a struct with
6666
// pointers to a schema instead of wrapping the enums.
67-
func (i *InferredSchema) Infer(value any, hints *Hints) *InferredSchema {
68-
hints = ensureHints(hints)
69-
67+
func (i *InferredSchema) Infer(value any, hints Hints) *InferredSchema {
7068
if value == nil {
7169
return &InferredSchema{
7270
SchemaType: SchemaTypeNullable,
@@ -316,7 +314,7 @@ func (i *InferredSchema) Infer(value any, hints *Hints) *InferredSchema {
316314
}
317315

318316
// IntoSchema will convert an `InferredSchema` to a final `Schema`.
319-
func (i *InferredSchema) IntoSchema(hints *Hints) Schema {
317+
func (i *InferredSchema) IntoSchema(hints Hints) Schema {
320318
switch i.SchemaType {
321319
case SchemaTypeUnknown, SchemaTypeAny:
322320
return Schema{}
@@ -389,23 +387,3 @@ func (i *InferredSchema) IntoSchema(hints *Hints) Schema {
389387

390388
return Schema{}
391389
}
392-
393-
func ensureHints(hints *Hints) *Hints {
394-
if hints == nil {
395-
return NewHints()
396-
}
397-
398-
if hints.Enums == nil {
399-
hints.Enums = NewHintSet()
400-
}
401-
402-
if hints.Values == nil {
403-
hints.Values = NewHintSet()
404-
}
405-
406-
if hints.Discriminator == nil {
407-
hints.Discriminator = NewHintSet()
408-
}
409-
410-
return hints
411-
}

inferrer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
// hints used when inferring.
99
type Inferrer struct {
1010
Inference *InferredSchema
11-
Hints *Hints
11+
Hints Hints
1212
}
1313

1414
// NewInferrer will create a new inferrer with a default `InferredSchema`.
15-
func NewInferrer(hints *Hints) *Inferrer {
15+
func NewInferrer(hints Hints) *Inferrer {
1616
return &Inferrer{
1717
Inference: NewInferredSchema(),
1818
Hints: hints,
@@ -37,7 +37,7 @@ func (i *Inferrer) IntoSchema() Schema {
3737
// the error occurred. If you already have the type of your data such as a slice
3838
// of numbers or a map of strings you can pass them directly to `Infer`. This is
3939
// just a convenience method if all you got is strings.
40-
func InferStrings(rows []string, hints *Hints) *Inferrer {
40+
func InferStrings(rows []string, hints Hints) *Inferrer {
4141
inferrer := NewInferrer(hints)
4242
if len(rows) == 0 {
4343
return inferrer

inferrer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ func TestJTDInfer(t *testing.T) {
144144
"hobbies": {Elements: &Schema{Type: jtd.TypeString}},
145145
},
146146
}
147-
gotSchema := InferStrings(rows, NewHints()).IntoSchema()
147+
gotSchema := InferStrings(rows, WithoutHints()).IntoSchema()
148148

149149
assert.EqualValues(t, expectedSchema, gotSchema)
150150
}
151151

152152
func TestJTDInferrerWithEnumHints(t *testing.T) {
153-
hints := &Hints{
153+
hints := Hints{
154154
Enums: NewHintSet().
155155
Add([]string{"name"}).
156156
Add([]string{"address", "city"}),
@@ -198,7 +198,7 @@ func TestJTDInferrerWithEnumHints(t *testing.T) {
198198
}
199199

200200
func TestJTDInferWithValuesHints(t *testing.T) {
201-
hints := &Hints{
201+
hints := Hints{
202202
Values: NewHintSet().Add([]string{}),
203203
}
204204

@@ -220,7 +220,7 @@ func TestJTDInferWithValuesHints(t *testing.T) {
220220
}
221221

222222
func TestJTDInferWithDiscriminatorHints(t *testing.T) {
223-
hints := &Hints{
223+
hints := Hints{
224224
Discriminator: NewHintSet().Add([]string{"-", "type"}),
225225
}
226226

0 commit comments

Comments
 (0)