diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2025-07-02 17:18:25 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2025-07-02 17:30:30 +0200 |
| commit | 876b3b930dc8c90391082ee5b629f847fbb5d337 (patch) | |
| tree | d4ce8504fdeced6ff9ea802ae777269d05fc3943 /refined/struct.go | |
| parent | e95f01274201b6fde1c1ea7818c7a7e0a77f0f43 (diff) | |
| download | go-gg-refinement-types-3.tar.gz | |
refined: UnmarshalJSON needs a PreScalar type after allrefinement-types-3
Diffstat (limited to 'refined/struct.go')
| -rw-r--r-- | refined/struct.go | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/refined/struct.go b/refined/struct.go index 35a0c85..ee34f0f 100644 --- a/refined/struct.go +++ b/refined/struct.go @@ -3,10 +3,26 @@ package refined -type Struct[P any] struct { - Has P +import "encoding/json" + +type Struct[T any] struct { + Has T +} + +func NewStruct[T any](attributes T) Struct[T] { + return Struct[T]{Has: attributes} } -func NewStruct[P any](payload P) Struct[P] { - return Struct[P]{Has: payload} +func (s Struct[T]) MarshalJSON() ([]byte, error) { + return json.Marshal(s.Has) +} + +func (s *Struct[T]) UnmarshalJSON(buf []byte) error { + err := json.Unmarshal(buf, &s.Has) + if err != nil { + return err + } + + // TODO reflect on the fields of s.Has; if any are refined.Value that are not occupied, attempt to fill the zero value through Refine() + return nil } |
