Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
Simplify generated schema a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
VladRassokhin committed Jul 9, 2018
1 parent b6e93a1 commit 0c62bc8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
18 changes: 15 additions & 3 deletions schemas-extractor/template/generate-schema.go
Expand Up @@ -108,7 +108,7 @@ func (m schemaMap) Export() SchemaInfo {
func export(v *schema.Schema) SchemaDefinition {
item := SchemaDefinition{}

item.Type = fmt.Sprintf("%s", v.Type)
item.Type = shortenType(fmt.Sprintf("%s", v.Type))
item.Optional = v.Optional
item.Required = v.Required
item.Description = v.Description
Expand All @@ -133,15 +133,27 @@ func export(v *schema.Schema) SchemaDefinition {
return item
}

func shortenType(value string) string {
if (len(value) > 4 && value[0:4] == "Type") {
return value[4:]
}
return value
}

func exportValue(value interface{}, t string) *SchemaElement {
s2, ok := value.(*schema.Schema)
if ok {
return &SchemaElement{Type: "SchemaElements", ElementsType: fmt.Sprintf("%s", s2.Type)}
return &SchemaElement{Type: "SchemaElements", ElementsType: shortenType(fmt.Sprintf("%s", s2.Type))}
}
r2, ok := value.(*schema.Resource)
if ok {
return &SchemaElement{Type: "SchemaInfo", Info: ExportResource(r2)}
}
vt, ok := value.(schema.ValueType)
if ok {
return &SchemaElement{Value: shortenType(fmt.Sprintf("%v", vt))}
}
// Unknown case
return &SchemaElement{Type: t, Value: fmt.Sprintf("%v", value)}
}

Expand Down Expand Up @@ -177,7 +189,7 @@ func DoGenerate(provider *schema.Provider, providerName string, outputFilePath s
}

type SchemaElement struct {
// One of ValueType or "SchemaElements" or "SchemaInfo"
// One of "schema.ValueType" or "SchemaElements" or "SchemaInfo"
Type string `json:",omitempty"`
// Set for simple types (from ValueType)
Value string `json:",omitempty"`
Expand Down
Expand Up @@ -388,16 +388,16 @@ class TypeModelLoader(val external: Map<String, TypeModelProvider.Additional>) {
typeObject
)
*/
return when (string) {
"TypeInvalid" -> Types.Invalid
"TypeBool" -> Types.Boolean
"TypeInt" -> Types.Number
"TypeFloat" -> Types.Number
"TypeString" -> Types.String
"TypeList" -> Types.Array
"TypeSet" -> Types.Array
"TypeMap" -> Types.Object
"TypeAny" -> Types.Any
return when (string?.removePrefix("Type")) {
"Invalid" -> Types.Invalid
"Bool" -> Types.Boolean
"Int" -> Types.Number
"Float" -> Types.Number
"String" -> Types.String
"List" -> Types.Array
"Set" -> Types.Array
"Map" -> Types.Object
"Any" -> Types.Any
else -> Types.Invalid
}
}
Expand Down

0 comments on commit 0c62bc8

Please sign in to comment.