cbc

package
v0.208.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: Apache-2.0 Imports: 9 Imported by: 11

Documentation

Overview

Package cbc provides a set of Common Basic Components.

Name is take from the similar namespace used in UBL.

Index

Constants

This section is empty.

Variables

View Source
var (
	CodePattern              = `^[A-Za-z0-9]+([\.\-\/ _\:]?[A-Za-z0-9]+)*$`
	CodePatternRegexp        = regexp.MustCompile(CodePattern)
	CodeMinLength     uint64 = 1
	CodeMaxLength     uint64 = 32
)

Basic code constants.

View Source
var (
	// KeyPattern describes what should keys look like
	KeyPattern = `^(?:[a-z]|[a-z0-9][a-z0-9-+]*[a-z0-9])$`
	// KeyValidationRegexp is used for key validation
	KeyValidationRegexp = regexp.MustCompile(KeyPattern)
	// KeySeparator is used to separate keys join using the "With"
	// method.
	KeySeparator = "+"
)
View Source
var (
	// KeyMinLength defines the minimum key length
	KeyMinLength uint64 = 1
	// KeyMaxLength defines the maximum key length
	KeyMaxLength uint64 = 64
)

Functions

func CodeMapHas added in v0.55.0

func CodeMapHas(keys ...Key) validation.Rule

CodeMapHas returns a validation rule that ensures the code set contains the provided keys.

func HasValidKeyIn added in v0.50.0

func HasValidKeyIn(keys ...Key) validation.Rule

HasValidKeyIn provides a validator to check the Key's value is within the provided known set.

func InCodeDefs added in v0.207.0

func InCodeDefs(list []*Definition) validation.Rule

InCodeDefs prepares a validation to provide a rule that will determine if the codes are in the provided set.

func InKeyDefs added in v0.69.0

func InKeyDefs(list []*Definition) validation.Rule

InKeyDefs prepares a validation to provide a rule that will determine if the keys are in the provided set.

func KeyStrings added in v0.81.0

func KeyStrings(keys []Key) []string

KeyStrings is a convenience method to convert a list of keys into a list of strings.

Types

type Code

type Code string

Code represents a string used to uniquely identify the data we're looking at. We use "code" instead of "id", to reenforce the fact that codes should be more easily set and used by humans within definitions than IDs or UUIDs. Codes are standardised so that when validated they must contain between 1 and 32 inclusive english alphabet letters or numbers with optional periods (`.`), dashes (`-`), underscores (`_`), forward slashes (`/`), colons (`:`) or spaces (` `) to separate blocks. Each block must only be separated by a single symbol.

The objective is to have a code that is easy to read and understand, while still being unique and easy to validate.

const CodeEmpty Code = ""

CodeEmpty is used when no code is defined.

const (
	// DefaultCodeSeparator is the default separator used to join codes.
	DefaultCodeSeparator Code = "-"
)

func DefinitionCodes added in v0.207.0

func DefinitionCodes(list []*Definition) []Code

DefinitionCodes helps extract the codes from a list of key definitions.

func NormalizeAlphanumericalCode added in v0.204.0

func NormalizeAlphanumericalCode(c Code) Code

NormalizeAlphanumericalCode cleans and normalizes the code, ensuring all letters are uppercase while also removing non-alphanumerical characters.

func NormalizeCode added in v0.200.0

func NormalizeCode(c Code) Code

NormalizeCode attempts to clean and normalize the provided code so that it matches what we'd expect instead of raising validation errors.

func NormalizeNumericalCode added in v0.204.0

func NormalizeNumericalCode(c Code) Code

NormalizeNumericalCode cleans and normalizes the code, while also removing non-numerical characters.

func (Code) In

func (c Code) In(ary ...Code) bool

In returns true if the code's value matches one of those in the provided list.

func (Code) IsEmpty

func (c Code) IsEmpty() bool

IsEmpty returns true if no code is specified.

func (Code) JSONSchema

func (Code) JSONSchema() *jsonschema.Schema

JSONSchema provides a representation of the struct for usage in Schema.

func (Code) Join added in v0.202.0

func (c Code) Join(c2 Code) Code

Join returns a new code that is the result of joining the provided code with the current one using a default separator.

func (Code) JoinWith added in v0.202.0

func (c Code) JoinWith(separator Code, c2 Code) Code

JoinWith returns a new code that is the result of joining the provided code with the current one using the provided separator. If any of the codes are empty, no separator will be added.

func (Code) String

func (c Code) String() string

String returns string representation of code.

func (Code) Validate

func (c Code) Validate() error

Validate ensures that the code complies with the expected rules.

type CodeMap added in v0.55.0

type CodeMap map[Key]Code

CodeMap is a map of keys to specific codes, useful to determine regime specific codes from their key counterparts.

func (CodeMap) Equals added in v0.55.0

func (cs CodeMap) Equals(other CodeMap) bool

Equals returns true if the code map has the same keys and values as the provided map.

func (CodeMap) Has added in v0.55.0

func (cs CodeMap) Has(keys ...Key) bool

Has returns true if the code map has values for all the provided keys.

func (CodeMap) JSONSchemaExtend added in v0.55.0

func (CodeMap) JSONSchemaExtend(schema *jsonschema.Schema)

