Skip to content

Add ParentElement interface #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ linters-settings:
- fieldalignment
- shadow
enable-all: true
ireturn:
allow:
- error
- github.com/twpayne/go-kml/v3.ParentElement
misspell:
locale: US
stylecheck:
Expand Down
7 changes: 6 additions & 1 deletion internal/generate/output.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ func {{ $name }}(children ...Element) *{{ $elementTypeName }} {
}
}

// Append appends children to e.
// Add appends children to e and returns e as a ParentElement.
func (e *{{ $elementTypeName }}) Add(children ...Element) ParentElement {
return e.Append(children...)
}

// Append appends children to e and returns e.
func (e *{{ $elementTypeName }}) Append(children ...Element) *{{ $elementTypeName }} {
e.Children = append(e.Children, children...)
return e
Expand Down
6 changes: 6 additions & 0 deletions kml.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ type Element interface {
xml.Marshaler
}

// A ParentElement is a KML element with children.
type ParentElement interface {
Element
Add(children ...Element) ParentElement
}

// A TopLevelElement is a top level KML element.
type TopLevelElement interface {
Element
Expand Down
105 changes: 90 additions & 15 deletions kml22gx.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions kml22gx.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ func GxSimpleArrayField(name, _type string, children ...Element) *GxSimpleArrayF
}
}

// Add appends children to e and returns e as a ParentElement.
func (e *GxSimpleArrayFieldElement) Add(children ...Element) ParentElement {
return e.Append(children...)
}

// Append appends children to e and returns e.
func (e *GxSimpleArrayFieldElement) Append(children ...Element) *GxSimpleArrayFieldElement {
e.Children = append(e.Children, children...)
return e
}

// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *GxSimpleArrayFieldElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{
Expand Down
14 changes: 7 additions & 7 deletions kml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ func TestSimpleElements(t *testing.T) {
element: kml.OverlayXY(kml.Vec2{X: 0, Y: 0, XUnits: kml.UnitsFraction, YUnits: kml.UnitsFraction}),
expected: `<overlayXY x="0" y="0" xunits="fraction" yunits="fraction"></overlayXY>`,
},
{
name: "Snippet",
element: kml.Snippet("snippet").WithMaxLines(1),
expected: `<Snippet maxLines="1">snippet</Snippet>`,
},
{
name: "value_charData",
element: kml.Value(xml.CharData("<>")),
Expand Down Expand Up @@ -289,10 +294,10 @@ func TestSimpleElements(t *testing.T) {
}
}

func TestCompoundElements(t *testing.T) {
func TestParentElements(t *testing.T) {
for _, tc := range []struct {
name string
element kml.Element
element kml.ParentElement
expected string
}{
{
Expand Down Expand Up @@ -397,11 +402,6 @@ func TestCompoundElements(t *testing.T) {
`</gx:SimpleArrayField>` +
`</Schema>`,
},
{
name: "Snippet",
element: kml.Snippet("snippet").WithMaxLines(1),
expected: `<Snippet maxLines="1">snippet</Snippet>`,
},
{
name: "gx:Wait",
element: kml.GxWait(
Expand Down
Loading
Loading