|
| 1 | +// Package primitives provides strongly-typed domain identifier newtypes. |
| 2 | +// |
| 3 | +// These ID types prevent accidental mixing of different identifier types |
| 4 | +// (e.g., passing an AccountID where a CustomerID is expected) by using |
| 5 | +// distinct Go types with compile-time safety. |
| 6 | +// |
| 7 | +// Each ID type: |
| 8 | +// - Validates UUID v4 format on construction |
| 9 | +// - Returns Result[T] for explicit error handling |
| 10 | +// - Supports JSON marshal/unmarshal with validation |
| 11 | +// - Implements fmt.Stringer for easy logging |
| 12 | +package primitives |
| 13 | + |
| 14 | +import ( |
| 15 | + "errors" |
| 16 | + "regexp" |
| 17 | + "strings" |
| 18 | + |
| 19 | + "github.com/meridianhub/meridian/pkg/platform/types" |
| 20 | +) |
| 21 | + |
| 22 | +// Sentinel errors for identifier validation. |
| 23 | +var ( |
| 24 | + ErrInvalidIDFormat = errors.New("invalid ID format: expected UUID v4") |
| 25 | + ErrEmptyID = errors.New("ID cannot be empty") |
| 26 | +) |
| 27 | + |
| 28 | +// uuidV4Pattern matches UUID v4 format (case-insensitive). |
| 29 | +// UUID v4 has version nibble '4' in position 13 and variant nibble [89ab] in position 17. |
| 30 | +var uuidV4Pattern = regexp.MustCompile( |
| 31 | + `^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`, |
| 32 | +) |
| 33 | + |
| 34 | +// validateUUID checks if a string is a valid UUID v4. |
| 35 | +func validateUUID(s string) error { |
| 36 | + if s == "" { |
| 37 | + return ErrEmptyID |
| 38 | + } |
| 39 | + if !uuidV4Pattern.MatchString(strings.ToLower(s)) { |
| 40 | + return ErrInvalidIDFormat |
| 41 | + } |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +// AccountID represents a validated account identifier. |
| 46 | +type AccountID string |
| 47 | + |
| 48 | +// NewAccountID validates and creates an AccountID from a UUID string. |
| 49 | +func NewAccountID(s string) types.Result[AccountID] { |
| 50 | + if err := validateUUID(s); err != nil { |
| 51 | + return types.Err[AccountID](err) |
| 52 | + } |
| 53 | + return types.Ok(AccountID(s)) |
| 54 | +} |
| 55 | + |
| 56 | +// String returns the string representation of the AccountID. |
| 57 | +func (id AccountID) String() string { return string(id) } |
| 58 | + |
| 59 | +// MarshalJSON implements json.Marshaler. |
| 60 | +func (id AccountID) MarshalJSON() ([]byte, error) { |
| 61 | + return []byte(`"` + string(id) + `"`), nil |
| 62 | +} |
| 63 | + |
| 64 | +// UnmarshalJSON implements json.Unmarshaler with validation. |
| 65 | +func (id *AccountID) UnmarshalJSON(data []byte) error { |
| 66 | + s := strings.Trim(string(data), `"`) |
| 67 | + result := NewAccountID(s) |
| 68 | + if result.IsError() { |
| 69 | + return result.Error() |
| 70 | + } |
| 71 | + *id = result.MustGet() |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// CustomerID represents a validated customer identifier. |
| 76 | +type CustomerID string |
| 77 | + |
| 78 | +// NewCustomerID validates and creates a CustomerID from a UUID string. |
| 79 | +func NewCustomerID(s string) types.Result[CustomerID] { |
| 80 | + if err := validateUUID(s); err != nil { |
| 81 | + return types.Err[CustomerID](err) |
| 82 | + } |
| 83 | + return types.Ok(CustomerID(s)) |
| 84 | +} |
| 85 | + |
| 86 | +// String returns the string representation of the CustomerID. |
| 87 | +func (id CustomerID) String() string { return string(id) } |
| 88 | + |
| 89 | +// MarshalJSON implements json.Marshaler. |
| 90 | +func (id CustomerID) MarshalJSON() ([]byte, error) { |
| 91 | + return []byte(`"` + string(id) + `"`), nil |
| 92 | +} |
| 93 | + |
| 94 | +// UnmarshalJSON implements json.Unmarshaler with validation. |
| 95 | +func (id *CustomerID) UnmarshalJSON(data []byte) error { |
| 96 | + s := strings.Trim(string(data), `"`) |
| 97 | + result := NewCustomerID(s) |
| 98 | + if result.IsError() { |
| 99 | + return result.Error() |
| 100 | + } |
| 101 | + *id = result.MustGet() |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +// TransactionID represents a validated transaction identifier. |
| 106 | +type TransactionID string |
| 107 | + |
| 108 | +// NewTransactionID validates and creates a TransactionID from a UUID string. |
| 109 | +func NewTransactionID(s string) types.Result[TransactionID] { |
| 110 | + if err := validateUUID(s); err != nil { |
| 111 | + return types.Err[TransactionID](err) |
| 112 | + } |
| 113 | + return types.Ok(TransactionID(s)) |
| 114 | +} |
| 115 | + |
| 116 | +// String returns the string representation of the TransactionID. |
| 117 | +func (id TransactionID) String() string { return string(id) } |
| 118 | + |
| 119 | +// MarshalJSON implements json.Marshaler. |
| 120 | +func (id TransactionID) MarshalJSON() ([]byte, error) { |
| 121 | + return []byte(`"` + string(id) + `"`), nil |
| 122 | +} |
| 123 | + |
| 124 | +// UnmarshalJSON implements json.Unmarshaler with validation. |
| 125 | +func (id *TransactionID) UnmarshalJSON(data []byte) error { |
| 126 | + s := strings.Trim(string(data), `"`) |
| 127 | + result := NewTransactionID(s) |
| 128 | + if result.IsError() { |
| 129 | + return result.Error() |
| 130 | + } |
| 131 | + *id = result.MustGet() |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +// PostingID represents a validated posting identifier. |
| 136 | +type PostingID string |
| 137 | + |
| 138 | +// NewPostingID validates and creates a PostingID from a UUID string. |
| 139 | +func NewPostingID(s string) types.Result[PostingID] { |
| 140 | + if err := validateUUID(s); err != nil { |
| 141 | + return types.Err[PostingID](err) |
| 142 | + } |
| 143 | + return types.Ok(PostingID(s)) |
| 144 | +} |
| 145 | + |
| 146 | +// String returns the string representation of the PostingID. |
| 147 | +func (id PostingID) String() string { return string(id) } |
| 148 | + |
| 149 | +// MarshalJSON implements json.Marshaler. |
| 150 | +func (id PostingID) MarshalJSON() ([]byte, error) { |
| 151 | + return []byte(`"` + string(id) + `"`), nil |
| 152 | +} |
| 153 | + |
| 154 | +// UnmarshalJSON implements json.Unmarshaler with validation. |
| 155 | +func (id *PostingID) UnmarshalJSON(data []byte) error { |
| 156 | + s := strings.Trim(string(data), `"`) |
| 157 | + result := NewPostingID(s) |
| 158 | + if result.IsError() { |
| 159 | + return result.Error() |
| 160 | + } |
| 161 | + *id = result.MustGet() |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +// LedgerID represents a validated ledger identifier. |
| 166 | +type LedgerID string |
| 167 | + |
| 168 | +// NewLedgerID validates and creates a LedgerID from a UUID string. |
| 169 | +func NewLedgerID(s string) types.Result[LedgerID] { |
| 170 | + if err := validateUUID(s); err != nil { |
| 171 | + return types.Err[LedgerID](err) |
| 172 | + } |
| 173 | + return types.Ok(LedgerID(s)) |
| 174 | +} |
| 175 | + |
| 176 | +// String returns the string representation of the LedgerID. |
| 177 | +func (id LedgerID) String() string { return string(id) } |
| 178 | + |
| 179 | +// MarshalJSON implements json.Marshaler. |
| 180 | +func (id LedgerID) MarshalJSON() ([]byte, error) { |
| 181 | + return []byte(`"` + string(id) + `"`), nil |
| 182 | +} |
| 183 | + |
| 184 | +// UnmarshalJSON implements json.Unmarshaler with validation. |
| 185 | +func (id *LedgerID) UnmarshalJSON(data []byte) error { |
| 186 | + s := strings.Trim(string(data), `"`) |
| 187 | + result := NewLedgerID(s) |
| 188 | + if result.IsError() { |
| 189 | + return result.Error() |
| 190 | + } |
| 191 | + *id = result.MustGet() |
| 192 | + return nil |
| 193 | +} |
0 commit comments