Skip to content

Commit b4e893e

Browse files
committed
Revert "use pointer semantics for schema"
This reverts commit 60b3b65.
1 parent a70144b commit b4e893e

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
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: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ type Schema struct {
3030
once sync.Once
3131
m map[string]TypeDef
3232

33-
lock sync.Mutex
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
3438
// Cached results of resolving type references to atoms. Only stores
3539
// type references which require fields of Atom to be overriden.
3640
resolvedTypes map[TypeRef]Atom
@@ -39,7 +43,7 @@ type Schema struct {
3943
// A TypeSpecifier references a particular type in a schema.
4044
type TypeSpecifier struct {
4145
Type TypeRef `yaml:"type,omitempty"`
42-
Schema *Schema `yaml:"schema,omitempty"`
46+
Schema Schema `yaml:"schema,omitempty"`
4347
}
4448

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

288292
// 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+
289298
s.lock.Lock()
290299
defer s.lock.Unlock()
291-
s.resolvedTypes = make(map[TypeRef]Atom)
292300

293301
var result Atom
294302
var exists bool
@@ -327,7 +335,7 @@ func (s *Schema) Resolve(tr TypeRef) (Atom, bool) {
327335
// If other is nil this method does nothing.
328336
// If other is already initialized, overwrites it with this instance
329337
// Warning: Not thread safe
330-
func (s *Schema) CopyInto(dst *Schema) {
338+
func (s Schema) CopyInto(dst *Schema) {
331339
if dst == nil {
332340
return
333341
}

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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ 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{
38-
Schema: &schema.Schema{},
39-
}
37+
p := Parser{}
4038
err := yaml.Unmarshal([]byte(s), &p.Schema)
4139
return &p, err
4240
}
@@ -76,7 +74,7 @@ func (p *Parser) TypeNames() (names []string) {
7674
// errors are deferred until a further function is called.
7775
func (p *Parser) Type(name string) ParseableType {
7876
return ParseableType{
79-
Schema: p.Schema,
77+
Schema: &p.Schema,
8078
TypeRef: schema.TypeRef{NamedType: &name},
8179
}
8280
}

0 commit comments

Comments
 (0)