Skip to content

Commit 5e3beea

Browse files
Add std/json support, remove redundant test on tag
1 parent d0470fc commit 5e3beea

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-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: 12 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):
@@ -327,6 +327,14 @@ func `$`*(ulid: ULID): string =
327327
else:
328328
result = JsBigInt.encode(ulid.toInt128(), 26)
329329

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())
330338

331339
when HasJsony:
332340
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)