Skip to content

Commit 3321026

Browse files
jamietannagpt-4.1
andcommitted
chore: allow unmarshaling the Discussion form template
This is was largely generated through GPT-4.1, with some tweaks to handle the `validations` key. Although my preference would be to use [0] and [1] (via [2]), it unfortunately doesn't handle the complex `if`s. This is a separate commit to isolate LLM-generated code. [0]: https://www.jvt.me/posts/2025/06/05/json-schema-go/ [1]: https://json.schemastore.org/github-issue-forms.json [2]: github/docs#37424 Co-authored-by: gpt-4.1 <github-copilot@jamietanna.co.uk>
1 parent 895dba3 commit 3321026

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

internal/discussionform/main.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package discussionform
2+
3+
import (
4+
"fmt"
5+
"gopkg.in/yaml.v3"
6+
)
7+
8+
type BodyItem struct {
9+
Type string
10+
Item any
11+
Validations map[string]bool
12+
}
13+
14+
func (b *BodyItem) UnmarshalYAML(value *yaml.Node) error {
15+
var typeHolder struct {
16+
Type string `yaml:"type"`
17+
}
18+
if err := value.Decode(&typeHolder); err != nil {
19+
return err
20+
}
21+
b.Type = typeHolder.Type
22+
23+
var validationsHolder struct {
24+
Validations map[string]bool `yaml:"validations"`
25+
}
26+
if err := value.Decode(&validationsHolder); err != nil {
27+
return err
28+
}
29+
b.Validations = validationsHolder.Validations
30+
31+
switch typeHolder.Type {
32+
case "dropdown":
33+
var dropdown Dropdown
34+
if err := value.Decode(&dropdown); err != nil {
35+
return err
36+
}
37+
b.Item = dropdown
38+
case "input":
39+
var input Input
40+
if err := value.Decode(&input); err != nil {
41+
return err
42+
}
43+
b.Item = input
44+
case "textarea":
45+
var textarea Textarea
46+
if err := value.Decode(&textarea); err != nil {
47+
return err
48+
}
49+
b.Item = textarea
50+
default:
51+
return fmt.Errorf("unknown type: %s", typeHolder.Type)
52+
}
53+
return nil
54+
}
55+
56+
type Dropdown struct {
57+
Type string `yaml:"type"`
58+
ID string `yaml:"id"`
59+
Attributes DropdownAttributes `yaml:"attributes"`
60+
}
61+
62+
type DropdownAttributes struct {
63+
Label string `yaml:"label"`
64+
Options []string `yaml:"options"`
65+
}
66+
67+
type Input struct {
68+
Type string `yaml:"type"`
69+
ID string `yaml:"id"`
70+
Attributes InputAttributes `yaml:"attributes"`
71+
}
72+
73+
type InputAttributes struct {
74+
Label string `yaml:"label"`
75+
}
76+
77+
type Textarea struct {
78+
Type string `yaml:"type"`
79+
ID string `yaml:"id"`
80+
Attributes TextareaAttributes `yaml:"attributes"`
81+
}
82+
83+
type TextareaAttributes struct {
84+
Label string `yaml:"label"`
85+
Description string `yaml:"description"`
86+
Value string `yaml:"value"`
87+
}
88+
89+
type Template struct {
90+
Body []BodyItem `yaml:"body"`
91+
}

0 commit comments

Comments
 (0)