rwadurian/backend/mpc-system/services/account/domain/value_objects/account_id.go

71 lines
1.6 KiB
Go

package value_objects
import (
"github.com/google/uuid"
)
// AccountID represents a unique account identifier
type AccountID struct {
value uuid.UUID
}
// NewAccountID creates a new AccountID
func NewAccountID() AccountID {
return AccountID{value: uuid.New()}
}
// AccountIDFromString creates an AccountID from a string
func AccountIDFromString(s string) (AccountID, error) {
id, err := uuid.Parse(s)
if err != nil {
return AccountID{}, err
}
return AccountID{value: id}, nil
}
// AccountIDFromUUID creates an AccountID from a UUID
func AccountIDFromUUID(id uuid.UUID) AccountID {
return AccountID{value: id}
}
// String returns the string representation
func (id AccountID) String() string {
return id.value.String()
}
// UUID returns the UUID value
func (id AccountID) UUID() uuid.UUID {
return id.value
}
// IsZero checks if the AccountID is zero
func (id AccountID) IsZero() bool {
return id.value == uuid.Nil
}
// Equals checks if two AccountIDs are equal
func (id AccountID) Equals(other AccountID) bool {
return id.value == other.value
}
// MarshalJSON implements json.Marshaler interface
func (id AccountID) MarshalJSON() ([]byte, error) {
return []byte(`"` + id.value.String() + `"`), nil
}
// UnmarshalJSON implements json.Unmarshaler interface
func (id *AccountID) UnmarshalJSON(data []byte) error {
// Remove quotes
str := string(data)
if len(str) >= 2 && str[0] == '"' && str[len(str)-1] == '"' {
str = str[1 : len(str)-1]
}
parsed, err := uuid.Parse(str)
if err != nil {
return err
}
id.value = parsed
return nil
}