Skip to content

Commit

Permalink
Merge pull request #4 from mundra-ankur/main
Browse files Browse the repository at this point in the history
support for Marshaling Project object
  • Loading branch information
vifraa committed Oct 15, 2022
2 parents 40b6a78 + 1cee1a4 commit a4fa829
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
22 changes: 22 additions & 0 deletions gopom.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,28 @@ func (p *Properties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err e
return nil
}

// MarshalXML marshals Properties into XML.
func (p Properties) MarshalXML(e *xml.Encoder, start xml.StartElement) error {

tokens := []xml.Token{start}

for key, value := range p.Entries {
t := xml.StartElement{Name: xml.Name{Local: key}}
tokens = append(tokens, t, xml.CharData(value), xml.EndElement{Name: t.Name})
}

tokens = append(tokens, xml.EndElement{Name: start.Name})

for _, t := range tokens {
err := e.EncodeToken(t)
if err != nil {
return err
}
}
// flush to ensure tokens are written
return e.Flush()
}

type Parent struct {
GroupID string `xml:"groupId"`
ArtifactID string `xml:"artifactId"`
Expand Down
23 changes: 23 additions & 0 deletions gopom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,26 @@ func Test_ParsingNotifierConfigurations(t *testing.T) {
assert.Equal(t, "value2", p.Properties.Entries["key2"])
assert.Equal(t, "value3", p.Properties.Entries["key3"])
}

func Test_MarshalingProjectToXML(t *testing.T) {
ignitePlugin := Plugin{
GroupID: "org.apache.ignite",
ArtifactID: "ignite-core",
Version: "2.14.0",
}

// Add plugin to build plugins of original project p and marshal it to XML.
p.Build.Plugins = append(p.Build.Plugins, ignitePlugin)

// Marshal the pom back to xml
marshaledXml, err := xml.MarshalIndent(p, " ", " ")
assert.Nil(t, err)

// Parse the marshaled XML back to a Project object
parsedPom := Project{}
err = xml.Unmarshal(marshaledXml, &parsedPom)
assert.Nil(t, err)

// Check that the two object are equal.
assert.Equal(t, p, parsedPom)
}

0 comments on commit a4fa829

Please sign in to comment.