From e4bde3d896f41aace6a6c4295afea85a0c404ba5 Mon Sep 17 00:00:00 2001 From: widmogrod Date: Sat, 11 May 2024 20:18:31 +0200 Subject: [PATCH] f: generic union example with JSON serialisation --- f/datas.go | 4 ++-- f/datas_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 f/datas_test.go diff --git a/f/datas.go b/f/datas.go index de331213..509ca02f 100644 --- a/f/datas.go +++ b/f/datas.go @@ -1,8 +1,8 @@ package f -//go:generate go run ../cmd/mkunion +//go:generate go run ../cmd/mkunion --type-registry -//go:tag mkunion:"Either" +//go:tag mkunion:"Either,serde" type ( Left[A, B any] struct{ Value A } Right[A, B any] struct{ Value B } diff --git a/f/datas_test.go b/f/datas_test.go new file mode 100644 index 00000000..c18d1311 --- /dev/null +++ b/f/datas_test.go @@ -0,0 +1,13 @@ +package f + +import ( + "fmt" + "github.com/widmogrod/mkunion/x/shared" +) + +func ExampleEitherToJSON() { + var either Either[int, string] = &Right[int, string]{Value: "hello"} + result, _ := shared.JSONMarshal(either) + fmt.Println(string(result)) + // Output: {"$type":"f.Right","f.Right":{"Value":"hello"}} +}