Skip to content

Commit 41b7a7f

Browse files
committed
feat: replace xml tag name
1 parent 3fd0228 commit 41b7a7f

File tree

5 files changed

+88
-10
lines changed

5 files changed

+88
-10
lines changed

examples/docs/apis-demo.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,25 @@ __Response__:
173173
</response>
174174
```
175175

176+
```javascript
177+
//StatusCode: 200
178+
<response> //object(common.Response), 通用返回结果
179+
<code>0</code> //int, 返回状态码
180+
<data> //object(handler.DemoXMLResponse2), XML测试返回对象2
181+
<address>abc</address> //string, 地址信息
182+
<city_id>123</city_id> //int64, 城市ID
183+
<id>123</id> //int64, 地址ID
184+
</data>
185+
<msg>success</msg> //string, 返回消息
186+
</response>
187+
```
188+
189+
```javascript
190+
//StatusCode: 200
191+
<response> //object(common.Response), 通用返回结果
192+
<code>10010</code> //int, 返回状态码
193+
<msg>sme error</msg> //string, 返回消息
194+
</response>
195+
```
196+
176197
---

examples/svc-greeter/main.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,24 @@ type Struct2 struct {
7575

7676
type XMLResponse struct {
7777
XMLName xml.Name `xml:"response"`
78-
Attr int `json:"attr" xml:"attr,attr"` //返回状态码
79-
Code int `json:"code" xml:"code"` //返回状态码
80-
Msg string `json:"msg" xml:"msg"` //返回消息
81-
Data interface{} `json:"data,omitempty" xml:"data,omitempty"` //返回具体数据
78+
Attr int `json:"attr" xml:"attr,attr"` //返回状态码
79+
Code int `json:"code" xml:"code"` //返回状态码
80+
Msg string `json:"msg" xml:"msg"` //返回消息
81+
Data interface{} //`json:"data,omitempty" xml:"data,omitempty"` //返回具体数据
8282
// InnerText string `xml:",innerxml"`
8383
// Arr []string `xml:"arr"`
8484
} //通用返回结果
8585

86+
type XMLData struct {
87+
XMLName xml.Name `xml:"mydata"`
88+
Title string
89+
Desc string
90+
}
91+
type XMLData2 struct {
92+
Title string
93+
Desc string
94+
}
95+
8696
//@title 测试greeter
8797
//@api GET /greeter
8898
//@group greeter
@@ -91,7 +101,8 @@ type XMLResponse struct {
91101
//@request Request
92102
//@response1 200 TestData "输出对象"
93103
//@response1 200 Struct1 "struct1"
94-
//@response 200 Response{data=TestData} "输出对象 dd"
104+
//@response 200 XMLResponse{data=[]XMLData} "输出xml"
105+
//@response 200 XMLResponse{data=[]XMLData2} "输出xml2"
95106
//@response1 200 common.Response{data=TestData} "输出对象 dd"
96107
//@response1 500 Response{code=10010,msg="异常"} "出错了"
97108
//@response1 500 int 错误

examples/svc-user/handler/demo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ type DemoXMLResponse struct {
9191
CityID int64 `xml:"city_id"` //城市ID
9292
Address string `xml:"address"` //地址信息
9393
} //XML测试返回对象
94+
type DemoXMLResponse2 struct {
95+
ID int64 `xml:"id"` //地址ID
96+
CityID int64 `xml:"city_id"` //城市ID
97+
Address string `xml:"address"` //地址信息
98+
} //XML测试返回对象2
9499

95100
//@api GET /demo/xml
96101
//@title XML测试
@@ -99,6 +104,8 @@ type DemoXMLResponse struct {
99104
//@request DemoXMLRequest
100105
//@format xml
101106
//@response 200 common.Response{code=0,msg="success",data=DemoXMLResponse}
107+
//@response 200 common.Response{code=0,msg="success",data=DemoXMLResponse2}
108+
//@response 200 common.Response{code=10010,msg="sme error"}
102109
//@author alovn
103110
func (h *AddressHandler) XML(w http.ResponseWriter, r *http.Request) {
104111
address := DemoXMLRequest{}

operation.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ func (operation *Operation) parseCombinedObject(refType string, astFile *ast.Fil
285285
return nil, err
286286
}
287287
key := strings.ToLower(keyVal[0])
288+
if old, ok := schemaA.Properties[key]; ok { //xml tag replace
289+
xmlTag, hasTag, isAttr, _, isInner := old.XMLTag()
290+
if _, has2 := schema.hasXMLName(); !has2 {
291+
if hasTag && !isAttr && !isInner {
292+
schema.xmlName = xmlTag
293+
} else {
294+
schema.xmlName = old.Name
295+
}
296+
}
297+
}
288298
if isArray {
289299
arrSchema := &TypeSchema{
290300
Name: key,

spec.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type ApiResponseSpec struct {
7777
Format string //json xml
7878
Schema *TypeSchema
7979
Description string
80+
body string //cache
8081
}
8182

8283
type TypeSchema struct {
@@ -93,6 +94,7 @@ type TypeSchema struct {
9394
typeSpecDef *TypeSpecDef
9495
example string //example value
9596
parameterTags map[string]string
97+
xmlName string
9698
}
9799

98100
func (s *ApiRequestSpec) Body() string {
@@ -110,7 +112,11 @@ func (s *ApiResponseSpec) Body() string {
110112
if s.Schema == nil {
111113
return ""
112114
}
113-
return s.Schema.Write(s.Format)
115+
if s.body != "" {
116+
return s.body
117+
}
118+
s.body = s.Schema.Write(s.Format)
119+
return s.body
114120
}
115121

116122
func (s *TypeSchema) Write(format string) string {
@@ -249,7 +255,7 @@ func (s *TypeSchema) parseXML(depth int, sb *strings.Builder, isNewLine bool) {
249255
if v.Name == "XMLName" && v.FullName == "xml.Name" { //ignore xmlname
250256
continue
251257
}
252-
key, isAttr, isOmitempty, isInner := v.XMLTag()
258+
key, _, isAttr, isOmitempty, isInner := v.XMLTag()
253259
if isAttr { //ignore attr
254260
continue
255261
}
@@ -407,7 +413,29 @@ func (v *TypeSchema) ValidateTag() (validate string) {
407413
return
408414
}
409415

416+
func (v *TypeSchema) hasXMLName() (xmlName string, has bool) {
417+
if v.xmlName != "" {
418+
return v.xmlName, true
419+
}
420+
if v.Properties != nil {
421+
if x, ok := v.Properties["xmlname"]; ok {
422+
if x.Name == "XMLName" && x.FullName == "xml.Name" {
423+
val, has2 := x.GetTag("xml")
424+
if has2 {
425+
xmlName = strings.Split(val, ",")[0]
426+
has = true
427+
return
428+
}
429+
}
430+
}
431+
}
432+
return "", false
433+
}
434+
410435
func (v *TypeSchema) XMLName() string {
436+
if v.xmlName != "" {
437+
return v.xmlName
438+
}
411439
if v.Properties != nil {
412440
if x, ok := v.Properties["xmlname"]; ok {
413441
if x.Name == "XMLName" && x.FullName == "xml.Name" {
@@ -428,7 +456,7 @@ func (v *TypeSchema) XMLAttrs() map[string]string {
428456
if schema.Name == "XMLName" && schema.FullName == "xml.Name" {
429457
continue
430458
}
431-
xmlTag, isAttr, _, _ := schema.XMLTag()
459+
xmlTag, _, isAttr, _, _ := schema.XMLTag()
432460
if isAttr {
433461
m[xmlTag] = schema.ExampleValue()
434462
}
@@ -438,12 +466,13 @@ func (v *TypeSchema) XMLAttrs() map[string]string {
438466
return nil
439467
}
440468

441-
func (v *TypeSchema) XMLTag() (xmlTag string, isAttr, isOmitempty, isInner bool) {
469+
func (v *TypeSchema) XMLTag() (xmlTag string, hasTag, isAttr, isOmitempty, isInner bool) {
442470
val, has := v.GetTag("xml")
443471
if has {
444472
arr := strings.Split(val, ",")
445473
if len(arr) > 0 {
446474
xmlTag = arr[0]
475+
hasTag = xmlTag != "" && xmlTag != "-"
447476
for i, a := range arr {
448477
if i == 0 {
449478
continue
@@ -460,7 +489,7 @@ func (v *TypeSchema) XMLTag() (xmlTag string, isAttr, isOmitempty, isInner bool)
460489
return
461490
}
462491
}
463-
return v.Name, false, false, false
492+
return v.Name, false, false, false, false
464493
}
465494

466495
func getFieldExample(typeName string, field *ast.Field) string {

0 commit comments

Comments
 (0)