From be8c35229bb838c93c887ff406408a265d90ac73 Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Tue, 13 Jun 2023 23:50:20 +0200 Subject: [PATCH] chore: added serialization for db option type --- astro.config.js | 7 +++++++ client/scripts/circuits-art.ts | 16 ++++++++-------- go.mod | 1 + go.sum | 2 ++ libs/db/db.go | 26 ++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/astro.config.js b/astro.config.js index e2323ab..a6a0a91 100644 --- a/astro.config.js +++ b/astro.config.js @@ -5,4 +5,11 @@ export default defineConfig({ publicDir: './client/public', srcDir: './client', outDir: './out/client', + vite: { + server: { + proxy: { + '/api': 'http://127.0.0.1:4000', + }, + }, + }, }) diff --git a/client/scripts/circuits-art.ts b/client/scripts/circuits-art.ts index f5b291e..5b4e8c6 100644 --- a/client/scripts/circuits-art.ts +++ b/client/scripts/circuits-art.ts @@ -206,14 +206,14 @@ function render(g: CanvasRenderingContext2D, state: State, time: number) { // } // } - const [mx, my] = state.mouse - g.save() - g.fillStyle = '#0008' - g.translate(Math.floor(mx / CELL_SIZE), Math.floor(my / CELL_SIZE)) - g.beginPath() - g.rect(0, 0, 1, 1) - g.fill() - g.restore() + // const [mx, my] = state.mouse + // g.save() + // g.fillStyle = '#0008' + // g.translate(Math.floor(mx / CELL_SIZE), Math.floor(my / CELL_SIZE)) + // g.beginPath() + // g.rect(0, 0, 1, 1) + // g.fill() + // g.restore() } setup() diff --git a/go.mod b/go.mod index 279a78c..bbb51e5 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.46.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect golang.org/x/sys v0.7.0 // indirect gotest.tools v2.2.0+incompatible // indirect ) diff --git a/go.sum b/go.sum index 0ace2b0..0761226 100644 --- a/go.sum +++ b/go.sum @@ -47,6 +47,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= diff --git a/libs/db/db.go b/libs/db/db.go index 88a02da..df6090a 100644 --- a/libs/db/db.go +++ b/libs/db/db.go @@ -9,9 +9,12 @@ package db import ( "database/sql" + "encoding/json" "fmt" "reflect" "strings" + + "golang.org/x/exp/slices" ) // Option è un valore che nel database può potenzialmente essere "NULL" @@ -33,6 +36,29 @@ type Option[T any] struct { value T } +func (o Option[T]) MarshalJSON() ([]byte, error) { + if !o.present { + return []byte("null"), nil + } + + return json.Marshal(o.value) +} + +func (o Option[T]) UnmarshalJSON(v []byte) error { + if slices.Equal(v, []byte("null")) { + var zero T + o.present = false + o.value = zero + return nil + } + + if err := json.Unmarshal(v, &o.value); err != nil { + return err + } + + return nil +} + func NewOption[T any](value T) Option[T] { return Option[T]{true, value} }