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 sets.go

type Nilable Uses

type Nilable interface {

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

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

    // Whether or not the value in this type was explicitly set
    IsSet() bool
}

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
}

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

Create a new NilableBool with the supplied value.

func (*NilableBool) Bool Uses

func (nb *NilableBool) Bool() bool

The currently stored value (whether or not it has been explicitly set).

func (*NilableBool) IsSet Uses

func (nb *NilableBool) IsSet() bool

See Nilable.IsSet

func (*NilableBool) MarshalJSON Uses

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

See 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

See Nilable.UnmarshalJSON

type NilableFloat64 Uses

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

An 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

Create a new NilableFloat64 with the supplied value.

func (*NilableFloat64) Float64 Uses

func (ni *NilableFloat64) Float64() float64

The currently stored value (whether or not it has been explicitly set).

func (*NilableFloat64) IsSet Uses

func (nb *NilableFloat64) IsSet() bool

See Nilable.IsSet

func (*NilableFloat64) MarshalJSON Uses

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

See 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

See Nilable.UnmarshalJSON

type NilableInt64 Uses

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

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

Create a new NilableInt64 with the supplied value.

func (*NilableInt64) Int64 Uses

func (ni *NilableInt64) Int64() int64

The currently stored value (whether or not it has been explicitly set).

func (*NilableInt64) IsSet Uses

func (nb *NilableInt64) IsSet() bool

See Nilable.IsSet

func (*NilableInt64) MarshalJSON Uses

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

See 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

See Nilable.UnmarshalJSON

type NilableString Uses

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

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

Create a new NilableString with the supplied value.

func (*NilableString) IsSet Uses

func (ns *NilableString) IsSet() bool

See Nilable.IsSet

func (*NilableString) MarshalJSON Uses

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

See 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

The currently stored value (whether or not it has been explicitly set).

func (*NilableString) UnmarshalJSON Uses

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

See Nilable.UnmarshalJSON

type OrderedStringSet Uses

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

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

func NewEmptyOrderedStringSet Uses

func NewEmptyOrderedStringSet() *OrderedStringSet

An empty OrderedStringSet

func NewOrderedStringSet Uses

func NewOrderedStringSet(m []string) *OrderedStringSet

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)

See StringSet.Enqueue

func (*OrderedStringSet) AddAll Uses

func (os *OrderedStringSet) AddAll(ss StringSet)

See StringSet.AddAll

func (*OrderedStringSet) Contains Uses

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

See 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

See StringSet.Size

type StringSet Uses

type StringSet interface {
    // Whether or not the set contains the supplied string.
    Contains(m string) bool

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

    // The members of the set a string slice.
    Contents() []string

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

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

Common behaviour for an ordered or unordered set of strings.

type UnorderedStringSet Uses

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

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

An empty UnorderedStringSet

func NewUnorderedStringSet Uses

func NewUnorderedStringSet(m []string) *UnorderedStringSet

A new UnorderedStringSet seeded with the supplied strings.

func (*UnorderedStringSet) Add Uses

func (ss *UnorderedStringSet) Add(s string)

See StringSet.Enqueue

func (*UnorderedStringSet) AddAll Uses

func (us *UnorderedStringSet) AddAll(ss StringSet)

See StringSet.AddAll

func (*UnorderedStringSet) Contains Uses

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

See StringSet.Contains

func (*UnorderedStringSet) Contents Uses

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

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

func (*UnorderedStringSet) Size Uses

func (ss *UnorderedStringSet) Size() int

See StringSet.Size

Package types imports 4 packages (graph) and is imported by 9 packages. Updated 2018-08-06. Refresh now. Tools for package owners.