Skip to content

Commit fe1b071

Browse files
committed
Use pointers for Hints and HintSet
This avoids copying the values on each new call to infer.
1 parent bee53fb commit fe1b071

File tree

7 files changed

+59
-33
lines changed

7 files changed

+59
-33
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-
"github.com/bombsimon/jtd-infer-go"
6+
jtdinfer "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.WithoutHints()).
15+
InferStrings(rows, jtdinfer.NewHints()).
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-
"github.com/bombsimon/jtd-infer-go"
6+
jtdinfer "github.com/bombsimon/jtd-infer-go"
77
)
88

99
func main() {
1010
schema := jtdinfer.
11-
NewInferrer(jtdinfer.WithoutHints()).
11+
NewInferrer(jtdinfer.NewHints()).
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-
"github.com/bombsimon/jtd-infer-go"
6+
jtdinfer "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: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ 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-
// WithoutHints is a shorthand to return empty hints.
16-
func WithoutHints() Hints {
17-
return Hints{}
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+
}
1822
}
1923

2024
// SubHints will return the sub hints for all hint sets for the passed key.
21-
func (h Hints) SubHints(key string) Hints {
22-
return Hints{
25+
func (h Hints) SubHints(key string) *Hints {
26+
return &Hints{
2327
DefaultNumType: h.DefaultNumType,
2428
Enums: h.Enums.SubHints(key),
2529
Values: h.Values.SubHints(key),
@@ -28,18 +32,18 @@ func (h Hints) SubHints(key string) Hints {
2832
}
2933

3034
// IsEnumActive checks if the enum hint set is active.
31-
func (h Hints) IsEnumActive() bool {
35+
func (h *Hints) IsEnumActive() bool {
3236
return h.Enums.IsActive()
3337
}
3438

3539
// IsValuesActive checks if the values hint set is active.
36-
func (h Hints) IsValuesActive() bool {
40+
func (h *Hints) IsValuesActive() bool {
3741
return h.Values.IsActive()
3842
}
3943

4044
// PeekActiveDiscriminator will peek the currently active discriminator, if any.
4145
// The returned boolean tells if there is an active discriminator.
42-
func (h Hints) PeekActiveDiscriminator() (string, bool) {
46+
func (h *Hints) PeekActiveDiscriminator() (string, bool) {
4347
return h.Discriminator.PeekActive()
4448
}
4549

@@ -49,21 +53,21 @@ type HintSet struct {
4953
}
5054

5155
// NewHintSet creates a new empty `HintSet`.
52-
func NewHintSet() HintSet {
53-
return HintSet{
56+
func NewHintSet() *HintSet {
57+
return &HintSet{
5458
Values: [][]string{},
5559
}
5660
}
5761

5862
// Add will add a path (slice) to the `HintSet`.
59-
func (h HintSet) Add(v []string) HintSet {
63+
func (h *HintSet) Add(v []string) *HintSet {
6064
h.Values = append(h.Values, v)
6165
return h
6266
}
6367

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

6973
for _, values := range h.Values {
@@ -77,13 +81,13 @@ func (h HintSet) SubHints(key string) HintSet {
7781
}
7882
}
7983

80-
return HintSet{
84+
return &HintSet{
8185
Values: filteredValues,
8286
}
8387
}
8488

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

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

inferred_schema.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ 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 {
67+
func (i *InferredSchema) Infer(value any, hints *Hints) *InferredSchema {
68+
hints = ensureHints(hints)
69+
6870
if value == nil {
6971
return &InferredSchema{
7072
SchemaType: SchemaTypeNullable,
@@ -314,7 +316,7 @@ func (i *InferredSchema) Infer(value any, hints Hints) *InferredSchema {
314316
}
315317

316318
// IntoSchema will convert an `InferredSchema` to a final `Schema`.
317-
func (i *InferredSchema) IntoSchema(hints Hints) Schema {
319+
func (i *InferredSchema) IntoSchema(hints *Hints) Schema {
318320
switch i.SchemaType {
319321
case SchemaTypeUnknown, SchemaTypeAny:
320322
return Schema{}
@@ -387,3 +389,23 @@ func (i *InferredSchema) IntoSchema(hints Hints) Schema {
387389

388390
return Schema{}
389391
}
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,
@@ -39,7 +39,7 @@ func (i *Inferrer) IntoSchema() Schema {
3939
//
4040
// If you need to infer simple values like strings or integers they can be
4141
// passed directly to `Infer`.
42-
func InferStrings(rows []string, hints Hints) *Inferrer {
42+
func InferStrings(rows []string, hints *Hints) *Inferrer {
4343
inferrer := NewInferrer(hints)
4444

4545
for _, row := range rows {

inferrer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ func TestJTDInfer(t *testing.T) {
2020
"hobbies": {Elements: &Schema{Type: jtd.TypeString}},
2121
},
2222
}
23-
gotSchema := InferStrings(rows, WithoutHints()).IntoSchema()
23+
gotSchema := InferStrings(rows, NewHints()).IntoSchema()
2424

2525
assert.EqualValues(t, expectedSchema, gotSchema)
2626
}
2727

2828
func TestJTDInferrerWithEnumHints(t *testing.T) {
29-
hints := Hints{
29+
hints := &Hints{
3030
Enums: NewHintSet().
3131
Add([]string{"name"}).
3232
Add([]string{"address", "city"}),
@@ -74,7 +74,7 @@ func TestJTDInferrerWithEnumHints(t *testing.T) {
7474
}
7575

7676
func TestJTDInferWithValuesHints(t *testing.T) {
77-
hints := Hints{
77+
hints := &Hints{
7878
Values: NewHintSet().Add([]string{}),
7979
}
8080

@@ -96,7 +96,7 @@ func TestJTDInferWithValuesHints(t *testing.T) {
9696
}
9797

9898
func TestJTDInferWithDiscriminatorHints(t *testing.T) {
99-
hints := Hints{
99+
hints := &Hints{
100100
Discriminator: NewHintSet().Add([]string{"-", "type"}),
101101
}
102102

0 commit comments

Comments
 (0)