Documentation ¶
Overview ¶
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 ¶
- type FieldAssociatedError
- type GenerateMappingError
- type Nilable
- type NilableBool
- type NilableFloat64
- type NilableInt64
- type NilableString
- type OrderedStringSet
- type ParamValueInjector
- type Params
- func (wp *Params) BoolValue(key string) (bool, error)
- func (wp *Params) Exists(key string) bool
- func (wp *Params) FloatNValue(key string, bits int) (float64, error)
- func (wp *Params) IntNInterfaceValue(i64 int64, bits int) interface{}
- func (wp *Params) IntNValue(key string, bits int) (int64, error)
- func (wp *Params) MultipleValues(key string) bool
- func (wp *Params) NotEmpty(key string) bool
- func (wp *Params) ParamNames() []string
- func (wp *Params) StringValue(key string) (string, error)
- func (wp *Params) UIntNInterfaceValue(u64 uint64, bits int) interface{}
- func (wp *Params) UIntNValue(key string, bits int) (uint64, error)
- type StringSet
- type UnorderedStringSet
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FieldAssociatedError ¶
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 ¶
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 ¶
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 ¶
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 ¶
func NewNilableBool(b bool) *NilableBool
NewNilableBool creates a new NilableBool with the supplied value.
func (*NilableBool) Bool ¶
func (nb *NilableBool) Bool() bool
Bool returns the currently stored value (whether or not it has been explicitly set).
func (*NilableBool) MarshalJSON ¶
func (nb *NilableBool) MarshalJSON() ([]byte, error)
MarshalJSON implements Nilable.MarshalJSON
func (*NilableBool) Set ¶
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 ¶
func (nb *NilableBool) UnmarshalJSON(b []byte) error
UnmarshalJSON implements Nilable.UnmarshalJSON
type NilableFloat64 ¶
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 ¶
func NewNilableFloat64(f float64) *NilableFloat64
NewNilableFloat64 creates a new NilableFloat64 with the supplied value.
func (*NilableFloat64) Float64 ¶
func (nf *NilableFloat64) Float64() float64
Float64 returns the currently stored value (whether or not it has been explicitly set).
func (*NilableFloat64) IsSet ¶
func (nf *NilableFloat64) IsSet() bool
IsSet implements Nilable.IsSet
func (*NilableFloat64) MarshalJSON ¶
func (nf *NilableFloat64) MarshalJSON() ([]byte, error)
MarshalJSON implements Nilable.MarshalJSON
func (*NilableFloat64) Set ¶
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 ¶
func (nf *NilableFloat64) UnmarshalJSON(b []byte) error
UnmarshalJSON implements Nilable.UnmarshalJSON
type NilableInt64 ¶
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 ¶
func NewNilableInt64(i int64) *NilableInt64
NewNilableInt64 creates a new NilableInt64 with the supplied value.
func (*NilableInt64) Int64 ¶
func (ni *NilableInt64) Int64() int64
Int64 returns the currently stored value (whether or not it has been explicitly set).
func (*NilableInt64) MarshalJSON ¶
func (ni *NilableInt64) MarshalJSON() ([]byte, error)
MarshalJSON implements Nilable.MarshalJSON
func (*NilableInt64) Set ¶
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 ¶
func (ni *NilableInt64) UnmarshalJSON(b []byte) error
UnmarshalJSON implements Nilable.UnmarshalJSON
type NilableString ¶
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 ¶
func NewNilableString(v string) *NilableString
NewNilableString creates a new NilableString with the supplied value.
func (*NilableString) MarshalJSON ¶
func (ns *NilableString) MarshalJSON() ([]byte, error)
MarshalJSON implements Nilable.MarshalJSON
func (*NilableString) Set ¶
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 ¶
func (ns *NilableString) String() string
String returns the currently stored value (whether or not it has been explicitly set).
func (*NilableString) UnmarshalJSON ¶
func (ns *NilableString) UnmarshalJSON(b []byte) error
UnmarshalJSON implements Nilable.UnmarshalJSON
type OrderedStringSet ¶
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 ¶
func NewEmptyOrderedStringSet() *OrderedStringSet
NewEmptyOrderedStringSet creates an empty OrderedStringSet
func NewOrderedStringSet ¶
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 ¶
func (os *OrderedStringSet) Add(s string)
Add implements StringSet.Add
func (*OrderedStringSet) AddAll ¶
func (os *OrderedStringSet) AddAll(ss StringSet)
AddAll implements StringSet.AddAll
func (*OrderedStringSet) Contains ¶
func (os *OrderedStringSet) Contains(m string) bool
Contains implements StringSet.Contains
func (*OrderedStringSet) Contents ¶
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 ¶
func (os *OrderedStringSet) Size() int
Size implements StringSet.Size
type ParamValueInjector ¶
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 ¶
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 ¶
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 NewSingleValueParams ¶
NewSingleValueParams creates a Params with only one value in it
func (*Params) BoolValue ¶
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 ¶
Exists returns true if a parameter with the supplied name exists, even if that parameter's value is an empty string.
func (*Params) FloatNValue ¶
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 ¶ added in v2.2.0
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 ¶
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 ¶
MultipleValues returns true if the parameter with the supplied name was set more than once (allowed for HTTP query parameters).
func (*Params) NotEmpty ¶
NotEmpty returns true if a parameter with the supplied name exists and has a non-empty string representation.
func (*Params) ParamNames ¶
ParamNames returns the names of all of the parameters stored
func (*Params) StringValue ¶
StringValue returns the string representation of the specified parameter or an error if no value exists for that parameter.
func (*Params) UIntNInterfaceValue ¶ added in v2.2.0
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 ¶
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 ¶
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 ¶
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 ¶
func NewEmptyUnorderedStringSet() *UnorderedStringSet
NewEmptyUnorderedStringSet creates an empty UnorderedStringSet
func NewUnorderedStringSet ¶
func NewUnorderedStringSet(m []string) *UnorderedStringSet
NewUnorderedStringSet creats a new UnorderedStringSet seeded with the supplied strings.
func (*UnorderedStringSet) Add ¶
func (us *UnorderedStringSet) Add(s string)
Add implements StringSet.Add
func (*UnorderedStringSet) AddAll ¶
func (us *UnorderedStringSet) AddAll(ss StringSet)
AddAll implements StringSet.AddAll
func (*UnorderedStringSet) Contains ¶
func (us *UnorderedStringSet) Contains(m string) bool
Contains implements StringSet.Contains
func (*UnorderedStringSet) Contents ¶
func (us *UnorderedStringSet) Contents() []string
Contents returns all of the strings contained in this set in a nondeterministic order
func (*UnorderedStringSet) Size ¶
func (us *UnorderedStringSet) Size() int
Size implements StringSet.Size