types

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2017 License: Apache-2.0 Imports: 4 Imported by: 9

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Nilable

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

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

func NewNilableBool(b bool) *NilableBool

Create a new NilableBool with the supplied value.

func (*NilableBool) Bool

func (nb *NilableBool) Bool() bool

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

func (*NilableBool) IsSet

func (nb *NilableBool) IsSet() bool

See Nilable.IsSet

func (*NilableBool) MarshalJSON

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

See 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

See Nilable.UnmarshalJSON

type NilableFloat64

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

func NewNilableFloat64(f float64) *NilableFloat64

Create a new NilableFloat64 with the supplied value.

func (*NilableFloat64) Float64

func (ni *NilableFloat64) Float64() float64

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

func (*NilableFloat64) IsSet

func (nb *NilableFloat64) IsSet() bool

See Nilable.IsSet

func (*NilableFloat64) MarshalJSON

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

See 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

See Nilable.UnmarshalJSON

type NilableInt64

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

func NewNilableInt64(i int64) *NilableInt64

Create a new NilableInt64 with the supplied value.

func (*NilableInt64) Int64

func (ni *NilableInt64) Int64() int64

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

func (*NilableInt64) IsSet

func (nb *NilableInt64) IsSet() bool

See Nilable.IsSet

func (*NilableInt64) MarshalJSON

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

See 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

See Nilable.UnmarshalJSON

type NilableString

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

func NewNilableString(v string) *NilableString

Create a new NilableString with the supplied value.

func (*NilableString) IsSet

func (ns *NilableString) IsSet() bool

See Nilable.IsSet

func (*NilableString) MarshalJSON

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

See 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

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

func (*NilableString) UnmarshalJSON

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

See Nilable.UnmarshalJSON

type OrderedStringSet

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

func NewEmptyOrderedStringSet() *OrderedStringSet

An empty OrderedStringSet

func NewOrderedStringSet

func NewOrderedStringSet(m []string) *OrderedStringSet

An OrderedStringSet with the supplied strings added to the new set in the provided order.

func (*OrderedStringSet) Add

func (os *OrderedStringSet) Add(s string)

See StringSet.Add

func (*OrderedStringSet) AddAll

func (os *OrderedStringSet) AddAll(ss StringSet)

See StringSet.AddAll

func (*OrderedStringSet) Contains

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

See 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

See StringSet.Size

type StringSet

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

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 NewUnorderedStringSet

func NewUnorderedStringSet(m []string) *UnorderedStringSet

A new UnorderedStringSet seeded with the supplied strings.

func (*UnorderedStringSet) Add

func (ss *UnorderedStringSet) Add(s string)

See StringSet.Add

func (*UnorderedStringSet) AddAll

func (us *UnorderedStringSet) AddAll(ss StringSet)

See StringSet.AddAll

func (*UnorderedStringSet) Contains

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

See StringSet.Contains

func (*UnorderedStringSet) Contents

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

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

func (*UnorderedStringSet) Size

func (ss *UnorderedStringSet) Size() int

See StringSet.Size

Jump to

Keyboard shortcuts

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