-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement xml Marshaler interface for event/reading tags
Signed-off-by: Chris Hung <[email protected]>
- Loading branch information
Chris Hung
committed
Jan 18, 2023
1 parent
2b7b082
commit 809b610
Showing
4 changed files
with
92 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// Copyright (C) 2023 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dtos | ||
|
||
import "encoding/xml" | ||
|
||
type Tags map[string]any | ||
|
||
// MarshalXML fulfills the Marshaler interface for Tags field, which is being ignored | ||
// from XML Marshaling since maps are not supported. We have to provide our own | ||
// marshaling of the Tags field if it is non-empty. | ||
func (t Tags) MarshalXML(e *xml.Encoder, start xml.StartElement) error { | ||
if len(t) == 0 { | ||
return nil | ||
} | ||
|
||
err := e.EncodeToken(start) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for k, v := range t { | ||
xmlMapEntry := struct { | ||
XMLName xml.Name | ||
Value any `xml:",chardata"` | ||
}{ | ||
XMLName: xml.Name{Local: k}, | ||
Value: v, | ||
} | ||
|
||
err = e.Encode(xmlMapEntry) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return e.EncodeToken(start.End()) | ||
} |
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,35 @@ | ||
// | ||
// Copyright (C) 2023 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dtos | ||
|
||
import ( | ||
"encoding/xml" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTags_MarshalXML(t *testing.T) { | ||
testTags := Tags{ | ||
"String": "value", | ||
"Numeric": 123, | ||
"Bool": false, | ||
} | ||
|
||
xml, err := xml.Marshal(testTags) | ||
require.NoError(t, err) | ||
|
||
contains := []string{ | ||
"<String>value</String>", | ||
"<Numeric>123</Numeric>", | ||
"<Bool>false</Bool>"} | ||
|
||
for _, v := range contains { | ||
ok := strings.Contains(string(xml), v) | ||
require.True(t, ok) | ||
} | ||
} |