Skip to content

Commit 8c3165a

Browse files
Merge pull request #2 from Yu-Vitaqua-fer-Chronos/devel
Add std/json support
2 parents f998c0d + 5d35f54 commit 8c3165a

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
name: Run Tests
22

33
on:
4-
[push, pull_request]
4+
pull_request:
5+
push:
6+
branches:
7+
- '**'
58

69
env:
710
nim-version: 'stable'

nulid.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "1.2.0"
3+
version = "1.3.0"
44
author = "Yu Vitaqua fer Chronos"
55
description = "An implementation of ULID!"
66
license = "CC0"

src/nulid.nim

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import std/[
2-
times
2+
times,
3+
json
34
]
45

56
import crockfordb32
@@ -28,10 +29,9 @@ when not NoLocks:
2829
Note: There are 2 defines that can be passed to the compiler to trigger different
2930
functionality in this library at runtime, they are listed here:
3031
- `--define:nulidInsecureRandom`: Uses `std/random` instead of `std/sysrand`.
31-
- `--define:nulidNoLocks`
32+
- `--define:nulidNoLocks`: Disables the use of locks.
3233
33-
The JS backend used `-d:nulidNoLocks` by default and Nimscript uses both.
34-
these flags by default (whether either work with NULID is untested).
34+
The JS backend used `-d:nulidNoLocks` by default.
3535
]##
3636

3737
when not defined(js):
@@ -176,6 +176,9 @@ proc wait(gen: ULIDGenerator): int64 {.gcsafe.} =
176176
proc ulid*(gen: ULIDGenerator, timestamp = LowInt48, randomness = LowUint80): ULID {.gcsafe.} =
177177
## Generate a `ULID`, if timestamp is equal to `0`, the `randomness` parameter
178178
## will be ignored.
179+
##
180+
## See also:
181+
## * `ulid(int64, UInt128) <#ulid_2>`_
179182
runnableExamples:
180183
let gen = initUlidGenerator()
181184

@@ -324,6 +327,14 @@ func `$`*(ulid: ULID): string =
324327
else:
325328
result = JsBigInt.encode(ulid.toInt128(), 26)
326329

330+
# std/json support
331+
proc `%`*(u: ULID): JsonNode = newJString($u)
332+
333+
proc to*(j: JsonNode, _: typedesc[ULID]): ULID =
334+
if j.kind != JString:
335+
raise newException(JsonKindError, "Expected a string!")
336+
337+
result = ULID.parse(j.getStr())
327338

328339
when HasJsony:
329340
import jsony

tests/test1.nim

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
# the letter 't').
55
#
66
# To run these tests, simply execute `nimble test`.
7-
8-
import unittest
7+
import std/[
8+
unittest,
9+
json
10+
]
911

1012
const UlidRandStr = "541019288874337045949482"
1113

@@ -47,3 +49,9 @@ when not defined(js):
4749

4850
check ulid == ULID.fromBytes(ulidBytes)
4951
check ulid.toBytes == ulidBytes
52+
53+
test "ULID std/json support":
54+
let ulid = ULID.parse("01H999MBGTEA8BDS0M5AWEBB1A")
55+
56+
check (%ulid).getStr() == "01H999MBGTEA8BDS0M5AWEBB1A"
57+
check (%ulid).to(ULID) == ulid

0 commit comments

Comments
 (0)