-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype.go
147 lines (124 loc) · 2.9 KB
/
type.go
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
139
140
141
142
143
144
145
146
147
package main
import (
"encoding/json"
"fmt"
"strings"
)
type Type struct {
Names []string
Description string
Package string
Wrapper string
}
func (t *Type) UnmarshalJSON(buf []byte) error {
//Type could be eith string type or []string type
//specific Unmarshall to cope with it :/
if string(buf) != "\"\"" {
tmp := map[string]interface{}{}
if err := json.Unmarshal(buf, &tmp); err != nil {
return err
}
//fmt.Println(tmp)
returnType, exists := tmp["type"]
if !exists {
returnType, exists = tmp["names"]
}
if !exists {
return fmt.Errorf("missing names field in map")
}
switch returnType.(type) {
case string:
t.Names = make([]string, 1, 1)
t.Names[0] = returnType.(string)
default:
typeArray := returnType.([]interface{})
length := len(typeArray)
t.Names = make([]string, length, length)
for i := 0; i < length; i++ {
t.Names[i] = typeArray[i].(string)
}
}
returnDescription, exists := tmp["description"]
if exists {
t.Description = returnDescription.(string)
}
}
return nil
}
func (t *Type) GetType() string {
if len(t.Names) == 1 {
return GoType(t.Names[0], t.Package)
}
return "interface{}"
}
func (t *Type) GetTypeOptionalPointer() string {
if !t.IsVoid() && !t.IsAnyType() && !t.IsNativeType() && !t.IsArray() && !t.IsCallback() && !t.HasWrapper() && !t.IsAnyParameters() {
return "*" + t.GetType()
}
return t.GetType()
}
func (t *Type) IsVoid() bool {
return len(t.Names) == 0 || (len(t.Names) == 1 && t.Names[0] == "")
}
func (t *Type) IsCallback() bool {
return t.GetType() == "func(...interface{})"
}
func (t *Type) IsAnyParameters() bool {
return t.GetType() == "...interface{}"
}
func (t *Type) IsAnyType() bool {
return t.GetType() == "interface{}"
}
func (t *Type) GetNativeType() string {
if len(t.Names) == 1 {
return GoNativeType(t.Names[0])
}
return ""
}
func (t *Type) IsNativeType() bool {
return t.GetNativeType() != ""
}
func (t *Type) IsGenericArray() bool {
return t.GetType() == "[]interface{}"
}
func (t *Type) IsArray() bool {
return strings.HasPrefix(t.GetType(), "[]")
}
func (t *Type) GetTypeInArray() string {
if len(t.Names) == 1 {
return GoTypeInArray(t.Names[0], t.Package)
}
return "interface{}"
}
func (t *Type) GetNativeTypeInArray() string {
if len(t.Names) == 1 {
return GoTypeNativeInArray(t.Names[0], t.Package)
}
return ""
}
func (t *Type) IsNativeTypeInArray() bool {
return t.GetNativeTypeInArray() != ""
}
func (t *Type) GetGopherjsCallName() string {
if t.IsNativeType() {
native := t.GetNativeType()
if native == "float64" {
return "Float"
}
return UpperInitial(native)
}
return ""
}
func (t *Type) GetGopherjsCallNameInArray() string {
if t.IsNativeTypeInArray() {
native := t.GetNativeTypeInArray()
if native == "float64" {
return "Float"
}
return UpperInitial(native)
}
return ""
}
func (t *Type) HasWrapper() bool {
return t.Wrapper != ""
}