Skip to content

Commit

Permalink
refactor: use serde for json de/serialization instead of utils/json (#…
Browse files Browse the repository at this point in the history
…704)

* json > nim-serde bump

Should wait until serde is integrated into nim-ethers before making these changes as there will be less import exceptions required.

* bump nim-serde

* change func to proc due to chronicles side effects

* import serde into utils/json, use as proxy

import nim-serde into utils/json and use utils/json as a proxy for serde functions, including overloading `%` and `fromJson` for application types.

* update tests to use serde

* bump serde to latest

* remove testjson -- no longer needed

* bump serde in nimble

* updates to reconcile rebase with master
  • Loading branch information
emizzle authored Mar 19, 2024
1 parent b4de53f commit f567f4e
Show file tree
Hide file tree
Showing 20 changed files with 43 additions and 701 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,6 @@
url = https://github.com/codex-storage/codex-storage-proofs-circuits.git
ignore = untracked
branch = master
[submodule "vendor/nim-serde"]
path = vendor/nim-serde
url = https://github.com/codex-storage/nim-serde.git
1 change: 1 addition & 0 deletions codex.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ requires "presto"
requires "protobuf_serialization >= 0.2.0 & < 0.3.0"
requires "questionable >= 0.10.13 & < 0.11.0"
requires "secp256k1"
requires "serde >= 1.0.0 & < 2.0.0"
requires "stew"
requires "upraises >= 0.1.0 & < 0.2.0"
requires "toml_serialization"
Expand Down
15 changes: 7 additions & 8 deletions codex/logutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
## module, and specifying `formatIt`. If textlines log output and json log output
## need to be different, overload `formatIt` and specify a `LogFormat`. If json
## serialization is needed, it can be declared with a `%` proc. `logutils`
## imports and exports `utils/json` which handles the de/serialization, examples
## imports and exports `nim-serde` which handles the de/serialization, examples
## below. **Only `codex/logutils` needs to be imported.**
##
## Using `logutils` in the Codex codebase:
## - Instead of importing `pkg/chronicles`, import `pkg/codex/logutils`
## - most of `chronicles` is exported by `logutils`
## - Instead of importing `std/json`, import `pkg/codex/utils/json`
## - `std/json` is exported by `utils/json` which is exported by `logutils`
## - Instead of importing `std/json`, import `pkg/serde/json`
## - `std/json` is exported by `serde` which is exported by `logutils`
## - Instead of importing `pkg/nim-json-serialization`, import
## `pkg/codex/utils/json`
## `pkg/serde/json` or use codex-specific overloads by importing `utils/json`
## - one of the goals is to remove the use of `nim-json-serialization`
##
## ```nim
Expand Down Expand Up @@ -54,7 +54,7 @@
## # chronicles json output
## {"lvl":"TRC","msg":"test","tid":14397405,"ba":{"treeCid":"zb2rhgsDE16rLtbwTFeNKbdSobtKiWdjJPvKEuPgrQAfndjU1","index":0}}
## ```
## In this case, `BlockAddress` is just an object, so `utils/json` can handle
## In this case, `BlockAddress` is just an object, so `nim-serde` can handle
## serializing it without issue (only fields annotated with `{.serialize.}` will
## serialize (aka opt-in serialization)).
##
Expand Down Expand Up @@ -95,20 +95,19 @@ import pkg/chronicles except toJson, `%`
from pkg/libp2p import Cid, MultiAddress, `$`
import pkg/questionable
import pkg/questionable/results
import ./utils/json except formatIt # TODO: remove exception?
import pkg/stew/byteutils
import pkg/stint
import pkg/upraises

import ./utils/json

export byteutils
export chronicles except toJson, formatIt, `%`
export questionable
export sequtils
export json except formatIt
export strutils
export sugar
export upraises
export json
export results

func shortLog*(long: string, ellipses = "*", start = 3, stop = 6): string =
Expand Down
4 changes: 2 additions & 2 deletions codex/rest/json.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ proc init*(_: type RestNodeId, id: NodeId): RestNodeId =
id: id
)

func `%`*(obj: StorageRequest | Slot): JsonNode =
proc `%`*(obj: StorageRequest | Slot): JsonNode =
let jsonObj = newJObject()
for k, v in obj.fieldPairs: jsonObj[k] = %v
jsonObj["id"] = %(obj.id)

return jsonObj

func `%`*(obj: RestNodeId): JsonNode = % $obj.id
proc `%`*(obj: RestNodeId): JsonNode = % $obj.id
2 changes: 1 addition & 1 deletion codex/slots/builder/builder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import std/sugar

import pkg/libp2p
import pkg/chronos
import pkg/chronicles
import pkg/questionable
import pkg/questionable/results
import pkg/constantine/math/io/io_fields

import ../../logutils
import ../../utils
import ../../stores
import ../../manifest
Expand Down
2 changes: 1 addition & 1 deletion codex/slots/sampler/sampler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

import std/sugar

import pkg/chronicles
import pkg/chronos
import pkg/questionable
import pkg/questionable/results
import pkg/stew/arrayops

import ../../logutils
import ../../market
import ../../blocktype as bt
import ../../merkletree
Expand Down
Loading

0 comments on commit f567f4e

Please sign in to comment.