@@ -27,7 +27,7 @@ function jsonToGo(json, typename)
27
27
} ;
28
28
}
29
29
30
- typename = format ( typename || "GIVE_ME_A_NAME " ) ;
30
+ typename = format ( typename || "AutoGenerated " ) ;
31
31
append ( "type " + typename + " " ) ;
32
32
33
33
parseScope ( scope ) ;
@@ -93,20 +93,24 @@ function jsonToGo(json, typename)
93
93
go += str ;
94
94
}
95
95
96
+ // Sanitizes and formats a string to make an appropriate identifier in Go
96
97
function format ( str )
97
98
{
98
- if ( str . match ( / ^ \d + $ / ) )
99
- str = "Number" + str ;
99
+ if ( ! str )
100
+ return "" ;
101
+ else if ( str . match ( / ^ \d + $ / ) )
102
+ str = "Num" + str ;
100
103
else if ( str . charAt ( 0 ) . match ( / \d / ) )
101
104
{
102
105
var numbers = { '0' : "Zero_" , '1' : "One_" , '2' : "Two_" , '3' : "Three_" ,
103
106
'4' : "Four_" , '5' : "Five_" , '6' : "Six_" , '7' : "Seven_" ,
104
107
'8' : "Eight_" , '9' : "Nine_" } ;
105
108
str = numbers [ str . charAt ( 0 ) ] + str . substr ( 1 ) ;
106
109
}
107
- return toProperCase ( str ) . replace ( / \s | _ | - / g , "" ) ;
110
+ return toProperCase ( str ) . replace ( / [ ^ a - z 0 - 9 ] / ig , "" ) ;
108
111
}
109
112
113
+ // Determines the most appropriate Go type
110
114
function goType ( val )
111
115
{
112
116
if ( val === null )
@@ -140,6 +144,7 @@ function jsonToGo(json, typename)
140
144
}
141
145
}
142
146
147
+ // Given two types, returns the more specific of the two
143
148
function mostSpecificPossibleGoType ( typ1 , typ2 )
144
149
{
145
150
if ( typ1 . substr ( 0 , 5 ) == "float"
@@ -152,6 +157,7 @@ function jsonToGo(json, typename)
152
157
return "interface{}" ;
153
158
}
154
159
160
+ // Proper cases a string according to Go conventions
155
161
function toProperCase ( str )
156
162
{
157
163
// https://github.com/golang/lint/blob/39d15d55e9777df34cdffde4f406ab27fd2e60c0/lint.go#L695-L731
@@ -162,7 +168,7 @@ function jsonToGo(json, typename)
162
168
"URL" , "UTF8" , "VM" , "XML" , "XSRF" , "XSS"
163
169
] ;
164
170
165
- return str . replace ( / ( ^ | [ \s _ - ] ) ( [ a - z ] + ) / g , function ( unused , sep , frag )
171
+ return str . replace ( / ( ^ | [ ^ a - z ] ) ( [ a - z ] + ) / ig , function ( unused , sep , frag )
166
172
{
167
173
if ( commonInitialisms . indexOf ( frag . toUpperCase ( ) ) >= 0 )
168
174
return sep + frag . toUpperCase ( ) ;
0 commit comments