-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(source): support xml source (#3)
convert xml to json, and then use json parser
- Loading branch information
1 parent
215dc8d
commit 77cc7f3
Showing
11 changed files
with
224 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<osm version="0.6" generator="CGImap 0.0.2"> | ||
<bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/> | ||
<foo>bar</foo> | ||
</osm> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,27 @@ | ||
package st2 | ||
|
||
type XMLContext struct { | ||
ContentTagPrefix string | ||
AttributeTagPrefix string | ||
} | ||
|
||
// Context struct contains the context running | ||
type Context struct { | ||
Src string | ||
Dst string | ||
Root string | ||
Prefix string | ||
Suffix string | ||
Src string | ||
Dst string | ||
Root string | ||
Prefix string | ||
Suffix string | ||
XMLContext XMLContext | ||
} | ||
|
||
func NewContext(src, dst, root, prefix, suffix string) Context { | ||
func NewContext(src, dst, root, prefix, suffix string, xmlContext XMLContext) Context { | ||
return Context{ | ||
Src: src, | ||
Dst: dst, | ||
Root: normalizeToken(root, ""), | ||
Prefix: normalizeToken(prefix, ""), | ||
Suffix: normalizeToken(suffix, ""), | ||
Src: src, | ||
Dst: dst, | ||
Root: normalizeToken(root, ""), | ||
Prefix: normalizeToken(prefix, ""), | ||
Suffix: normalizeToken(suffix, ""), | ||
XMLContext: xmlContext, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package st2 | ||
|
||
import ( | ||
"bytes" | ||
|
||
xj "github.com/basgys/goxml2json" | ||
) | ||
|
||
type XMLUnmarshalTagFormat struct { | ||
ContentTagPrefix string | ||
AttributeTagPrefix string | ||
} | ||
|
||
func (x XMLUnmarshalTagFormat) Unmarshal(data []byte, v any) error { | ||
reader := bytes.NewReader(data) | ||
root := &xj.Node{} | ||
decoder := xj.NewDecoder(reader) | ||
err := decoder.DecodeWithCustomPrefixes(root, x.ContentTagPrefix, x.AttributeTagPrefix) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
encoder := xj.NewEncoder(buf) | ||
err = encoder.Encode(root) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
json := buf.Bytes() | ||
|
||
return jsonapi.Unmarshal(json, v) | ||
} | ||
|
||
func (x XMLUnmarshalTagFormat) TagFormat() string { | ||
return `xml:"%s"` | ||
} | ||
|
||
func NewXMLParser(ctx Context) *StructuredParser { | ||
return NewStructuredParser(ctx, &XMLUnmarshalTagFormat{ | ||
ContentTagPrefix: ctx.XMLContext.ContentTagPrefix, | ||
AttributeTagPrefix: ctx.XMLContext.AttributeTagPrefix, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package st2 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestXMLUnmarshalTagFormat_Unmarshal(t *testing.T) { | ||
type fields struct { | ||
ContentTagPrefix string | ||
AttributeTagPrefix string | ||
} | ||
type args struct { | ||
data []byte | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
wantErr bool | ||
expectJson string | ||
}{ | ||
{ | ||
name: "succ", | ||
fields: fields{ | ||
ContentTagPrefix: "", | ||
AttributeTagPrefix: ",", | ||
}, | ||
args: args{ | ||
data: []byte(`<?xml version="1.0" encoding="UTF-8"?> | ||
<osm version="0.6" generator="CGImap 0.0.2"> | ||
<bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/> | ||
<foo>bar</foo> | ||
</osm>`), | ||
}, | ||
wantErr: false, | ||
expectJson: `{"osm":{",version":"0.6",",generator":"CGImap 0.0.2","bounds":{",minlon":"12.2487570",",maxlat":"54.0913900",",maxlon":"12.2524800",",minlat":"54.0889580"},"foo":"bar"}}`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
x := XMLUnmarshalTagFormat{ | ||
ContentTagPrefix: tt.fields.ContentTagPrefix, | ||
AttributeTagPrefix: tt.fields.AttributeTagPrefix, | ||
} | ||
var v any | ||
if err := x.Unmarshal(tt.args.data, &v); (err != nil) != tt.wantErr { | ||
t.Errorf("XMLUnmarshalTagFormat.Unmarshal() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
|
||
var expectV any | ||
if err := jsonapi.UnmarshalFromString(tt.expectJson, &expectV); err != nil { | ||
t.Errorf("UnmarshalFromString error = %v", err) | ||
} | ||
|
||
if !reflect.DeepEqual(v, expectV) { | ||
t.Errorf("%+v should equal to %+v", v, expectV) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestXMLUnmarshalTagFormat_TagFormat(t *testing.T) { | ||
type fields struct { | ||
ContentTagPrefix string | ||
AttributeTagPrefix string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want string | ||
}{ | ||
{ | ||
name: "test", | ||
want: `xml:"%s"`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
x := XMLUnmarshalTagFormat{ | ||
ContentTagPrefix: tt.fields.ContentTagPrefix, | ||
AttributeTagPrefix: tt.fields.AttributeTagPrefix, | ||
} | ||
if got := x.TagFormat(); got != tt.want { | ||
t.Errorf("XMLUnmarshalTagFormat.TagFormat() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |