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 {
4545}
4646
4747type 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"`
5051}
5152
5253type Where struct {
@@ -87,22 +88,27 @@ func (de *If) Format(getFunc func(key string) string) string {
8788 return ""
8889 }
8990
91+ data := ""
92+ if de .Foreach .Data != "" {
93+ data = strings .TrimSpace (de .Foreach .Format (getFunc ))
94+ }
95+ data += strings .TrimSpace (de .Data )
9096 if len (andStrs ) != 0 && len (orStrs ) < 2 {
9197 for _ , v := range andStrs {
9298 ret := Compare (v , getFunc )
9399 if ret != true {
94100 return ""
95101 }
96102 }
97- return strings . TrimSpace ( de . Data )
103+ return data
98104 }
99105
100106 ret := false
101107 if len (orStrs ) != 0 {
102108 for _ , v := range orStrs {
103109 ret = Compare (v , getFunc )
104110 if ret == true {
105- return strings . TrimSpace ( de . Data )
111+ return data
106112 }
107113 }
108114 if ret == false {
Original file line number Diff line number Diff line change @@ -451,3 +451,37 @@ func TestXmlDynamicForeach3(t *testing.T) {
451451
452452 })
453453}
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