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

Spec case updates #17

Merged
merged 7 commits into from
Apr 13, 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
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
typeid (0.2.1)
typeid (0.2.2)
uuid7 (~> 0.2.0)

GEM
Expand Down
9 changes: 5 additions & 4 deletions lib/typeid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class Error < StandardError; end
# @param string [String] string representation of a +TypeID+
# @return [TypeID]
def self.from_string(string)
case string.split("_")
in [suffix]
case string.rpartition("_")
in ["", "", suffix]
from("", suffix)

in [prefix, suffix]
in [prefix, "_", suffix]
raise Error, "prefix cannot be empty when there's a separator" if prefix.empty?

from(prefix, suffix)
Expand Down Expand Up @@ -83,7 +83,8 @@ def initialize(
suffix: TypeID::UUID.generate(timestamp: timestamp).base32
)
raise Error, "prefix length cannot be greater than #{MAX_PREFIX_LENGTH}" if prefix.length > MAX_PREFIX_LENGTH
raise Error, "prefix must be lowercase ASCII characters" unless prefix.match?(/^[a-z]*$/)
raise Error, "prefix must be lowercase ASCII characters" unless prefix.match?(/^[a-z_]*$/)
raise Error, "prefix cannot start or end with an underscore" if prefix.start_with?("_") || prefix.end_with?("_")
raise Error, "suffix must be #{TypeID::UUID::Base32::ENCODED_STRING_LENGTH} characters" unless suffix.length == TypeID::UUID::Base32::ENCODED_STRING_LENGTH
raise Error, "suffix must only contain the letters in '#{TypeID::UUID::Base32::ALPHABET}'" unless suffix.chars.all? { |char| TypeID::UUID::Base32::ALPHABET.include?(char) }
raise Error, "suffix must start with a 0-7 digit to avoid overflows" unless ("0".."7").cover?(suffix.chars.first)
Expand Down
2 changes: 1 addition & 1 deletion lib/typeid/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class TypeID < String
VERSION = "0.2.1".freeze
VERSION = "0.2.2".freeze
end
19 changes: 15 additions & 4 deletions spec/invalid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Each example contains an invalid TypeID string. Implementations are expected
# to throw an error when attempting to parse/validate these strings.
#
# Last updated: 2023-07-05
# Last updated: 2024-04-10 (for version 0.3.0 of the spec)

- name: prefix-uppercase
typeid: "PREFIX_00000000000000000000000000"
Expand All @@ -18,9 +18,10 @@
typeid: "pre.fix_00000000000000000000000000"
description: "The prefix can't have symbols, it needs to be alphabetic"

- name: prefix-underscore
typeid: "pre_fix_00000000000000000000000000"
description: "The prefix can't have symbols, it needs to be alphabetic"
# Test removed in v0.3.0 – we now allow underscores in the prefix
# - name: prefix-underscore
# typeid: "pre_fix_00000000000000000000000000"
# description: "The prefix can't have symbols, it needs to be alphabetic"

- name: prefix-non-ascii
typeid: "préfix_00000000000000000000000000"
Expand Down Expand Up @@ -86,3 +87,13 @@
# This is the first suffix that overflows into 129 bits
typeid: "prefix_8zzzzzzzzzzzzzzzzzzzzzzzzz"
description: "The suffix should encode at most 128-bits"

# Tests below were added in v0.3.0 when we started allowing '_' within the
# type prefix.
- name: prefix-underscore-start
typeid: "_prefix_00000000000000000000000000"
description: "The prefix can't start with an underscore"

- name: prefix-underscore-end
typeid: "prefix__00000000000000000000000000"
description: "The prefix can't end with an underscore"
9 changes: 8 additions & 1 deletion spec/valid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# note that not all of them are UUIDv7s. When *generating* new random typeids,
# implementations should always use UUIDv7s.
#
# Last updated: 2023-07-05
# Last updated: 2024-04-10 (for version 0.3.0 of the spec)

- name: nil
typeid: "00000000000000000000000000"
Expand Down Expand Up @@ -64,3 +64,10 @@
typeid: "prefix_01h455vb4pex5vsknk084sn02q"
prefix: "prefix"
uuid: "01890a5d-ac96-774b-bcce-b302099a8057"

# Tests below were added in v0.3.0 when we started allowing '_' within the
# type prefix.
- name: prefix-underscore
typeid: "pre_fix_00000000000000000000000000"
prefix: "pre_fix"
uuid: "00000000-0000-0000-0000-000000000000"
Loading