mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
Everything service has examples now, fixes for client generator (#203)
This commit is contained in:
@@ -157,6 +157,138 @@ func TestTsGen(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeExample(t *testing.T) {
|
||||
spec := &openapi3.Swagger{}
|
||||
err := json.Unmarshal([]byte(timeExample), &spec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(spec.Components.Schemas) == 0 {
|
||||
t.Fatal("boo")
|
||||
}
|
||||
|
||||
fmt.Println(spec.Components.Schemas)
|
||||
res := schemaToGoExample("time", "NowRequest", spec.Components.Schemas, map[string]interface{}{
|
||||
"location": "London",
|
||||
})
|
||||
if strings.TrimSpace(res) != strings.TrimSpace(timeExp) {
|
||||
t.Log(res, timeExp)
|
||||
}
|
||||
|
||||
fmt.Println(spec.Components.Schemas)
|
||||
res = schemaToGoExample("time", "ZoneRequest", spec.Components.Schemas, map[string]interface{}{
|
||||
"location": "London",
|
||||
})
|
||||
if strings.TrimSpace(res) != strings.TrimSpace(timeExp) {
|
||||
t.Log(res, timeExp)
|
||||
}
|
||||
}
|
||||
|
||||
const timeExample = `{
|
||||
"components": {
|
||||
"schemas": {
|
||||
|
||||
"NowRequest": {
|
||||
"description": "Get the current time",
|
||||
"properties": {
|
||||
"location": {
|
||||
"description": "optional location, otherwise returns UTC",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"title": "NowRequest",
|
||||
"type": "object"
|
||||
},
|
||||
"NowResponse": {
|
||||
"properties": {
|
||||
"localtime": {
|
||||
"description": "the current time as HH:MM:SS",
|
||||
"type": "string"
|
||||
},
|
||||
"location": {
|
||||
"description": "the location as Europe/London",
|
||||
"type": "string"
|
||||
},
|
||||
"timestamp": {
|
||||
"description": "timestamp as 2006-01-02T15:04:05.999999999Z07:00",
|
||||
"type": "string"
|
||||
},
|
||||
"timezone": {
|
||||
"description": "the timezone as BST",
|
||||
"type": "string"
|
||||
},
|
||||
"unix": {
|
||||
"description": "the unix timestamp",
|
||||
"format": "int64",
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"title": "NowResponse",
|
||||
"type": "object"
|
||||
},
|
||||
"ZoneRequest": {
|
||||
"description": "Get the timezone info for a specific location",
|
||||
"properties": {
|
||||
"location": {
|
||||
"description": "location to lookup e.g postcode, city, ip address",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"title": "ZoneRequest",
|
||||
"type": "object"
|
||||
},
|
||||
"ZoneResponse": {
|
||||
"properties": {
|
||||
"abbreviation": {
|
||||
"description": "the abbreviated code e.g BST",
|
||||
"type": "string"
|
||||
},
|
||||
"country": {
|
||||
"description": "country of the timezone",
|
||||
"type": "string"
|
||||
},
|
||||
"dst": {
|
||||
"description": "is daylight savings",
|
||||
"type": "boolean"
|
||||
},
|
||||
"latitude": {
|
||||
"description": "e.g 51.42",
|
||||
"format": "double",
|
||||
"type": "number"
|
||||
},
|
||||
"localtime": {
|
||||
"description": "the local time",
|
||||
"type": "string"
|
||||
},
|
||||
"location": {
|
||||
"description": "location requested",
|
||||
"type": "string"
|
||||
},
|
||||
"longitude": {
|
||||
"description": "e.g -0.37",
|
||||
"format": "double",
|
||||
"type": "number"
|
||||
},
|
||||
"region": {
|
||||
"description": "region of timezone",
|
||||
"type": "string"
|
||||
},
|
||||
"timezone": {
|
||||
"description": "the timezone e.g Europe/London",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"title": "ZoneResponse",
|
||||
"type": "object"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
const timeExp = `Location: London,
|
||||
`
|
||||
|
||||
func TestExample(t *testing.T) {
|
||||
|
||||
spec := &openapi3.Swagger{}
|
||||
|
||||
@@ -70,7 +70,14 @@ func main() {
|
||||
return strings.Join(parts[1:len(parts)-1], "") + "Response"
|
||||
},
|
||||
"endpointComment": func(endpoint string, schemas map[string]*openapi3.SchemaRef) string {
|
||||
comm := schemas[strings.Title(endpoint)+"Request"].Value.Description
|
||||
v := schemas[strings.Title(endpoint)+"Request"]
|
||||
if v == nil {
|
||||
panic("can't find " + strings.Title(endpoint) + "Request")
|
||||
}
|
||||
if v.Value == nil {
|
||||
return ""
|
||||
}
|
||||
comm := v.Value.Description
|
||||
ret := ""
|
||||
for _, line := range strings.Split(comm, "\n") {
|
||||
ret += "// " + strings.TrimSpace(line) + "\n"
|
||||
@@ -236,11 +243,15 @@ func main() {
|
||||
}
|
||||
|
||||
exam, err := ioutil.ReadFile(filepath.Join(workDir, serviceName, "examples.json"))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err == nil {
|
||||
m := map[string][]example{}
|
||||
err = json.Unmarshal(exam, &m)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Println(string(exam), err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -565,7 +576,7 @@ func schemaToType(language, serviceName, typeName string, schemas map[string]*op
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
var fieldSeparator, objectOpen, objectClose, arrayPrefix, arrayPostfix, fieldDelimiter, stringType, numberType, boolType string
|
||||
var fieldSeparator, arrayPrefix, arrayPostfix, fieldDelimiter, stringType, numberType, boolType string
|
||||
var int32Type, int64Type, floatType, doubleType, mapType, anyType, typePrefix string
|
||||
var fieldUpperCase bool
|
||||
switch language {
|
||||
@@ -574,8 +585,8 @@ func schemaToType(language, serviceName, typeName string, schemas map[string]*op
|
||||
fieldSeparator = "?: "
|
||||
arrayPrefix = ""
|
||||
arrayPostfix = "[]"
|
||||
objectOpen = "{\n"
|
||||
objectClose = "}"
|
||||
//objectOpen = "{\n"
|
||||
//objectClose = "}"
|
||||
fieldDelimiter = ";"
|
||||
stringType = "string"
|
||||
numberType = "number"
|
||||
@@ -592,8 +603,8 @@ func schemaToType(language, serviceName, typeName string, schemas map[string]*op
|
||||
fieldSeparator = " "
|
||||
arrayPrefix = "[]"
|
||||
arrayPostfix = ""
|
||||
objectOpen = "{"
|
||||
objectClose = "}"
|
||||
//objectOpen = "{"
|
||||
// objectClose = "}"
|
||||
fieldDelimiter = ""
|
||||
stringType = "string"
|
||||
numberType = "int64"
|
||||
@@ -694,7 +705,14 @@ func schemaToType(language, serviceName, typeName string, schemas map[string]*op
|
||||
case "boolean":
|
||||
ret += k + fieldSeparator + arrayPrefix + boolType + arrayPostfix + fieldDelimiter
|
||||
case "object":
|
||||
ret += k + fieldSeparator + arrayPrefix + objectOpen + recurse(v.Value.Items.Value.Properties, level+1) + strings.Repeat(" ", level) + objectClose + arrayPostfix + fieldDelimiter
|
||||
// type is a dynamic map
|
||||
// if additional properties is not present, it's an any type,
|
||||
// like the proto struct type
|
||||
if v.Value.AdditionalProperties != nil {
|
||||
ret += k + fieldSeparator + arrayPrefix + fmt.Sprintf(mapType, valueToType(v.Value.AdditionalProperties)) + arrayPostfix + fieldDelimiter
|
||||
} else {
|
||||
ret += k + fieldSeparator + arrayPrefix + fmt.Sprintf(mapType, anyType) + arrayPostfix + fieldDelimiter
|
||||
}
|
||||
}
|
||||
}
|
||||
case "string":
|
||||
|
||||
Reference in New Issue
Block a user