From d0470fca92b68b8ea78946a8544fcc917ff6109d Mon Sep 17 00:00:00 2001 From: Yu-Vitaqua-fer-Chronos Date: Fri, 15 Dec 2023 19:37:57 +0000 Subject: [PATCH 1/3] Fix docs --- src/nulid.nim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/nulid.nim b/src/nulid.nim index 01d37ea..8a6a23f 100644 --- a/src/nulid.nim +++ b/src/nulid.nim @@ -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() @@ -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() From 5e3beea010503e7d5d422418da811a8b74663a5b Mon Sep 17 00:00:00 2001 From: Yu-Vitaqua-fer-Chronos Date: Thu, 25 Jan 2024 21:44:41 +0000 Subject: [PATCH 2/3] Add std/json support, remove redundant test on tag --- .github/workflows/test.yml | 5 ++++- nulid.nimble | 2 +- src/nulid.nim | 16 ++++++++++++---- tests/test1.nim | 12 ++++++++++-- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0d20c97..a57d81a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,7 +1,10 @@ name: Run Tests on: - [push, pull_request] + pull_request + push: + branches: + - '**' env: nim-version: 'stable' diff --git a/nulid.nimble b/nulid.nimble index a1933ae..0db4f8f 100644 --- a/nulid.nimble +++ b/nulid.nimble @@ -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" diff --git a/src/nulid.nim b/src/nulid.nim index 8a6a23f..ff64d04 100644 --- a/src/nulid.nim +++ b/src/nulid.nim @@ -1,5 +1,6 @@ import std/[ - times + times, + json ] import crockfordb32 @@ -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): @@ -327,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 diff --git a/tests/test1.nim b/tests/test1.nim index f89dabf..64bbca4 100644 --- a/tests/test1.nim +++ b/tests/test1.nim @@ -4,8 +4,10 @@ # the letter 't'). # # To run these tests, simply execute `nimble test`. - -import unittest +import std/[ + unittest, + json +] const UlidRandStr = "541019288874337045949482" @@ -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 \ No newline at end of file From 5d35f54bac13e379552f40b493a9f2e172ef99af Mon Sep 17 00:00:00 2001 From: Yu-Vitaqua-fer-Chronos Date: Thu, 25 Jan 2024 21:49:52 +0000 Subject: [PATCH 3/3] Fix action --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a57d81a..fd704db 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,7 +1,7 @@ name: Run Tests on: - pull_request + pull_request: push: branches: - '**'