granitic: github.com/graniticio/granitic/types Index | Files

package types

import "github.com/graniticio/granitic/types"

Package types provides versions of Go's built-in types that better support web-services.

Serialising data into and out of some of Go's native types can be problematic. This is because the zero value of numbers, bools and strings are not nil, but 0, false and "" respectively. This means the intent of the original data can be lost. Did the caller provider 'false' or just forget to specify a value?

A similar problem is solved with Go's sql.NullXXX types for handling null values in and out of databases, but those types are not suitable for use with web services.

Grantic defines a set of four 'nilable' types for handling int64, float64, bool and string values that might not always have a value associated with them. There is deep support for these types throughout Granitic including JSON and XML marhsalling/unmarshalling, path and query parameter binding, validation, query templating and RDBMS access. Developers are strongly encouraged to use nilable types instead of native types wherever possible.

This package also defines a number of simple implmentations of a 'set'. Caution should be used when using these types in your own application as they are not goroutine safe or intended to store large numbers of strings.

Index

Package Files

nilable.go params.go sets.go

type FieldAssociatedError Uses

type FieldAssociatedError interface {
    // RecordField captures the field that was involved in the error
    RecordField(string)
}

FieldAssociatedError is implemented by types that can record which field on a struct caused a problem

type GenerateMappingError Uses

type GenerateMappingError func(paramName string, fieldName string, typeName string, params *Params) error

GenerateMappingError is used to create an error in the context of mapping a parameter into a struct field

type Nilable Uses

type Nilable interface {

    // MarshalJSON converts the contained value to JSON or nil if no value is set.
    MarshalJSON() ([]byte, error)

    // UnmarshalJSON populates the type with the supplied JSON value or ignore if value is JSON null
    UnmarshalJSON(b []byte) error

    // IsSet returns true if the value in this type was explicitly set
    IsSet() bool
}

Nilable is implemented by a type that acts as a wrapper round a native type to track whether a value has actually been set.

type NilableBool Uses

type NilableBool struct {
    // contains filtered or unexported fields
}

NilableBool is a bool where it can be determined if false is an explicitly set value, or just the default zero value.

func NewNilableBool Uses

func NewNilableBool(b bool) *NilableBool

NewNilableBool creates a new NilableBool with the supplied value.

func (*NilableBool) Bool Uses

func (nb *NilableBool) Bool() bool

Bool returns the currently stored value (whether or not it has been explicitly set).

func (*NilableBool) IsSet Uses

func (nb *NilableBool) IsSet() bool

IsSet implements Nilable.IsSet

func (*NilableBool) MarshalJSON Uses

func (nb *NilableBool) MarshalJSON() ([]byte, error)

MarshalJSON implements Nilable.MarshalJSON

func (*NilableBool) Set Uses

func (nb *NilableBool) Set(v bool)

Set sets the contained value to the supplied value and makes IsSet true even if the supplied value is false.

func (*NilableBool) UnmarshalJSON Uses

func (nb *NilableBool) UnmarshalJSON(b []byte) error

UnmarshalJSON implements Nilable.UnmarshalJSON

type NilableFloat64 Uses

type NilableFloat64 struct {
    // contains filtered or unexported fields
}

NilableFloat64 is a float64 where it can be determined if 0 is an explicitly set value, or just the default zero value.

func NewNilableFloat64 Uses

func NewNilableFloat64(f float64) *NilableFloat64

NewNilableFloat64 creates a new NilableFloat64 with the supplied value.

func (*NilableFloat64) Float64 Uses

func (nf *NilableFloat64) Float64() float64

Float64 returns the currently stored value (whether or not it has been explicitly set).

func (*NilableFloat64) IsSet Uses

func (nf *NilableFloat64) IsSet() bool

IsSet implements Nilable.IsSet

func (*NilableFloat64) MarshalJSON Uses

func (nf *NilableFloat64) MarshalJSON() ([]byte, error)

MarshalJSON implements Nilable.MarshalJSON

func (*NilableFloat64) Set Uses

func (nf *NilableFloat64) Set(v float64)

Set sets the contained value to the supplied value and makes IsSet true even if the supplied value is 0.

func (*NilableFloat64) UnmarshalJSON Uses

func (nf *NilableFloat64) UnmarshalJSON(b []byte) error

UnmarshalJSON implements Nilable.UnmarshalJSON

type NilableInt64 Uses

type NilableInt64 struct {
    // contains filtered or unexported fields
}