JSONSchemaExtend ensures the pattern property is set correctly.

func (CodeMap) Validate added in v0.55.0

func (cs CodeMap) Validate() error

Validate ensures the code maps data looks correct.

type Definition added in v0.207.0

type Definition struct {
	// Key being defined.
	Key Key `json:"key,omitempty" jsonschema:"title=Key"`
	// Code this definition represents.
	Code Code `json:"code,omitempty" jsonschema:"title=Code"`

	// Short name for the key.
	Name i18n.String `json:"name" jsonschema:"title=Name"`
	// Description offering more details about when the key should be used.
	Desc i18n.String `json:"desc,omitempty" jsonschema:"title=Description"`
	// Meta defines any additional details that may be useful or associated
	// with the key.
	Meta Meta `json:"meta,omitempty" jsonschema:"title=Meta"`

	// Values defines the possible values associated with the key, which themselves will
	// either be keys or codes depending on the context.
	Values []*Definition `json:"values,omitempty" jsonschema:"title=Values"`

	// Pattern is used to validate the key value instead of using a fixed value
	// from the code or key definitions.
	Pattern string `json:"pattern,omitempty" jsonschema:"title=Pattern"`

	// Map helps map local keys to specific codes, useful for converting the
	// described key into a local code.
	Map CodeMap `json:"map,omitempty" jsonschema:"title=Code Map"`
}

Definition defines properties of a key, code, or other value that has a specific meaning or utility.

func GetCodeDefinition added in v0.207.0

func GetCodeDefinition(code Code, list []*Definition) *Definition

GetCodeDefinition helps fetch the code definition instance from a list.

func GetKeyDefinition added in v0.69.0

func GetKeyDefinition(key Key, list []*Definition) *Definition

GetKeyDefinition helps fetch the key definition instance from a list.

func (*Definition) CodeDef added in v0.207.0

func (d *Definition) CodeDef(code Code) *Definition

CodeDef searches the list of values and provides the matching definition.

func (*Definition) HasCode added in v0.207.0

func (d *Definition) HasCode(code Code) bool

HasCode loops through values and determines if there is a match for the code.

func (*Definition) HasKey added in v0.207.0

func (d *Definition) HasKey(key Key) bool

HasKey loops through values and determines if there is a match for the key.

func (*Definition) KeyDef added in v0.207.0

func (d *Definition) KeyDef(key Key) *Definition

KeyDef searches the list of values and provides the matching definition.

func (*Definition) Validate added in v0.207.0

func (d *Definition) Validate() error

Validate ensures the definition looks correct.

type Key

type Key string

Key is used to define an ID or code that more closely represents a human name. The objective is to make it easier to define constants that can be re-used more easily.

const KeyEmpty Key = ""

KeyEmpty is used when no key is available.

func AppendUniqueKeys added in v0.204.0

func AppendUniqueKeys(keys []Key, key ...Key) []Key

AppendUniqueKeys is a convenience method to append keys to a list ensuring that any existing keys are not re-added.

func DefinitionKeys added in v0.81.0

func DefinitionKeys(list []*Definition) []Key

DefinitionKeys helps extract the keys from a list of key definitions.

func (Key) Has added in v0.50.0

func (k Key) Has(ke Key) bool

Has returns true if the key contains the provided key.

func (Key) HasPrefix added in v0.50.0

func (k Key) HasPrefix(ke Key) bool

HasPrefix checks to see if the key starts with the provided key. As per `Has`, only the complete key between `+` symbols are matched.

func (Key) In

func (k Key) In(set ...Key) bool

In returns true if the key's value matches one of those in the provided list.

func (Key) IsEmpty added in v0.50.0

func (k Key) IsEmpty() bool

IsEmpty returns true if the key has no value.

func (Key) JSONSchema

func (Key) JSONSchema() *jsonschema.Schema

JSONSchema provides a representation of the struct for usage in Schema.

func (Key) Pop added in v0.207.0

func (k Key) Pop() Key

Pop removes the last key from a list and returns the remaining base, or an empty key if there is nothing left.

Example:

Key("a+b+c").Pop() => Key("a+b")

func (Key) String

func (k Key) String() string

String provides string representation of key

func (Key) Validate

func (k Key) Validate() error

Validate ensures the key complies with the basic syntax requirements.

func (Key) With

func (k Key) With(ke Key) Key

With provides a new key that combines another joining them together with a `+` symbol.

type Meta

type Meta map[Key]string

Meta defines a structure for data about the data being defined. Typically would be used for adding additional IDs or specifications not already defined or required by the base structure.

GOBL is focussed on ensuring the recipient has everything they need, as such, meta should only be used for data that may be used by intermediary conversion processes that should not be needed by the end-user.

We need to always use strings for values so that meta-data is easy to convert into other formats, such as protobuf which has strict type requirements.

func (Meta) Equals added in v0.112.0

func (m Meta) Equals(m2 Meta) bool

Equals checks if the meta data is the same.

func (Meta) JSONSchemaExtend added in v0.50.0

func (Meta) JSONSchemaExtend(schema *jsonschema.Schema)

JSONSchemaExtend ensures the meta keys are valid.

func (Meta) Validate

func (m Meta) Validate() error

Validate ensures the meta data looks correct.

Jump to

Keyboard shortcuts

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