Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add std/json support #2

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name: Run Tests

on:
[push, pull_request]
pull_request:
push:
branches:
- '**'

env:
nim-version: 'stable'
Expand Down
2 changes: 1 addition & 1 deletion nulid.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "1.2.0"
version = "1.3.0"
author = "Yu Vitaqua fer Chronos"
description = "An implementation of ULID!"
license = "CC0"
Expand Down
21 changes: 16 additions & 5 deletions src/nulid.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import std/[
times
times,
json
]

import crockfordb32
Expand Down Expand Up @@ -28,10 +29,9 @@ when not NoLocks:
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:
- `--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.
these flags by default (whether either work with NULID is untested).
The JS backend used `-d:nulidNoLocks` by default.
]##

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

Expand Down Expand Up @@ -208,7 +211,7 @@ proc ulid*(timestamp = LowInt48, randomness = LowUint80): ULID =
## Generate a `ULID` using the global generator.
##
## See also:
## * `ulid(ULIDGenerator, int64, UInt128) <#ulid,ULIDGenerator,int64>`_
## * `ulid(ULIDGenerator, int64, UInt128) <#ulid,ULIDGenerator>`_
runnableExamples:
echo ulid()

Expand Down Expand Up @@ -324,6 +327,14 @@ func `$`*(ulid: ULID): string =
else:
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:
import jsony
Expand Down
12 changes: 10 additions & 2 deletions tests/test1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
# the letter 't').
#
# To run these tests, simply execute `nimble test`.

import unittest
import std/[
unittest,
json
]

const UlidRandStr = "541019288874337045949482"

Expand Down Expand Up @@ -47,3 +49,9 @@ when not defined(js):

check ulid == ULID.fromBytes(ulidBytes)
check ulid.toBytes == ulidBytes

test "ULID std/json support":
let ulid = ULID.parse("01H999MBGTEA8BDS0M5AWEBB1A")

check (%ulid).getStr() == "01H999MBGTEA8BDS0M5AWEBB1A"
check (%ulid).to(ULID) == ulid
Loading