internal

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Services []Service
)

Functions

This section is empty.

Types

type Additional

type Additional struct {
	Name           string   `json:"name"`
	RegexGroupName string   `json:"regex_group_name"`
	Lookups        []Lookup `json:"lookup"`
}

Additional describes lookup values that may be encoded into the tracking number.

type CheckDigit

type CheckDigit interface {
	// Generate calculates a check digit
	Generate(seed string) (string, error)

	// Validate accepts the serial number and check digit portions of a tracking
	// number and validates the serial number.
	Validate(serial, code string) bool
}

CheckDigit provides an interface for all check digit algorithms.

func NewMod10

func NewMod10(em, om int) CheckDigit

func NewMod3736

func NewMod3736() CheckDigit

func NewMod7

func NewMod7() CheckDigit

func NewNoop

func NewNoop() CheckDigit

NewNoop instantiates a check digit algorithm that always passes.

func NewS10

func NewS10(weightings []int, exists []string) CheckDigit

func NewSumProductWithWeightingsAndModulo

func NewSumProductWithWeightingsAndModulo(weightings []int, m1, m2 int) CheckDigit

type CheckDigitOpts

type CheckDigitOpts struct {
	Name            string `json:"name"`
	EvensMultiplier int    `json:"evens_multiplier"`
	OddsMultiplier  int    `json:"odds_multiplier"`
	Weightings      []int  `json:"weightings"`
	Modulo1         int    `json:"modulo1"`
	Modulo2         int    `json:"modulo2"`
}

CheckDigitOpts contains the configuration values for check digit validations

type Courier

type Courier struct {
	Name        string    `json:"name"`
	CourierCode string    `json:"courier_code"`
	Services    []Service `json:"tracking_numbers"`
}

Courier is the data from a single json file

type Lookup

type Lookup struct {
	Matches          string      `json:"matches"`
	MatchesRegex     RegexParser `json:"matches_regex"`
	Name             string      `json:"name"`
	Description      string      `json:"description"`
	CountryCode      string      `json:"country_code"`
	CountryShortCode string      `json:"country_short_code"`
	Country          string      `json:"country"`
	Courier          string      `json:"courier"`
	CourierURL       string      `json:"courier_url"`
	UPUReferenceURL  string      `json:"upu_reference_url"`
}

type Mod10

type Mod10 struct {
	// 0: Even 1: Odd
	Multipliers [2]int
}

func (*Mod10) Generate

func (m10 *Mod10) Generate(s string) (string, error)

func (*Mod10) Validate

func (m10 *Mod10) Validate(s, cd string) bool

type Mod3736

type Mod3736 struct{}

func (*Mod3736) Generate

func (m3 *Mod3736) Generate(s string) (string, error)

func (*Mod3736) Validate

func (m3 *Mod3736) Validate(s, cd string) bool

type Mod7

type Mod7 struct{}

func (Mod7) Generate

func (m Mod7) Generate(s string) (string, error)

func (Mod7) Validate

func (m Mod7) Validate(s, cd string) bool

type Noop

type Noop struct{}

Noop is used if no check digit is needed.

func (Noop) Generate

func (n Noop) Generate(s string) (string, error)

func (Noop) Validate

func (n Noop) Validate(s, cd string) bool

type PrependIf

type PrependIf struct {
	Regex   RegexParser `json:"matches_regex"`
	Content string      `json:"content"`
}

PrependIf contains the definition for tracking number prefixes that may be required to validate properly.

type RegexParser

type RegexParser struct {
	Regex *regexp.Regexp
}

RegexParser is a helper type to convert the PCRE regex to compatible Regex.

func (*RegexParser) UnmarshalJSON

func (r *RegexParser) UnmarshalJSON(buf []byte) error

type S10

type S10 struct {
	Weightings []int
}

func (*S10) Generate

func (s10 *S10) Generate(s string) (string, error)

func (*S10) Validate

func (s10 *S10) Validate(s, cd string) bool

type Service

type Service struct {
	ID          string `json:"id"`
	Name        string `json:"name"`
	CourierName string `json:"-"` // from flattening courier json file
	CourierCode string `json:"-"` // from flattening courier json file
	Description string `json:"description"`
	TrackingURL string `json:"tracking_url"`

	Regex RegexParser `json:"regex"`

	Validation `json:"validation,omitempty"`

	// Test numbers drive the package level tests.
	TestNumbers struct {
		Valid   []string `json:"valid"`
		Invalid []string `json:"invalid"`
	} `json:"test_numbers"`

	Additional []Additional `json:"additional,omitempty"`

	Partners []struct {
		Description string `json:"description"`
		PartnerType string `json:"partner_type"`
		PartnerID   string `json:"partner_id"`
		Validation  struct {
			MatchesAll []struct {
				RegexGroupName string `json:"regex_group_name"`
				Matches        string `json:"matches"`
			} `json:"matches_all"`
		} `json:"validation"`
	} `json:"partners,omitempty"`
}

Service is a single service from a courier json file. Couriers may provide more than one service with uniquely formatted tracking numbers.

func (Service) ValidateAdditionalExists

func (s Service) ValidateAdditionalExists(matchKey string, keys map[string]string) bool

ValidateAdditionalExists validates lookup values from the additional data. The values must be defined in the service to pass this validation.

type SumProductWithWeightingsAndModulo

type SumProductWithWeightingsAndModulo struct {
	Weightings []int
	Modulo1    int
	Modulo2    int
}

func (*SumProductWithWeightingsAndModulo) Generate

func (*SumProductWithWeightingsAndModulo) Validate

func (sp *SumProductWithWeightingsAndModulo) Validate(s, cd string) bool

type Validation

type Validation struct {
	CheckDigitOpts `json:"checksum"`
	Validator      CheckDigit `json:"-"`
	Additional     struct {
		// Exists verifies a portions of the additional data extracted matches
		// an entry in the corresponding list of lookups defined with the
		// service.
		Exists []string `json:"exists"`
	} `json:"additional"`

	// SerialNumberFormat conditionally modifies the serial number prior
	// to validating.
	SerialNumberFormat struct {
		PrependIf PrependIf `json:"prepend_if"`
	} `json:"serial_number_format"`
}

Validation defines the validations to be applied to a tracking number.

func (*Validation) SetValidator

func (val *Validation) SetValidator()

SetValidator applies the appropriate validation function for check digits.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL