Add std/json support, remove redundant test on tag

This commit is contained in:
Yu-Vitaqua-fer-Chronos 2024-01-25 21:44:41 +00:00
parent d0470fca92
commit 5e3beea010
4 changed files with 27 additions and 8 deletions

View file

@ -1,7 +1,10 @@
name: Run Tests name: Run Tests
on: on:
[push, pull_request] pull_request
push:
branches:
- '**'
env: env:
nim-version: 'stable' nim-version: 'stable'

View file

@ -1,6 +1,6 @@
# Package # Package
version = "1.2.0" version = "1.3.0"
author = "Yu Vitaqua fer Chronos" author = "Yu Vitaqua fer Chronos"
description = "An implementation of ULID!" description = "An implementation of ULID!"
license = "CC0" license = "CC0"

View file

@ -1,5 +1,6 @@
import std/[ import std/[
times times,
json
] ]
import crockfordb32 import crockfordb32
@ -28,10 +29,9 @@ when not NoLocks:
Note: There are 2 defines that can be passed to the compiler to trigger different Note: There are 2 defines that can be passed to the compiler to trigger different
functionality in this library at runtime, they are listed here: functionality in this library at runtime, they are listed here:
- `--define:nulidInsecureRandom`: Uses `std/random` instead of `std/sysrand`. - `--define:nulidInsecureRandom`: Uses `std/random` instead of `std/sysrand`.
- `--define:nulidNoLocks` - `--define:nulidNoLocks`: Disables the use of locks.
The JS backend used `-d:nulidNoLocks` by default and Nimscript uses both. The JS backend used `-d:nulidNoLocks` by default.
these flags by default (whether either work with NULID is untested).
]## ]##
when not defined(js): when not defined(js):
@ -327,6 +327,14 @@ func `$`*(ulid: ULID): string =
else: else:
result = JsBigInt.encode(ulid.toInt128(), 26) result = JsBigInt.encode(ulid.toInt128(), 26)
# std/json support
proc `%`*(u: ULID): JsonNode = newJString($u)
proc to*(j: JsonNode, _: typedesc[ULID]): ULID =
if j.kind != JString:
raise newException(JsonKindError, "Expected a string!")
result = ULID.parse(j.getStr())
when HasJsony: when HasJsony:
import jsony import jsony

View file

@ -4,8 +4,10 @@
# the letter 't'). # the letter 't').
# #
# To run these tests, simply execute `nimble test`. # To run these tests, simply execute `nimble test`.
import std/[
import unittest unittest,
json
]
const UlidRandStr = "541019288874337045949482" const UlidRandStr = "541019288874337045949482"
@ -47,3 +49,9 @@ when not defined(js):
check ulid == ULID.fromBytes(ulidBytes) check ulid == ULID.fromBytes(ulidBytes)
check ulid.toBytes == ulidBytes check ulid.toBytes == ulidBytes
test "ULID std/json support":
let ulid = ULID.parse("01H999MBGTEA8BDS0M5AWEBB1A")
check (%ulid).getStr() == "01H999MBGTEA8BDS0M5AWEBB1A"
check (%ulid).to(ULID) == ulid