Skip to content

Commit

Permalink
Reuse existing data if found in fixture
Browse files Browse the repository at this point in the history
This allows some data reuse instead of returning all subresources
without a resource ID as an empty object.
  • Loading branch information
brandur committed Jun 27, 2017
1 parent d3b8774 commit ed7c888
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ func (g *DataGenerator) generateInternal(schema *JSONSchema, requestPath string,
}
}

data, err := g.generateResource(schema)
if err != nil {
return nil, err
var data interface{}
if existingData != nil {
data = existingData
} else {
data, err = g.generateResource(schema)
if err != nil {
return nil, err
}
}

if schema.Properties != nil {
Expand All @@ -51,39 +56,40 @@ func (g *DataGenerator) generateInternal(schema *JSONSchema, requestPath string,
if listData != nil {
return listData, nil
}

for key, property := range schema.Properties {
dataMap := data.(map[string]interface{})

subSchema := property

var subExpansions *ExpansionLevel
if expansions != nil {
var ok bool
subExpansions, ok = expansions.expansions[key]

var expansion *JSONSchema
if property.XExpansionResources != nil {
expansion = property.XExpansionResources.OneOf[0]
dataMap, ok := data.(map[string]interface{})
if ok {
for key, property := range schema.Properties {

subSchema := property

var subExpansions *ExpansionLevel
if expansions != nil {
var ok bool
subExpansions, ok = expansions.expansions[key]

var expansion *JSONSchema
if property.XExpansionResources != nil {
expansion = property.XExpansionResources.OneOf[0]
}

// Point to the expanded schema in either the case that an
// expansion was requested on this field or we have a wildcard
// expansion active.
if expansion != nil && (ok || expansions.wildcard) {
subSchema = expansion
}
}

// Point to the expanded schema in either the case that an
// expansion was requested on this field or we have a wildcard
// expansion active.
if expansion != nil && (ok || expansions.wildcard) {
subSchema = expansion
keyData, err := g.generateInternal(
subSchema, requestPath, subExpansions, dataMap[key])
if err == errNotSupported {
continue
}
if err != nil {
return nil, err
}
dataMap[key] = keyData
}

keyData, err := g.generateInternal(
subSchema, requestPath, subExpansions, dataMap[key])
if err == errNotSupported {
continue
}
if err != nil {
return nil, err
}
dataMap[key] = keyData
}
}

Expand Down

0 comments on commit ed7c888

Please sign in to comment.