diff options
author | xengineering <me@xengineering.eu> | 2024-11-06 20:45:23 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-11-06 20:45:23 +0100 |
commit | 4c21918d28a01cf8819239dad4869ae49461292e (patch) | |
tree | 689097ebb9ad43d388681503e3a0c873faff7bb6 | |
parent | b8999e29ab99073ccdc1d83c3ec57fd2350684b5 (diff) | |
download | ceres-4c21918d28a01cf8819239dad4869ae49461292e.tar ceres-4c21918d28a01cf8819239dad4869ae49461292e.tar.zst ceres-4c21918d28a01cf8819239dad4869ae49461292e.zip |
REMOVE ME: playground: Add test for Optional implementation
THIS WORKS GREAT!
-rw-r--r-- | playground/main.go | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/playground/main.go b/playground/main.go index 38b5f0d..8c67cc0 100644 --- a/playground/main.go +++ b/playground/main.go @@ -8,9 +8,35 @@ import ( "os" ) +type Optional[T any] struct { + Value T + Exists bool +} + +func (o *Optional[T]) UnmarshalJSON(data []byte) error { + var Temp *T + + err := json.Unmarshal(data, &Temp) + if err != nil { + return err + } + + if Temp == nil { + var empty T + o.Value = empty + o.Exists = false + return nil + } + + o.Value = *Temp + o.Exists = true + return nil +} + type Testy struct { - Flag *bool `json:"flag"` - Cake *[]string `json:"cake"` + Flag *bool `json:"flag"` + Cake *[]string `json:"cake"` + Opt Optional[string] `json:"opt"` } const TEXT = `{ @@ -18,11 +44,13 @@ const TEXT = `{ "one", "two", "three" - ] + ], + "opt": "peter" }` const TEMPLATE = `Flag is: {{if .Flag}}{{.Flag}}{{else}}[none]{{end}} Cake is: {{if .Cake}}{{range .Cake}}{{.}} {{end}}{{else}}[none]{{end}} +Opt is: {{if .Opt.Exists}}{{.Opt.Value}}{{else}}[none]{{end}} ` func main() { |