| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"log" |
| 10 |
"strings" |
| 11 |
) |
| 12 |
|
| 13 |
var typeNames = make(map[*Type]string) |
| 14 |
var genTypes []*newType |
| 15 |
|
| 16 |
func findTypeNames(model *Model) { |
| 17 |
for _, s := range model.Structures { |
| 18 |
for _, e := range s.Extends { |
| 19 |
nameType(e, nil) |
| 20 |
} |
| 21 |
for _, m := range s.Mixins { |
| 22 |
nameType(m, nil) |
| 23 |
} |
| 24 |
for _, p := range s.Properties { |
| 25 |
nameType(p.Type, []string{s.Name, p.Name}) |
| 26 |
} |
| 27 |
} |
| 28 |
for _, t := range model.Enumerations { |
| 29 |
nameType(t.Type, []string{t.Name}) |
| 30 |
} |
| 31 |
for _, t := range model.TypeAliases { |
| 32 |
nameType(t.Type, []string{t.Name}) |
| 33 |
} |
| 34 |
for _, r := range model.Requests { |
| 35 |
nameType(r.Params, []string{"Param", r.Method}) |
| 36 |
nameType(r.Result, []string{"Result", r.Method}) |
| 37 |
nameType(r.RegistrationOptions, []string{"RegOpt", r.Method}) |
| 38 |
} |
| 39 |
for _, n := range model.Notifications { |
| 40 |
nameType(n.Params, []string{"Param", n.Method}) |
| 41 |
nameType(n.RegistrationOptions, []string{"RegOpt", n.Method}) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
func nameType(t *Type, path []string) string { |
| 48 |
if t == nil || typeNames[t] != "" { |
| 49 |
return "" |
| 50 |
} |
| 51 |
switch t.Kind { |
| 52 |
case "base": |
| 53 |
typeNames[t] = t.Name |
| 54 |
return t.Name |
| 55 |
case "reference": |
| 56 |
typeNames[t] = t.Name |
| 57 |
return t.Name |
| 58 |
case "array": |
| 59 |
nm := "[]" + nameType(t.Element, append(path, "Elem")) |
| 60 |
typeNames[t] = nm |
| 61 |
return nm |
| 62 |
case "map": |
| 63 |
key := nameType(t.Key, nil) |
| 64 |
value := nameType(t.Value.(*Type), append(path, "Value")) |
| 65 |
nm := "map[" + key + "]" + value |
| 66 |
typeNames[t] = nm |
| 67 |
return nm |
| 68 |
|
| 69 |
case "and": |
| 70 |
nm := nameFromPath("And", path) |
| 71 |
typeNames[t] = nm |
| 72 |
for _, it := range t.Items { |
| 73 |
nameType(it, append(path, "Item")) |
| 74 |
} |
| 75 |
genTypes = append(genTypes, &newType{ |
| 76 |
name: nm, |
| 77 |
typ: t, |
| 78 |
kind: "and", |
| 79 |
items: t.Items, |
| 80 |
line: t.Line, |
| 81 |
}) |
| 82 |
return nm |
| 83 |
case "literal": |
| 84 |
nm := nameFromPath("Lit", path) |
| 85 |
typeNames[t] = nm |
| 86 |
for _, p := range t.Value.(ParseLiteral).Properties { |
| 87 |
nameType(p.Type, append(path, p.Name)) |
| 88 |
} |
| 89 |
genTypes = append(genTypes, &newType{ |
| 90 |
name: nm, |
| 91 |
typ: t, |
| 92 |
kind: "literal", |
| 93 |
properties: t.Value.(ParseLiteral).Properties, |
| 94 |
line: t.Line, |
| 95 |
}) |
| 96 |
return nm |
| 97 |
case "tuple": |
| 98 |
nm := nameFromPath("Tuple", path) |
| 99 |
typeNames[t] = nm |
| 100 |
for _, it := range t.Items { |
| 101 |
nameType(it, append(path, "Item")) |
| 102 |
} |
| 103 |
genTypes = append(genTypes, &newType{ |
| 104 |
name: nm, |
| 105 |
typ: t, |
| 106 |
kind: "tuple", |
| 107 |
items: t.Items, |
| 108 |
line: t.Line, |
| 109 |
}) |
| 110 |
return nm |
| 111 |
case "or": |
| 112 |
nm := nameFromPath("Or", path) |
| 113 |
typeNames[t] = nm |
| 114 |
for i, it := range t.Items { |
| 115 |
|
| 116 |
nameType(it, append(path, fmt.Sprintf("Item%d", i))) |
| 117 |
} |
| 118 |
|
| 119 |
names := make(map[string]int) |
| 120 |
var msg strings.Builder |
| 121 |
for _, it := range t.Items { |
| 122 |
if line, ok := names[typeNames[it]]; ok { |
| 123 |
|
| 124 |
fmt.Fprintf(&msg, "lines %d %d dup, %s for %s\n", line, it.Line, typeNames[it], nm) |
| 125 |
} |
| 126 |
names[typeNames[it]] = t.Line |
| 127 |
} |
| 128 |
|
| 129 |
if len(names) == 1 { |
| 130 |
var solekey string |
| 131 |
for k := range names { |
| 132 |
solekey = k |
| 133 |
} |
| 134 |
if solekey == "string" { |
| 135 |
typeNames[t] = "string" |
| 136 |
return "string" |
| 137 |
} |
| 138 |
|
| 139 |
log.Printf("unexpected: single-case 'or' type has non-string key %s: %s", nm, solekey) |
| 140 |
log.Fatal(msg.String()) |
| 141 |
} else if len(names) == 2 { |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
newNm := "" |
| 146 |
sawNull := false |
| 147 |
for k := range names { |
| 148 |
if k == "null" { |
| 149 |
sawNull = true |
| 150 |
} else { |
| 151 |
newNm = k |
| 152 |
} |
| 153 |
} |
| 154 |
if sawNull { |
| 155 |
typeNames[t] = newNm |
| 156 |
return newNm |
| 157 |
} |
| 158 |
} |
| 159 |
genTypes = append(genTypes, &newType{ |
| 160 |
name: nm, |
| 161 |
typ: t, |
| 162 |
kind: "or", |
| 163 |
items: t.Items, |
| 164 |
line: t.Line, |
| 165 |
}) |
| 166 |
return nm |
| 167 |
case "stringLiteral": |
| 168 |
typeNames[t] = "string" |
| 169 |
return "string" |
| 170 |
default: |
| 171 |
log.Fatalf("nameType: %T unexpected, line:%d path:%v", t, t.Line, path) |
| 172 |
panic("unreachable in nameType") |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func nameFromPath(prefix string, path []string) string { |
| 177 |
nm := prefix + "_" + strings.Join(path, "_") |
| 178 |
|
| 179 |
nm = strings.ReplaceAll(nm, "/", "_") |
| 180 |
return nm |
| 181 |
} |