From 876b3b930dc8c90391082ee5b629f847fbb5d337 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Wed, 2 Jul 2025 17:18:25 +0200 Subject: refined: UnmarshalJSON needs a PreScalar type after all --- refined/struct.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'refined/struct.go') 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 } -- cgit v1.2.3