-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtscode.go
More file actions
138 lines (125 loc) · 3.5 KB
/
tscode.go
File metadata and controls
138 lines (125 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package oojson
import (
"bytes"
"fmt"
"sort"
"github.com/iancoleman/strcase"
"golang.org/x/exp/maps"
)
const (
TsBool = "boolean"
TsNumber = "number"
TsAny = "any"
TsString = "string"
)
type TsOption struct {
exportNameFunc ExportNameFunc
imports map[string]struct{}
exportRenames map[string]string
}
func DefaultTsOption() *TsOption {
opt := &TsOption{
imports: make(map[string]struct{}),
}
opt.exportNameFunc = func(name string) string {
if rename, ok := opt.exportRenames[name]; ok {
return rename
}
return strcase.ToLowerCamel(name)
}
return opt
}
// goType returns the Go type of v.
func GetTsType(v *Value, name string, indent string, options *TsOption) (string, string) {
// Determine the number of distinct types observed.
distinctTypes := 0
if v.Arrays > 0 {
distinctTypes++
}
if v.Bools > 0 {
distinctTypes++
}
if v.Float64s > 0 {
distinctTypes++
}
if v.Ints > 0 {
distinctTypes++
}
if v.Nulls > 0 {
distinctTypes++
}
if v.Objects > 0 {
distinctTypes++
}
if v.Strings > 0 {
distinctTypes++
}
subClasses := map[string]string{}
// Based on the observed distinct types, find the most specific Go type.
switch {
case distinctTypes == 1 && v.Arrays > 0:
fallthrough
case distinctTypes == 2 && v.Arrays > 0 && v.Nulls > 0:
elementType, customDefinition := GetTsType(v.ArrayElements, name, indent, options)
if len(customDefinition) > 0 {
subClasses[elementType] = customDefinition
}
return fmt.Sprintf("%v[]", elementType), customDefinition
case distinctTypes == 1 && v.Bools > 0:
return TsBool, ""
case distinctTypes == 2 && v.Bools > 0 && v.Nulls > 0:
return TsBool, ""
case distinctTypes == 1 && v.Float64s > 0:
return TsNumber, ""
case distinctTypes == 2 && v.Float64s > 0 && v.Nulls > 0:
return TsNumber, ""
case distinctTypes == 1 && v.Ints > 0:
return TsNumber, ""
case distinctTypes == 2 && v.Ints > 0 && v.Nulls > 0:
return TsNumber, ""
case distinctTypes == 2 && v.Float64s > 0 && v.Ints > 0:
return TsNumber, ""
case distinctTypes == 3 && v.Float64s > 0 && v.Ints > 0 && v.Nulls > 0:
return TsNumber, ""
case distinctTypes == 1 && v.Objects > 0:
fallthrough
case distinctTypes == 2 && v.Objects > 0 && v.Nulls > 0:
if len(v.ObjectProperties) == 0 {
return TsAny, ""
}
b := &bytes.Buffer{}
properties := maps.Keys(v.ObjectProperties)
sort.Strings(properties)
fmt.Fprintf(b, "type %v = {\n", name)
var unparseableProperties []string
for _, property := range properties {
if isUnparsableProperty(property) {
unparseableProperties = append(unparseableProperties, property)
continue
}
subClassType, customCode := GetTsType(v.ObjectProperties[property], strcase.ToCamel(property), indent, options)
if len(customCode) > 0 {
subClasses[subClassType] = customCode
}
fmt.Fprintf(b, "%v%s: %s;\n", indent, strcase.ToLowerCamel(property), subClassType)
}
fmt.Fprintf(b, "}\n\n")
for _, property := range unparseableProperties {
fmt.Fprintf(b, "// %q cannot be unmarshalled into a struct field by encoding/json.\n", property)
}
for _, code := range subClasses {
fmt.Fprintf(b, "%s\n", code)
}
return name, b.String()
case distinctTypes == 1 && v.Strings > 0 && v.Times == v.Strings:
return TsString, ""
case distinctTypes == 1 && v.Strings > 0:
return TsString, ""
case distinctTypes == 2 && v.Strings > 0 && v.Nulls > 0 && v.Times == v.Strings:
return TsString, ""
case distinctTypes == 2 && v.Strings > 0 && v.Nulls > 0:
return TsString, ""
default:
return TsAny, ""
}
}