$ go get github.com/dameleon/structr
structr generate -c ${YOUR_CONFIGURATION_YAML_FILE} ${INPUT_FILE_PATH}...
structr template
structr help
For example, translating JSONSchema type to swift type.
## structr configulation file
# Definitions for translating each type of JSONSchema.
type_translate_map:
string: "String"
integer: "Int"
number: "Float"
boolean: "Bool"
null: "nil"
array: "[{{.InnerType}}]"
object: "{{.InnerType}}"
# If execute structr with "outDir" option, structr outputs file(s) with the definition of "output_filename" template.
output_filename: "{{.Name}}.swift"
# To output the structure of JSONSchema in dependency(a.k.a specified of "$ref" key in JSONSchema).
output_dependencies: true
# Templates for output the structure
structure_template: |
struct {{.Name|toUpperCamelCase}} {
{{range .Properties}}
var {{.Name}}: {{.Type|translateTypeName|toUpperCamelCase}}?{{end}}
{{.Children|extractStructures}}
}
# If the structure has children, to specify strings to nest structure of children
child_structures_nesting: " "
Also, you can get configulation file template following command
$ structr template > ${YOUR_CONFIGURATION_YAML_FILE}
# output with stdout
$ structr generate -c ${YOUR_CONFIGURATION_YAML_FILE} ${INPUT_FILE_PATH}
struct YourStruct {
var hoo: String?
var bar: String?
var child: ChildStruct?
var dependency: YourDependencyStruct?
struct ChildStruct {
var baz: String?
}
}
struct YourDependencyStruct {
var hoge: String?
var fuga: String?
var piyo: String?
}
# output with file
$ structr generate -c ${YOUR_CONFIGURATION_YAML_FILE} --ourDir ${OUTPUT_DIR_PATH} ${INPUT_FILE_PATH}
$ tree ${OUTPUT_DIR_PATH}
OUTPUT_DIR_PATH
├── YourStruct.swift
└── YourDependencyStruct.swift
struct {
.Type string
.InnerType string
}
StructureNode
for more details, see nodes.go
toUpperCamelCase
: returns upper camelized string{"foo-bar-baz" | toUpperCamelCase} -> "FooBarBaz"
{"foo bar baz" | toUpperCamelCase} -> "FooBarBaz"
{"fooBarBaz" | toUpperCamelCase} -> "FooBarBaz"
toLowerCamelCase
: returns lower camelized string{"foo-bar-baz" | toLowerCamelCase} -> "fooBarBaz"
{"foo bar baz" | toLowerCamelCase} -> "fooBarBaz"
{"fooBarBaz" | toLowerCamelCase} -> "fooBarBaz"
translateTypeName
: returns translated string ofTypeNode
- ex:
{"type_translate_map": { "string": "TypedString" }}
{TypeNode{ Name: "string" } | translateTypeName} -> "TypedString"
- ex:
{"type_translate_map": { "string": "TypedString", "array": "[]{{.InnerType}}" }}
{TypeNode{ Name: "array", InnerType: "string" } | translateTypeName} -> "[]TypedString"
- ex:
extractStructures
: returns extracted StructureNodes(for StructureNode.Children)
dameleon dameleon[at]gmail.com
The MIT License (MIT)