Skip to content

Commit

Permalink
feat: add Compact() string method to all types
Browse files Browse the repository at this point in the history
  • Loading branch information
mavolin committed Feb 27, 2024
1 parent 5257099 commit 6456d73
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type BIC struct {
Each type implements:

* `String() string` to get the notation pretty-printed
* `MarshalText() ([]byte, error)` to get the notation in compact form
* `Compact() string` to get the notation in compact, machine-readable form
* `MarshalText() ([]byte, error)` same as `Compact`
* `UnmarshalText([]byte) error` to parse the notation

Additionally, each package provides these two functions:
Expand Down
6 changes: 5 additions & 1 deletion bic/bic.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ func (bic BIC) String() string {
return bic.BusinessPartyPrefix + bic.CountryCode + bic.BusinessPartySuffix + bic.BranchCode
}

func (bic *BIC) Compact() string {
return bic.String()
}

var _ encoding.TextMarshaler = BIC{}

func (bic BIC) MarshalText() ([]byte, error) {
return []byte(bic.String()), nil
return []byte(bic.Compact()), nil
}

var _ encoding.TextUnmarshaler = (*BIC)(nil)
Expand Down
6 changes: 5 additions & 1 deletion de/healthinsurancenumber/health_insurance_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ func (id HealthInsuranceNumber) String() string {
return string(id)
}

func (id HealthInsuranceNumber) Compact() string {
return string(id)
}

var _ encoding.TextMarshaler = HealthInsuranceNumber("")

func (id HealthInsuranceNumber) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
return []byte(id.Compact()), nil
}

var _ encoding.TextUnmarshaler = (*HealthInsuranceNumber)(nil)
Expand Down
7 changes: 6 additions & 1 deletion de/pin/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,19 @@ func (pin PensionInsuranceNumber) String() string {

var _ encoding.TextMarshaler = PensionInsuranceNumber{}

// MarshalText implements encoding.TextMarshaler.
// Compact renders a compact representation of the pension insurance number.
//
// In contrast to String(), MarshalText does not add spaces between the
// different parts of the pension insurance number.
//
// The returned string will have the following format:
//
// AADDMMYYLSSC
func (pin PensionInsuranceNumber) Compact() string {
return fmt.Sprintf("%02d%02d%02d%02d%c%02d%d",
pin.AreaCode, pin.BirthDay, pin.BirthMonth, pin.BirthYear, pin.LastNameLetter, pin.SerialNumber, pin.CheckDigit)
}

func (pin PensionInsuranceNumber) MarshalText() ([]byte, error) {
s := fmt.Sprintf("%02d%02d%02d%02d%c%02d%d",
pin.AreaCode, pin.BirthDay, pin.BirthMonth, pin.BirthYear, pin.LastNameLetter, pin.SerialNumber, pin.CheckDigit)
Expand Down
6 changes: 5 additions & 1 deletion de/postalcode/postalcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ func (c PostalCode) String() string {
return string(c)
}

func (c PostalCode) Compact() string {
return string(c)
}

var _ encoding.TextMarshaler = PostalCode("")

func (c PostalCode) MarshalText() ([]byte, error) {
return []byte(c), nil
return []byte(c.Compact()), nil
}

var _ encoding.TextUnmarshaler = (*PostalCode)(nil)
Expand Down
10 changes: 5 additions & 5 deletions de/tin/tin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func (id TIN) String() string {

var _ encoding.TextMarshaler = TIN(0)

// MarshalText implements encoding.TextMarshaler.
//
// In contrast to String(), it returns the TIN as is, without any formatting.
func (id TIN) Compact() string {
return strconv.FormatUint(uint64(id), 10)
}

func (id TIN) MarshalText() ([]byte, error) {
s := strconv.FormatUint(uint64(id), 10)
return []byte(s), nil
return []byte(id.Compact()), nil
}

var _ encoding.TextUnmarshaler = (*TIN)(nil)
Expand Down
13 changes: 8 additions & 5 deletions iban/iban.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (iban IBAN) String() string {
sb.Grow(maxLen + maxLen/4 + 1)

sb.WriteString(iban.CountryCode.Code)
sb.WriteString(uint8ToTwoDigitString(iban.Checksum))
sb.WriteString(twoDigitStr(int(iban.Checksum)))

for i := 0; i < len(iban.BBAN); i += 4 {
sb.WriteByte(' ')
Expand All @@ -67,11 +67,14 @@ func (iban IBAN) String() string {
return sb.String()
}

func (iban IBAN) Compact() string {
return iban.CountryCode.Code + twoDigitStr(int(iban.Checksum)) + iban.BBAN
}

var _ encoding.TextMarshaler = IBAN{}

func (iban IBAN) MarshalText() ([]byte, error) {
s := iban.CountryCode.Code + uint8ToTwoDigitString(iban.Checksum) + iban.BBAN
return []byte(s), nil
return []byte(iban.Compact()), nil
}

var _ encoding.TextUnmarshaler = (*IBAN)(nil)
Expand All @@ -86,6 +89,6 @@ func (iban *IBAN) UnmarshalText(text []byte) error {
return nil
}

func uint8ToTwoDigitString(n uint8) string {
return string('0'+n/10) + string('0'+n%10)
func twoDigitStr(n int) string {
return string(rune('0'+n/10)) + string(rune('0'+n%10))
}
6 changes: 5 additions & 1 deletion iso3166/iso3166.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ func (c Alpha2Code) String() string {
return c.Code
}

func (c Alpha2Code) Compact() string {
return c.Code
}

var _ encoding.TextMarshaler = Alpha2Code{}

func (c Alpha2Code) MarshalText() ([]byte, error) {
return []byte(c.Code), nil
return []byte(c.Compact()), nil
}

var _ encoding.TextUnmarshaler = (*Alpha2Code)(nil)
Expand Down

0 comments on commit 6456d73

Please sign in to comment.