NilableInt64 is an int64 where it can be determined if 0 is an explicitly set value, or just the default zero value.

func NewNilableInt64 Uses

func NewNilableInt64(i int64) *NilableInt64

NewNilableInt64 creates a new NilableInt64 with the supplied value.

func (*NilableInt64) Int64 Uses

func (ni *NilableInt64) Int64() int64

Int64 returns the currently stored value (whether or not it has been explicitly set).

func (*NilableInt64) IsSet Uses

func (ni *NilableInt64) IsSet() bool

IsSet implements Nilable.IsSet

func (*NilableInt64) MarshalJSON Uses

func (ni *NilableInt64) MarshalJSON() ([]byte, error)

MarshalJSON implements Nilable.MarshalJSON

func (*NilableInt64) Set Uses

func (ni *NilableInt64) Set(v int64)

Set sets the contained value to the supplied value and makes IsSet true even if the supplied value is 0.

func (*NilableInt64) UnmarshalJSON Uses

func (ni *NilableInt64) UnmarshalJSON(b []byte) error

UnmarshalJSON implements Nilable.UnmarshalJSON

type NilableString Uses

type NilableString struct {
    // contains filtered or unexported fields
}

NilableString is a string where it can be determined if "" is an explicitly set value, or just the default zero value

func NewNilableString Uses

func NewNilableString(v string) *NilableString

NewNilableString creates a new NilableString with the supplied value.

func (*NilableString) IsSet Uses

func (ns *NilableString) IsSet() bool

IsSet implements Nilable.IsSet

func (*NilableString) MarshalJSON Uses

func (ns *NilableString) MarshalJSON() ([]byte, error)

MarshalJSON implements Nilable.MarshalJSON

func (*NilableString) Set Uses

func (ns *NilableString) Set(v string)

Set sets the contained value to the supplied value and makes IsSet true even if the supplied value is the empty string.

func (*NilableString) String Uses

func (ns *NilableString) String() string

String returns the currently stored value (whether or not it has been explicitly set).

func (*NilableString) UnmarshalJSON Uses

func (ns *NilableString) UnmarshalJSON(b []byte) error

UnmarshalJSON implements Nilable.UnmarshalJSON

type OrderedStringSet Uses

type OrderedStringSet struct {
    // contains filtered or unexported fields
}

An OrderedStringSet is a set of strings where the order in which the strings were added to the set is preserved. Calls to Contents will return the strings in the same order in which they were added. This type is not goroutine safe and not recommended for the storage of large number of strings.

func NewEmptyOrderedStringSet Uses

func NewEmptyOrderedStringSet() *OrderedStringSet

NewEmptyOrderedStringSet creates an empty OrderedStringSet

func NewOrderedStringSet Uses

func NewOrderedStringSet(m []string) *OrderedStringSet

NewOrderedStringSet creates an OrderedStringSet with the supplied strings added to the new set in the provided order.

func (*OrderedStringSet) Add Uses

func (os *OrderedStringSet) Add(s string)

Add implements StringSet.Add

func (*OrderedStringSet) AddAll Uses

func (os *OrderedStringSet) AddAll(ss StringSet)

AddAll implements StringSet.AddAll

func (*OrderedStringSet) Contains Uses

func (os *OrderedStringSet) Contains(m string) bool

Contains implements StringSet.Contains

func (*OrderedStringSet) Contents Uses

func (os *OrderedStringSet) Contents() []string

Contents returns the all of the strings in the set in the same order in which they were added.

func (*OrderedStringSet) Size Uses

func (os *OrderedStringSet) Size() int

Size implements StringSet.Size

type ParamValueInjector Uses

type ParamValueInjector struct {
}

ParamValueInjector takes a series of key/value (string/string) parameters and tries to inject them into the fields on a target struct

func (*ParamValueInjector) BindValueToField Uses

func (pb *ParamValueInjector) BindValueToField(paramName string, fieldName string, p *Params, t interface{}, errorFn GenerateMappingError, index ...int) error

BindValueToField attempts to take a named parameter from the supplied set of parameters and inject it into a field on the supplied target, converting to the correct data type as it goes.

type Params Uses

type Params struct {
    // contains filtered or unexported fields
}

Params is an abstraction of the HTTP query parameters or path parameters with type-safe accessors.

func NewParams Uses

func NewParams(v url.Values, pn []string) *Params

NewParams creates a Params with the supplied contents

func NewSingleValueParams Uses

func NewSingleValueParams(name string, value string) *Params

NewSingleValueParams creates a Params with only one value in it

