| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
|
| 6 |
"github.com/ysmood/gson" |
| 7 |
) |
| 8 |
|
| 9 |
type objType int |
| 10 |
|
| 11 |
const ( |
| 12 |
objTypeStruct objType = iota // such as object |
| 13 |
objTypePrimitive // such as string, bool |
| 14 |
) |
| 15 |
|
| 16 |
type cdpType string |
| 17 |
|
| 18 |
const ( |
| 19 |
cdpTypeTypes cdpType = "types" |
| 20 |
cdpTypeCommands cdpType = "commands" |
| 21 |
cdpTypeEvents cdpType = "events" |
| 22 |
) |
| 23 |
|
| 24 |
type domain struct { |
| 25 |
name string |
| 26 |
experimental bool |
| 27 |
description string |
| 28 |
definitions []*definition |
| 29 |
global gson.JSON |
| 30 |
} |
| 31 |
|
| 32 |
func (schema *domain) find(id string) gson.JSON { |
| 33 |
domain := schema.name |
| 34 |
list := strings.Split(id, ".") |
| 35 |
if len(list) == 2 { |
| 36 |
domain, id = list[0], list[1] |
| 37 |
} |
| 38 |
|
| 39 |
for _, schema := range schema.global.Get("domains").Arr() { |
| 40 |
if schema.Get("domain").Str() == domain { |
| 41 |
for _, s := range schema.Get("types").Arr() { |
| 42 |
if s.Get("id").Str() == id { |
| 43 |
return s |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
panic("cannot find: " + domain + "." + id) |
| 49 |
} |
| 50 |
|
| 51 |
func (schema *domain) ref(id string) bool { |
| 52 |
return schema.find(id).Has("properties") |
| 53 |
} |
| 54 |
|
| 55 |
type definition struct { |
| 56 |
domain *domain |
| 57 |
objType objType |
| 58 |
cdpType cdpType |
| 59 |
typeName string |
| 60 |
enum []string |
| 61 |
name string |
| 62 |
originName string |
| 63 |
description string |
| 64 |
experimental bool |
| 65 |
deprecated bool |
| 66 |
optional bool |
| 67 |
command bool |
| 68 |
returnValue bool |
| 69 |
props []*definition |
| 70 |
skip bool |
| 71 |
} |
| 72 |
|
| 73 |
func parse(schema gson.JSON) []*domain { |
| 74 |
patch(schema) |
| 75 |
|
| 76 |
list := []*domain{} |
| 77 |
|
| 78 |
for _, domainSchema := range schema.Get("domains").Arr() { |
| 79 |
list = append(list, parseDomain(schema, domainSchema)) |
| 80 |
} |
| 81 |
|
| 82 |
return list |
| 83 |
} |
| 84 |
|
| 85 |
func parseDomain(global, schema gson.JSON) *domain { |
| 86 |
domain := &domain{ |
| 87 |
name: schema.Get("domain").Str(), |
| 88 |
experimental: schema.Get("experimental").Bool(), |
| 89 |
definitions: []*definition{}, |
| 90 |
global: global, |
| 91 |
} |
| 92 |
|
| 93 |
if schema.Has("description") { |
| 94 |
domain.description = schema.Get("description").Str() |
| 95 |
} |
| 96 |
|
| 97 |
for _, cdpType := range []cdpType{cdpTypeTypes, cdpTypeCommands, cdpTypeEvents} { |
| 98 |
for _, typeSchame := range schema.Get(string(cdpType)).Arr() { |
| 99 |
domain.definitions = append(domain.definitions, parseDef(domain, cdpType, typeSchame)...) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return domain |
| 104 |
} |
| 105 |
|
| 106 |
func parseDef(domain *domain, cdpType cdpType, schema gson.JSON) []*definition { |
| 107 |
list := []*definition{} |
| 108 |
|
| 109 |
switch cdpType { |
| 110 |
case cdpTypeTypes: |
| 111 |
if schema.Has("properties") { |
| 112 |
list = append(list, parseStruct(domain, cdpType, schema.Get("id").Str(), false, schema, "properties")...) |
| 113 |
} else { |
| 114 |
list = append(list, &definition{ |
| 115 |
domain: domain, |
| 116 |
typeName: typeName(domain, schema), |
| 117 |
name: domain.name + symbol(schema.Get("id").Str()), |
| 118 |
description: schema.Get("description").Str(), |
| 119 |
deprecated: schema.Get("deprecated").Bool(), |
| 120 |
experimental: schema.Get("experimental").Bool(), |
| 121 |
objType: objTypePrimitive, |
| 122 |
enum: enumList(schema), |
| 123 |
skip: schema.Get("skip").Bool(), |
| 124 |
}) |
| 125 |
} |
| 126 |
case cdpTypeCommands: |
| 127 |
list = append(list, parseStruct(domain, cdpType, schema.Get("name").Str(), true, schema, "parameters")...) |
| 128 |
if schema.Has("returns") { |
| 129 |
list = append(list, parseStruct(domain, cdpType, schema.Get("name").Str()+"Result", false, schema, "returns")...) |
| 130 |
} |
| 131 |
|
| 132 |
case cdpTypeEvents: |
| 133 |
list = append(list, parseStruct(domain, cdpType, schema.Get("name").Str(), false, schema, "parameters")...) |
| 134 |
|
| 135 |
default: |
| 136 |
panic("type error: " + schema.Str()) |
| 137 |
} |
| 138 |
|
| 139 |
return list |
| 140 |
} |
| 141 |
|
| 142 |
func parseStruct(domain *domain, cdpType cdpType, name string, isCommand bool, schema gson.JSON, propsPath string) []*definition { |
| 143 |
list := []*definition{} |
| 144 |
|
| 145 |
props := []*definition{} |
| 146 |
for _, propSchema := range schema.Get(propsPath).Arr() { |
| 147 |
typeName := typeName(domain, propSchema) |
| 148 |
|
| 149 |
prop := &definition{ |
| 150 |
objType: objTypePrimitive, |
| 151 |
name: symbol(propSchema.Get("name").Str()), |
| 152 |
originName: propSchema.Get("name").Str(), |
| 153 |
description: propSchema.Get("description").Str(), |
| 154 |
optional: propSchema.Get("optional").Bool(), |
| 155 |
deprecated: propSchema.Get("deprecated").Bool(), |
| 156 |
experimental: propSchema.Get("experimental").Bool(), |
| 157 |
typeName: typeName, |
| 158 |
} |
| 159 |
|
| 160 |
props = append(props, prop) |
| 161 |
|
| 162 |
if propSchema.Has("enum") { |
| 163 |
enum := &definition{ |
| 164 |
domain: domain, |
| 165 |
name: domain.name + symbol(name) + symbol(propSchema.Get("name").Str()), |
| 166 |
objType: objTypePrimitive, |
| 167 |
description: "enum", |
| 168 |
enum: enumList(propSchema), |
| 169 |
typeName: typeName, |
| 170 |
} |
| 171 |
list = append(list, enum) |
| 172 |
|
| 173 |
prop.typeName = enum.name |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
desc := schema.Get("description").Str() |
| 178 |
if !isCommand && schema.Has("returns") { |
| 179 |
desc = "..." |
| 180 |
} |
| 181 |
|
| 182 |
list = append(list, &definition{ |
| 183 |
domain: domain, |
| 184 |
cdpType: cdpType, |
| 185 |
objType: objTypeStruct, |
| 186 |
typeName: typeName(domain, schema), |
| 187 |
name: domain.name + symbol(name), |
| 188 |
originName: name, |
| 189 |
description: desc, |
| 190 |
optional: schema.Get("optional").Bool(), |
| 191 |
deprecated: schema.Get("deprecated").Bool(), |
| 192 |
experimental: schema.Get("experimental").Bool(), |
| 193 |
props: props, |
| 194 |
command: isCommand, |
| 195 |
returnValue: schema.Has("returns"), |
| 196 |
skip: schema.Get("skip").Bool(), |
| 197 |
}) |
| 198 |
|
| 199 |
return list |
| 200 |
} |