-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.go
More file actions
38 lines (31 loc) · 1001 Bytes
/
form.go
File metadata and controls
38 lines (31 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package hmc
import (
"encoding/xml"
)
// Form is analogous to HTML's <form> which represents a state transition that requires input from the client.
// It describes what data is needed, how it should be submitted, and where
// it should be sent.
//
// Elements should contain a struct representing,
// and elements semantically related to the form,
// which would usually be [Input], [Select], [Map], [Link], etc.
// but might also be something like `Error string` or `Warning string` fields.
type Form[T any] struct {
Method string `json:"method,omitempty"`
Elements T `json:"elements"`
}
func (i Form[T]) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{Local: "c:Form"}
if i.Method != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "method"}, Value: i.Method})
}
err := e.EncodeToken(start)
if err != nil {
return err
}
err = e.Encode(i.Elements)
if err != nil {
return err
}
return e.EncodeToken(start.End())
}