func (*Params) BoolValue Uses

func (wp *Params) BoolValue(key string) (bool, error)

BoolValue returns the bool representation of the specified parameter (using Go's bool conversion rules) or an error if no value exists for that parameter.

func (*Params) Exists Uses

func (wp *Params) Exists(key string) bool

Exists returns true if a parameter with the supplied name exists, even if that parameter's value is an empty string.

func (*Params) FloatNValue Uses

func (wp *Params) FloatNValue(key string, bits int) (float64, error)

FloatNValue returns a float representation of the specified parameter with the specified bit size, or an error if no value exists for that parameter or if the value could not be converted to a float.

func (*Params) IntNInterfaceValue Uses

func (wp *Params) IntNInterfaceValue(i64 int64, bits int) interface{}

IntNInterfaceValue returns a signed int representation of the specified parameter as the correct basic type (int8, int32 etc), or an error if no value exists for that parameter or if the value could not be converted to an int.

func (*Params) IntNValue Uses

func (wp *Params) IntNValue(key string, bits int) (int64, error)

IntNValue returns a signed int representation of the specified parameter with the specified bit size, or an error if no value exists for that parameter or if the value could not be converted to an int.

func (*Params) MultipleValues Uses

func (wp *Params) MultipleValues(key string) bool

MultipleValues returns true if the parameter with the supplied name was set more than once (allowed for HTTP query parameters).

func (*Params) NotEmpty Uses

func (wp *Params) NotEmpty(key string) bool

NotEmpty returns true if a parameter with the supplied name exists and has a non-empty string representation.

func (*Params) ParamNames Uses

func (wp *Params) ParamNames() []string

ParamNames returns the names of all of the parameters stored

func (*Params) StringValue Uses

func (wp *Params) StringValue(key string) (string, error)

StringValue returns the string representation of the specified parameter or an error if no value exists for that parameter.

func (*Params) UIntNInterfaceValue Uses

func (wp *Params) UIntNInterfaceValue(u64 uint64, bits int) interface{}

UIntNInterfaceValue returns an unsigned int representation of the specified parameter as the correct basic type (uint8, uint32 etc), or an error if no value exists for that parameter or if the value could not be converted to an int.

func (*Params) UIntNValue Uses

func (wp *Params) UIntNValue(key string, bits int) (uint64, error)

UIntNValue returns an unsigned int representation of the specified parameter with the specified bit size, or an error if no value exists for that parameter or if the value could not be converted to an unsigned int.

type StringSet Uses

type StringSet interface {
    // Contains returns true if the set contains the supplied string
    Contains(m string) bool

    // Add adds the supplied string to the set. If the set already contains the supplied value, it is ignored.
    Add(s string)

    // Contents returns the members of the set as a string slice
    Contents() []string

    // Size returns the number of members of the set.
    Size() int

    // AddAll adds all the members of the supplied set to this set.
    AddAll(os StringSet)
}

StringSet defines common behaviour for an ordered or unordered set of strings.

type UnorderedStringSet Uses

type UnorderedStringSet struct {
    // contains filtered or unexported fields
}

An UnorderedStringSet is a set of strings where the order in which the strings were added to the set is not recorded.

This type is not goroutine safe and not recommended for the storage large number of strings.

func NewEmptyUnorderedStringSet Uses

func NewEmptyUnorderedStringSet() *UnorderedStringSet

NewEmptyUnorderedStringSet creates an empty UnorderedStringSet

func NewUnorderedStringSet Uses

func NewUnorderedStringSet(m []string) *UnorderedStringSet

NewUnorderedStringSet creats a new UnorderedStringSet seeded with the supplied strings.

func (*UnorderedStringSet) Add Uses

func (us *UnorderedStringSet) Add(s string)

Add implements StringSet.Add

func (*UnorderedStringSet) AddAll Uses

func (us *UnorderedStringSet) AddAll(ss StringSet)

AddAll implements StringSet.AddAll

func (*UnorderedStringSet) Contains Uses

func (us *UnorderedStringSet) Contains(m string) bool

Contains implements StringSet.Contains

func (*UnorderedStringSet) Contents Uses

func (us *UnorderedStringSet) Contents() []string

Contents returns all of the strings contained in this set in a nondeterministic order

func (*UnorderedStringSet) Size Uses

func (us *UnorderedStringSet) Size() int

Size implements StringSet.Size

Package types imports 8 packages (graph). Updated 2020-05-30. Refresh now. Tools for package owners.