Skip to content

Commit

Permalink
Fix Reference (#1095)
Browse files Browse the repository at this point in the history
Make sure that reference type attributes have been computed
prior to building base attribute that relies on it.
  • Loading branch information
Raphaël Simon committed Mar 1, 2017
1 parent b2a70d7 commit 2098c07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
37 changes: 30 additions & 7 deletions design/apidsl/attribute.go
Expand Up @@ -110,13 +110,7 @@ func Attribute(name string, args ...interface{}) {
return
}

var baseAttr *design.AttributeDefinition
if parent.Reference != nil && parent.Reference.IsObject() {
if att, ok := parent.Reference.ToObject()[name]; ok {
baseAttr = design.DupAtt(att)
}
}

baseAttr := attributeFromRef(name, parent.Reference)
dataType, description, dsl := parseAttributeArgs(baseAttr, args...)
if baseAttr != nil {
if description != "" {
Expand All @@ -143,6 +137,35 @@ func Attribute(name string, args ...interface{}) {
}
}

// attributeFromRef returns a base attribute given a reference data type.
// It takes care of running the DSL on the reference type if it hasn't run yet.
func attributeFromRef(name string, ref design.DataType) *design.AttributeDefinition {
if ref == nil {
return nil
}
switch t := ref.(type) {
case *design.UserTypeDefinition:
if t.DSLFunc != nil {
dsl := t.DSLFunc
t.DSLFunc = nil
dslengine.Execute(dsl, t.AttributeDefinition)
}
if att, ok := t.ToObject()[name]; ok {
return design.DupAtt(att)
}
case *design.MediaTypeDefinition:
if t.DSLFunc != nil {
dsl := t.DSLFunc
t.DSLFunc = nil
dslengine.Execute(dsl, t)
}
if att, ok := t.ToObject()[name]; ok {
return design.DupAtt(att)
}
}
return nil
}

func parseAttributeArgs(baseAttr *design.AttributeDefinition, args ...interface{}) (design.DataType, string, func()) {
var (
dataType design.DataType
Expand Down
4 changes: 3 additions & 1 deletion dslengine/runner.go
Expand Up @@ -294,7 +294,9 @@ func runSet(set DefinitionSet) error {
for _, def := range set[executed:] {
executed++
if source, ok := def.(Source); ok {
Execute(source.DSL(), source)
if dsl := source.DSL(); dsl != nil {
Execute(dsl, source)
}
}
}
if recursed > 100 {
Expand Down

0 comments on commit 2098c07

Please sign in to comment.