Skip to content

Commit 80e49d7

Browse files
committed
Add ParentElement interface
1 parent bd760a3 commit 80e49d7

File tree

8 files changed

+448
-77
lines changed

8 files changed

+448
-77
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ linters-settings:
126126
- fieldalignment
127127
- shadow
128128
enable-all: true
129+
ireturn:
130+
allow:
131+
- error
132+
- github.com/twpayne/go-kml/v3.ParentElement
129133
misspell:
130134
locale: US
131135
stylecheck:

internal/generate/output.go.tmpl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ func {{ $name }}(children ...Element) *{{ $elementTypeName }} {
5858
}
5959
}
6060

61-
// Append appends children to e.
61+
// Add adds children to e and returns e as a ParentElement.
62+
func (e *{{ $elementTypeName }}) Add(children ...Element) ParentElement {
63+
return e.Append(children...)
64+
}
65+
66+
// Append appends children to e and returns e.
6267
func (e *{{ $elementTypeName }}) Append(children ...Element) *{{ $elementTypeName }} {
6368
e.Children = append(e.Children, children...)
6469
return e

kml.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ type Element interface {
3838
xml.Marshaler
3939
}
4040

41+
// A ParentElement is a KML element with children.
42+
type ParentElement interface {
43+
Element
44+
Add(children ...Element) ParentElement
45+
}
46+
4147
// A TopLevelElement is a top level KML element.
4248
type TopLevelElement interface {
4349
Element

kml22gx.gen.go

Lines changed: 90 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kml22gx.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ func GxSimpleArrayField(name, _type string, children ...Element) *GxSimpleArrayF
152152
}
153153
}
154154

155+
// Add adds children to e and returns e as a ParentElement.
156+
func (e *GxSimpleArrayFieldElement) Add(children ...Element) ParentElement {
157+
return e.Append(children...)
158+
}
159+
160+
// Append appends children to e and returns e.
161+
func (e *GxSimpleArrayFieldElement) Append(children ...Element) *GxSimpleArrayFieldElement {
162+
e.Children = append(e.Children, children...)
163+
return e
164+
}
165+
155166
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
156167
func (e *GxSimpleArrayFieldElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
157168
startElement := xml.StartElement{

kml_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ func TestSimpleElements(t *testing.T) {
164164
element: kml.OverlayXY(kml.Vec2{X: 0, Y: 0, XUnits: kml.UnitsFraction, YUnits: kml.UnitsFraction}),
165165
expected: `<overlayXY x="0" y="0" xunits="fraction" yunits="fraction"></overlayXY>`,
166166
},
167+
{
168+
name: "Snippet",
169+
element: kml.Snippet("snippet").WithMaxLines(1),
170+
expected: `<Snippet maxLines="1">snippet</Snippet>`,
171+
},
167172
{
168173
name: "value_charData",
169174
element: kml.Value(xml.CharData("<>")),
@@ -289,10 +294,10 @@ func TestSimpleElements(t *testing.T) {
289294
}
290295
}
291296

292-
func TestCompoundElements(t *testing.T) {
297+
func TestParentElements(t *testing.T) {
293298
for _, tc := range []struct {
294299
name string
295-
element kml.Element
300+
element kml.ParentElement
296301
expected string
297302
}{
298303
{
@@ -397,11 +402,6 @@ func TestCompoundElements(t *testing.T) {
397402
`</gx:SimpleArrayField>` +
398403
`</Schema>`,
399404
},
400-
{
401-
name: "Snippet",
402-
element: kml.Snippet("snippet").WithMaxLines(1),
403-
expected: `<Snippet maxLines="1">snippet</Snippet>`,
404-
},
405405
{
406406
name: "gx:Wait",
407407
element: kml.GxWait(

0 commit comments

Comments
 (0)