File tree Expand file tree Collapse file tree 2 files changed +44
-4
lines changed Expand file tree Collapse file tree 2 files changed +44
-4
lines changed Original file line number Diff line number Diff line change @@ -45,8 +45,9 @@ type Include struct {
45
45
}
46
46
47
47
type If struct {
48
- Test string `xml:"test,attr"`
49
- Data string `xml:",chardata"`
48
+ Foreach Foreach `xml:"foreach"`
49
+ Test string `xml:"test,attr"`
50
+ Data string `xml:",chardata"`
50
51
}
51
52
52
53
type Where struct {
@@ -87,22 +88,27 @@ func (de *If) Format(getFunc func(key string) string) string {
87
88
return ""
88
89
}
89
90
91
+ data := ""
92
+ if de .Foreach .Data != "" {
93
+ data = strings .TrimSpace (de .Foreach .Format (getFunc ))
94
+ }
95
+ data += strings .TrimSpace (de .Data )
90
96
if len (andStrs ) != 0 && len (orStrs ) < 2 {
91
97
for _ , v := range andStrs {
92
98
ret := Compare (v , getFunc )
93
99
if ret != true {
94
100
return ""
95
101
}
96
102
}
97
- return strings . TrimSpace ( de . Data )
103
+ return data
98
104
}
99
105
100
106
ret := false
101
107
if len (orStrs ) != 0 {
102
108
for _ , v := range orStrs {
103
109
ret = Compare (v , getFunc )
104
110
if ret == true {
105
- return strings . TrimSpace ( de . Data )
111
+ return data
106
112
}
107
113
}
108
114
if ret == false {
Original file line number Diff line number Diff line change @@ -451,3 +451,37 @@ func TestXmlDynamicForeach3(t *testing.T) {
451
451
452
452
})
453
453
}
454
+
455
+ func TestXmlDynamicIfForeach (t * testing.T ) {
456
+ src := `INSERT INTO TEST_TABLE(id, username, password) VALUES
457
+ <if test="{0} != nil">
458
+ <foreach item="item" index="index" collection="{0}"
459
+ open="" separator="," close="">
460
+ (#{item.testParseStruct.Username}, #{item.testParseStruct.Password})
461
+ </foreach>
462
+ </if>
463
+ `
464
+ logging .SetLevel (logging .DEBUG )
465
+ m , err := xml .ParseDynamic (src , nil )
466
+ if err != nil {
467
+ t .Fatal (err )
468
+ }
469
+ t .Run ("foreach first" , func (t * testing.T ) {
470
+ params := []testParseStruct {
471
+ {
472
+ Username : "user1" ,
473
+ Password : "pw1" ,
474
+ },
475
+ {
476
+ Username : "user2" ,
477
+ Password : "pw2" ,
478
+ },
479
+ }
480
+ ret , err := m .ParseMetadata ("mysql" , params )
481
+ if err != nil {
482
+ t .Fatal (err )
483
+ }
484
+ t .Logf ("arg first : %s\n " , ret )
485
+
486
+ })
487
+ }
You can’t perform that action at this time.
0 commit comments