chore: added serialization for db option type

next-astro
parent e36fddb2b3
commit be8c35229b

@ -5,4 +5,11 @@ export default defineConfig({
publicDir: './client/public', publicDir: './client/public',
srcDir: './client', srcDir: './client',
outDir: './out/client', outDir: './out/client',
vite: {
server: {
proxy: {
'/api': 'http://127.0.0.1:4000',
},
},
},
}) })

@ -206,14 +206,14 @@ function render(g: CanvasRenderingContext2D, state: State, time: number) {
// } // }
// } // }
const [mx, my] = state.mouse // const [mx, my] = state.mouse
g.save() // g.save()
g.fillStyle = '#0008' // g.fillStyle = '#0008'
g.translate(Math.floor(mx / CELL_SIZE), Math.floor(my / CELL_SIZE)) // g.translate(Math.floor(mx / CELL_SIZE), Math.floor(my / CELL_SIZE))
g.beginPath() // g.beginPath()
g.rect(0, 0, 1, 1) // g.rect(0, 0, 1, 1)
g.fill() // g.fill()
g.restore() // g.restore()
} }
setup() setup()

@ -22,6 +22,7 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.46.0 // indirect github.com/valyala/fasthttp v1.46.0 // indirect
github.com/valyala/tcplisten v1.0.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 golang.org/x/sys v0.7.0 // indirect
gotest.tools v2.2.0+incompatible // indirect gotest.tools v2.2.0+incompatible // indirect
) )

@ -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-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-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 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.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.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=

@ -9,9 +9,12 @@ package db
import ( import (
"database/sql" "database/sql"
"encoding/json"
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
"golang.org/x/exp/slices"
) )
// Option è un valore che nel database può potenzialmente essere "NULL" // Option è un valore che nel database può potenzialmente essere "NULL"
@ -33,6 +36,29 @@ type Option[T any] struct {
value T 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] { func NewOption[T any](value T) Option[T] {
return Option[T]{true, value} return Option[T]{true, value}
} }

Loading…
Cancel
Save