| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/dave/jennifer/jen" |
| 9 |
) |
| 10 |
|
| 11 |
type keyInfo struct { |
| 12 |
Type jen.Code |
| 13 |
Comment string |
| 14 |
NoJSONTag bool |
| 15 |
Embedded bool |
| 16 |
OmitEmpty bool |
| 17 |
} |
| 18 |
|
| 19 |
func parseJenKeysAsMap(lines map[string]keyInfo) (map[string]interface{}, error) { |
| 20 |
// e.g. keeps a reference from a.b to the corresponding struct |
| 21 |
mapReferences := map[string]map[string]interface{}{} |
| 22 |
|
| 23 |
for _, line := range sortedKeys(lines) { |
| 24 |
typ3 := lines[line].Type |
| 25 |
|
| 26 |
// prepend $. so the following loop always runs even for parts |
| 27 |
// with no dots, and replace [] as .* to easily treat slices as |
| 28 |
// maps for now |
| 29 |
lineMod := "$." + strings.ReplaceAll(line, "[]", ".*") |
| 30 |
|
| 31 |
parts := strings.Split(lineMod, ".") |
| 32 |
|
| 33 |
var m map[string]interface{} |
| 34 |
var ok bool |
| 35 |
|
| 36 |
for i, part := range parts[:len(parts)-1] { |
| 37 |
key := strings.Join(parts[0:i+1], ".") |
| 38 |
parentKey := strings.Join(parts[0:i], ".") |
| 39 |
|
| 40 |
if val, ok := mapReferences[parentKey][part].(*jen.Statement); ok { |
| 41 |
// return nil, fmt.Errorf("1: tried to parse '%s' as '%T', but it already terminated at '%#v'", key, m, val) |
| 42 |
|
| 43 |
fmt.Fprintf(os.Stderr, "! Key %q already terminated at %T, but %q implies it's a map, so that's what we'll do\n", key, val, line) |
| 44 |
} |
| 45 |
|
| 46 |
if m, ok = mapReferences[key]; !ok { |
| 47 |
m = map[string]interface{}{} |
| 48 |
mapReferences[key] = m |
| 49 |
} |
| 50 |
|
| 51 |
if parentKey != "" { |
| 52 |
mapReferences[parentKey][part] = m |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
lastPart := parts[len(parts)-1] |
| 57 |
|
| 58 |
if val, ok := m[lastPart]; ok { |
| 59 |
return nil, fmt.Errorf("2: wanted to terminate '%s' at '%#v', but it was already parsed as '%#T'", line, typ3, val) |
| 60 |
} |
| 61 |
|
| 62 |
m[lastPart] = lines[line] |
| 63 |
} |
| 64 |
|
| 65 |
final := map[string]interface{}{} |
| 66 |
for k, v := range mapReferences["$"] { |
| 67 |
if !strings.Contains(k, ".") { |
| 68 |
final[k] = v |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return final, nil |
| 73 |
} |
| 74 |
|
| 75 |
func parseJenKeysAsStruct(name string, lines map[string]keyInfo) (*jen.Statement, error) { |
| 76 |
m, err := parseJenKeysAsMap(lines) |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
|
| 81 |
// mutually recrusive recursive with traverseStruct |
| 82 |
var traverse func(data interface{}, g *jen.Group, parent string) |
| 83 |
|
| 84 |
traverseStruct := func(s *jen.Statement, name string, t map[string]interface{}) { |
| 85 |
s.Id(name).StructFunc(func(subg *jen.Group) { |
| 86 |
for _, k := range sortedKeys(t) { |
| 87 |
v := t[k] |
| 88 |
traverse(v, subg, k) |
| 89 |
} |
| 90 |
}).Line() |
| 91 |
} |
| 92 |
|
| 93 |
// keep track of any anonymous structs as we'll want to make them |
| 94 |
// siblings with the original parent struct |
| 95 |
anonymousStructs := []*jen.Statement{} |
| 96 |
|
| 97 |
traverse = func(data interface{}, g *jen.Group, parent string) { |
| 98 |
var idType jen.Code |
| 99 |
id := pascal(parent) |
| 100 |
tag := strings.TrimSuffix(parent, "[]") |
| 101 |
|
| 102 |
switch t := data.(type) { |
| 103 |
case map[string]interface{}: |
| 104 |
// if there's an * in the keys, the parent key we're on |
| 105 |
// must have been a slice, so use "[]" as the marker, |
| 106 |
// and redo the recursion |
| 107 |
for _, k := range sortedKeys(t) { |
| 108 |
v := t[k] |
| 109 |
if k == "*" { |
| 110 |
traverse(v, g, parent+"[]") |
| 111 |
return |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// Since the type to our nested struct will always be |
| 116 |
// a pointer, it'll be omitted during encoding if that |
| 117 |
// value is ever nil, which seems always like the |
| 118 |
// preferred behavior. 🤷 |
| 119 |
tag += ",omitempty" |
| 120 |
|
| 121 |
idType = jen.Op("*").Id(id) // is a nested anon struct, so use a pointer to it itself as the type |
| 122 |
idDeplural := id |
| 123 |
|
| 124 |
if strings.HasSuffix(id, "[]") { |
| 125 |
id = strings.TrimSuffix(id, "[]") |
| 126 |
idDeplural = id |
| 127 |
|
| 128 |
// try to depluralize lol |
| 129 |
// TODO add more exhaustive rules as necessary |
| 130 |
// because this isn't really robust |
| 131 |
if strings.HasSuffix(id, "s") { |
| 132 |
fmt.Printf(" ! %s is a slice and looks plural, we'll try to depluralize...\n", id) |
| 133 |
idDeplural = strings.TrimSuffix(id, "s") |
| 134 |
} |
| 135 |
|
| 136 |
idType = jen.Index().Op("*").Id(idDeplural) |
| 137 |
} |
| 138 |
|
| 139 |
s := jen.Empty() |
| 140 |
traverseStruct(s, idDeplural, t) |
| 141 |
anonymousStructs = append(anonymousStructs, s) |
| 142 |
case keyInfo: |
| 143 |
idType = t.Type |
| 144 |
|
| 145 |
if t.Comment != "" { |
| 146 |
g.Comment(t.Comment) |
| 147 |
} |
| 148 |
if t.Embedded { |
| 149 |
id = "" |
| 150 |
t.NoJSONTag = true |
| 151 |
} |
| 152 |
if t.OmitEmpty { |
| 153 |
tag += ",omitempty" |
| 154 |
} |
| 155 |
if t.NoJSONTag { |
| 156 |
tag = "" |
| 157 |
} |
| 158 |
default: |
| 159 |
panic("unhandled case idk") |
| 160 |
} |
| 161 |
|
| 162 |
g.Id(id).Add(idType).Do(func(s *jen.Statement) { |
| 163 |
if tag == "" { |
| 164 |
return |
| 165 |
} |
| 166 |
s.Tag(map[string]string{"json": tag}) |
| 167 |
}).Line() |
| 168 |
} |
| 169 |
|
| 170 |
s := jen.Type() |
| 171 |
traverseStruct(s, name, m) |
| 172 |
for _, q := range anonymousStructs { |
| 173 |
s.Line().Type().Add(q).Line() |
| 174 |
} |
| 175 |
return s, nil |
| 176 |
} |
| 177 |
|
| 178 |
func pascal(text string) string { |
| 179 |
nodash := strings.ReplaceAll(text, "-", " ") |
| 180 |
noundies := strings.ReplaceAll(nodash, "_", " ") |
| 181 |
titled := strings.Title(noundies) |
| 182 |
return strings.ReplaceAll(titled, " ", "") |
| 183 |
} |