Skip to content

Commit 60b3b65

Browse files
committed
use pointer semantics for schema
1 parent 00b6b52 commit 60b3b65

File tree

5 files changed

+18
-24
lines changed

5 files changed

+18
-24
lines changed

internal/fixture/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ func (cs ChangeParser) run(state *State) error {
477477
// Swap the schema in for use with the live object so it merges.
478478
// If the schema is incompatible, this will fail validation.
479479

480-
liveWithNewSchema, err := typed.AsTyped(state.Live.AsValue(), &cs.Parser.Schema, state.Live.TypeRef())
480+
liveWithNewSchema, err := typed.AsTyped(state.Live.AsValue(), cs.Parser.Schema, state.Live.TypeRef())
481481
if err != nil {
482482
return err
483483
}

schema/elements.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ type Schema struct {
3030
once sync.Once
3131
m map[string]TypeDef
3232

33-
// Once used to protect the initialization of `lock` field.
34-
lockOnce sync.Once
35-
// Lock which protects writes to resolvedTypes. Used as pointer so that
36-
// schema may be used as a value type
37-
lock *sync.Mutex
33+
lock sync.Mutex
3834
// Cached results of resolving type references to atoms. Only stores
3935
// type references which require fields of Atom to be overriden.
4036
resolvedTypes map[TypeRef]Atom
@@ -43,7 +39,7 @@ type Schema struct {
4339
// A TypeSpecifier references a particular type in a schema.
4440
type TypeSpecifier struct {
4541
Type TypeRef `yaml:"type,omitempty"`
46-
Schema Schema `yaml:"schema,omitempty"`
42+
Schema *Schema `yaml:"schema,omitempty"`
4743
}
4844

4945
// TypeDef represents a named type in a schema.
@@ -290,13 +286,9 @@ func (s *Schema) Resolve(tr TypeRef) (Atom, bool) {
290286
}
291287

292288
// Check to see if we have a cached version of this type
293-
s.lockOnce.Do(func() {
294-
s.lock = &sync.Mutex{}
295-
s.resolvedTypes = make(map[TypeRef]Atom)
296-
})
297-
298289
s.lock.Lock()
299290
defer s.lock.Unlock()
291+
s.resolvedTypes = make(map[TypeRef]Atom)
300292

301293
var result Atom
302294
var exists bool
@@ -335,7 +327,7 @@ func (s *Schema) Resolve(tr TypeRef) (Atom, bool) {
335327
// If other is nil this method does nothing.
336328
// If other is already initialized, overwrites it with this instance
337329
// Warning: Not thread safe
338-
func (s Schema) CopyInto(dst *Schema) {
330+
func (s *Schema) CopyInto(dst *Schema) {
339331
if dst == nil {
340332
return
341333
}

schema/elements_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func TestCopyInto(t *testing.T) {
161161
theCopy := Schema{}
162162
s.CopyInto(&theCopy)
163163

164-
if !reflect.DeepEqual(s, theCopy) {
164+
if !reflect.DeepEqual(&s, &theCopy) {
165165
t.Fatal("")
166166
}
167167

@@ -170,7 +170,7 @@ func TestCopyInto(t *testing.T) {
170170
theCopy = Schema{}
171171
s.CopyInto(&theCopy)
172172

173-
if !reflect.DeepEqual(s, theCopy) {
173+
if !reflect.DeepEqual(&s, &theCopy) {
174174
t.Fatal("")
175175
}
176176
})

schema/equals_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ func fuzzInterface(i *interface{}, c fuzz.Continue) {
3131
*i = &m
3232
}
3333

34-
func (Schema) Generate(rand *rand.Rand, size int) reflect.Value {
35-
s := Schema{}
34+
func (*Schema) Generate(rand *rand.Rand, size int) reflect.Value {
35+
s := &Schema{}
3636
f := fuzz.New().RandSource(rand).MaxDepth(4)
37-
f.Fuzz(&s)
37+
f.Fuzz(s)
3838
return reflect.ValueOf(s)
3939
}
4040

@@ -73,13 +73,13 @@ func TestEquals(t *testing.T) {
7373
// The "copy known fields" section of these function is to break if folks
7474
// add new fields without fixing the Equals function and this test.
7575
funcs := []interface{}{
76-
func(x Schema) bool {
77-
if !x.Equals(&x) {
76+
func(x *Schema) bool {
77+
if !x.Equals(x) {
7878
return false
7979
}
8080
var y Schema
8181
y.Types = x.Types
82-
return x.Equals(&y) == reflect.DeepEqual(&x, &y)
82+
return x.Equals(&y) == reflect.DeepEqual(x, &y)
8383
},
8484
func(x TypeDef) bool {
8585
if !x.Equals(&x) {

typed/parser.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ type YAMLObject string
2929

3030
// Parser implements YAMLParser and allows introspecting the schema.
3131
type Parser struct {
32-
Schema schema.Schema
32+
Schema *schema.Schema
3333
}
3434

3535
// create builds an unvalidated parser.
3636
func create(s YAMLObject) (*Parser, error) {
37-
p := Parser{}
37+
p := Parser{
38+
Schema: &schema.Schema{},
39+
}
3840
err := yaml.Unmarshal([]byte(s), &p.Schema)
3941
return &p, err
4042
}
@@ -74,7 +76,7 @@ func (p *Parser) TypeNames() (names []string) {
7476
// errors are deferred until a further function is called.
7577
func (p *Parser) Type(name string) ParseableType {
7678
return ParseableType{
77-
Schema: &p.Schema,
79+
Schema: p.Schema,
7880
TypeRef: schema.TypeRef{NamedType: &name},
7981
}
8082
}

0 commit comments

Comments
 (0)