Skip to content

Commit

Permalink
Fix new float schema and improve its test (#317)
Browse files Browse the repository at this point in the history
* Fix using string to parse and serialize floats

* Handle entity history parsing and improve test
  • Loading branch information
JonoPrest authored Nov 2, 2024
1 parent c7b5c22 commit 62f77a5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
24 changes: 11 additions & 13 deletions codegenerator/cli/templates/static/codegen/src/GqlDbCustomTypes.res
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
// In postgres floats are stored as numeric, which get returned to us as strtings. So the decoder / encoder need to do that for us.
module Float = {
@genType
type t = float

let schema =
S.string
->S.setName("GqlDbCustomTypes.Float")
->S.transform(s => {
parser: string => {
switch string->Belt.Float.fromString {
| Some(db) => db
| None => s.fail("The string is not valid GqlDbCustomTypes.Float")
}
},
serializer: float => float->Js.Float.toString,
})
external fromStringUnsafe: string => float = "Number"

let schema = S.union([
S.float,
//This is needed to parse entity history json fields
S.string->S.transform(_s => {
parser: string => string->fromStringUnsafe,
serializer: Utils.magic,
}),
])->S.setName("GqlDbCustomTypes.Float")
}

// Schema allows parsing strings or numbers to ints
Expand All @@ -28,6 +25,7 @@ module Int = {

let schema = S.union([
S.int,
//This is needed to parse entity history json fields
S.string->S.transform(_s => {
parser: string => string->fromStringUnsafe,
serializer: Utils.magic,
Expand Down
35 changes: 27 additions & 8 deletions scenarios/test_codegen/test/SerDe_Test.res
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,25 @@ describe("SerDe Test", () => {
// arrayOfTimestamps: [],
}

//Fails if serialziation does not work
let set = DbFunctionsEntities.batchSet(~entityMod=module(Entities.EntityWithAllTypes))

//Fails if parsing does not work
let read = DbFunctionsEntities.batchRead(~entityMod=module(Entities.EntityWithAllTypes))
//set the entity
await DbFunctions.sql->set([entity])
switch await DbFunctions.sql->set([entity]) {
| exception exn =>
Js.log(exn)
Assert.fail("Failed to set entity in table")
| _ => ()
}

switch await DbFunctions.sql->read([entity.id]) {
| exception exn =>
Js.log(exn)
Assert.fail("Failed to read entity from table")
| [_entity] => ()
| _ => Assert.fail("Should have returned a row on batch read fn")
}

//The copy function will do it's custom postgres serialization of the entity
await DbFunctions.sql->DbFunctions.EntityHistory.copyAllEntitiesToEntityHistory
Expand All @@ -54,12 +69,16 @@ describe("SerDe Test", () => {
switch res {
| [row] =>
let json = row["params"]
let parsed = json->S.parseOrRaiseWith(Entities.EntityWithAllTypes.schema)
Assert.deepEqual(
parsed,
entity,
~message="Postgres json serialization should be compatable with our schema",
)
let parsed = json->S.parseWith(Entities.EntityWithAllTypes.schema)
switch parsed {
| Ok(parsed) =>
Assert.deepEqual(
parsed,
entity,
~message="Postgres json serialization should be compatable with our schema",
)
| Error(e) => Assert.fail("Failed to parse entity history: " ++ e->S.Error.reason)
}
| _ => Assert.fail("Should have returned a row")
}
})
Expand Down

0 comments on commit 62f77a5

Please sign in to comment.