null

package module
v0.0.0-...-e99255a Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2019 License: MIT Imports: 7 Imported by: 0

README

null

Build Status Code Coverage Go Report Card GoDoc

import "github.com/farsil/null"

null is a Go package which aims to provide a reasonable way to deal with undefined values. It defines wrapper types that hold a boolean flag to specify whether the wrapped type is valid or not. The validity flag is used during marshaling (unmarshaling) to decide whether an undefined value should be marshaled (unmarshaled) in lieu of the underlying value. The undefined value varies according to the specific serialization interface:

  • With JSON, the undefined value is the JSON string null
  • With SQL, the undefined value is the SQL value NULL
  • With flag, the undefined value is ""
  • With the default text encoding, the undefined value is an empty []byte

Types in the package implement all the necessary interfaces to be used with the flag package, to handle JSON and SQL NULL values, so it can be used in place of the sql.Nullxxx types. The interfaces implemented by each type are:

  • TextMarshaler from encoding
  • TextUnmarshaler from encoding
  • Marshaler from encoding/json
  • Unmarshaler from encoding/json
  • Value from flag
  • Scanner from database/sql
  • Valuer from database/sql/driver
  • Stringer from fmt (as part of the Value interface from flag)

Installation

Installation is as simple as invoking:

$ go get github.com/farsil/null

Data Types

The package defines the following data types:

  • null.String which wraps a string
  • null.Bool which wraps a bool
  • null.Int which wraps an int
  • null.Uint which wraps an uint
  • null.Float64 which wraps a float64
  • null.Time which wraps a time.Time

Note that JSON does not define a standard datetime representation. In this package, a null.Time object is represented as an RFC3339 string when a null.Time object is marshaled, and an RFC3339 string is parsed when unmarshaling from JSON.

Example

package main

import "github.com/farsil/null"

var data = []byte(`{
    "title": "The foo does a bar"
}`)

type BookTitle struct {
	Title    null.String `json:"title"`
	Subtitle null.String `json:"subtitle"`
}

func main() {
	var t Title
	if err := json.Unmarshal(data, &t); err != nil {
		// process error
	}

	if !t.Title.Valid {
		fmt.Println("Book has no title")
		return
	}

	fmt.Println("Book title:", t.Title.Str)

	if t.Subtitle.Valid {
		fmt.Println("Book subtitle:", t.Subtitle.Str)
	} else {
		fmt.Println("Book has no subtitle")
	}
}

Output

Book title: 'The foo does a bar'
Book has no subtitle

Documentation

The documentation is available at the address https://godoc.org/github.com/farsil/null.

Credits

This repository is heavily inspired by https://github.com/guregu/null. I originally wanted to create a fork of that repository, but since there are critical incompatibilities of interfaces and behavior, I decided to start from scratch.

Documentation

Overview

Package null provides nullable types that are conscious of undefined values when marshaling or unmarshaling.

JSON

Nullable types in this package can be marshaled to (unmarshaled from) JSON, as they all implement the json.Marshaler and json.Unmarshaler interfaces.

var json := struct {
    Mandatory   string      `json:"mandatory"`
    Optional    null.String `json:"optional"`
}{
    Mandatory: "foo",
    Optional:  bar,
}

In the example, bar is a String. If bar is valid, the resulting JSON is going to be:

{
    "mandatory": "foo",
    "optional": <bar.Str>
}

otherwise:

{
    "mandatory": "foo",
    "optional": null
}

Unmarshaling from JSON works the other way around: JSON value null is converted to an invalid nullable, otherwise a compatible JSON type is unmarshaled into the appropriate nullable.

JSON and the omitempty struct tag

Nullable types in this package offer a Ptr() method that is useful to deal with the omitempty struct tag:

var json := struct {
    Mandatory  string  `json:"mandatory"`
    Optional   *string `json:"optional,omitempty"`
}{
    Mandatory: "foo",
    Optional:  bar.Ptr(),
}

In the example, bar is a String. If bar is valid, Ptr() returns a pointer to the underlying value, otherwise returns nil. json.Marshal will recognize nil pointers as empty values, omitting the associated name from the JSON output.

SQL

Nullable types in this package recognize SQL NULL values and implement the driver.Valuer and sql.Scanner interfaces. Suppose we have the following table in our database:

CREATE TABLE example (
    mandatory varchar(50) primary key not null,
    optional  varchar(50) default null
);

We may use the following struct that matches the table struct:

 var sql := struct {
     Mandatory   string      `db:"mandatory"`
     Optional    null.String `db:"optional"`
 }{
     Mandatory:  "foo",
     Optional:   bar,
}

In the example, bar is a String. If sql is inserted into the database, and bar is not valid, a SELECT query will return:

+-----------+-----------+
| mandatory | optional  |
+-----------+-----------+
| foo       | <bar.Str> |
+-----------+-----------+

otherwise:

+-----------+----------+
| mandatory | optional |
+-----------+----------+
| foo       | NULL     |
+-----------+----------+

It is also possible to scan values from the database. In that case, if the scanned value corresponds to an SQL NULL, an invalid nullable is initialized, otherwise, a compatible SQL value is scanned into the appropriate nullable.

Package flag

Nullable types may also receive values from the command line via the flag package, as they implement the flag.Value interface:

var bar null.String

func init() {
    flag.Var(&bar, "bar", "holds a bar")
}

func main() {
    flag.Parse()
    // now bar can be used
}

bar will be invalid if the command line option "-bar" is not passed or is empty, otherwise it will be valid and it will hold the content of "-bar".

Index

Constants

View Source
const (
	// InvalidNullableString is returned by String methods when the nullable is
	// not valid.
	InvalidNullableString = "<invalid>"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	// Bool holds the underlying bool value.
	Bool bool

	// Valid holds the validity flag. If true, the underlying value is valid.
	// If false, it is invalid, and thus meaningless.
	Valid bool
}

Bool implements a nullable bool.

func BoolFrom

func BoolFrom(v bool) Bool

BoolFrom creates a valid Bool from v.

func BoolFromPtr

func BoolFromPtr(p *bool) Bool

BoolFromPtr creates a Bool from pointer p. If p is nil, the returned Bool is invalid.

func BoolFromZero

func BoolFromZero(v bool) Bool

BoolFromZero creates a Bool from v. If v is false, the returned Bool is invalid.

func (*Bool) From

func (b *Bool) From(v bool)

From sets the underlying value of b to v. b becomes valid.

func (*Bool) FromPtr

func (b *Bool) FromPtr(p *bool)

FromPtr invalidates b if p is nil, otherwise it sets the underlying value of b to the value pointed to by p, and b becomes valid.

func (*Bool) FromZero

func (b *Bool) FromZero(v bool)

FromZero invalidates b if v is false, otherwise it sets the underlying value of b to v, and b becomes valid.

func (Bool) MarshalJSON

func (b Bool) MarshalJSON() (data []byte, err error)

MarshalJSON encodes the underlying value of b to a JSON boolean if b is valid, otherwise it returns the JSON null value. err is always nil.

func (Bool) MarshalText

func (b Bool) MarshalText() (data []byte, err error)

MarshalText marshals b to a byte string representation. If b is valid, it marshals the underlying value of b to either "true" or "false", otherwise it returns nil. err is always nil.

func (Bool) Ptr

func (b Bool) Ptr() *bool

Ptr returns a pointer to the underlying value of b if b is valid, otherwise returns nil.

func (*Bool) Scan

func (b *Bool) Scan(obj interface{}) error

Scan assigns a value from a database driver. If obj's type is bool, b becomes valid, and the underlying value of b becomes the value of obj. If obj is nil, b becomes invalid. If obj's type is any other type, b becomes invalid, and a TypeError is returned.

func (*Bool) Set

func (b *Bool) Set(str string) error

Set invalidates b if str is the empty string, otherwise it parses str into the underlying value of b, and b becomes valid. If str is not a recognized boolean string, b becomes invalid and a ParseError is returned. Recognized boolean strings are 1, true, True, TRUE, T, t, 0, false, False, FALSE, f, F.

func (Bool) String

func (b Bool) String() string

String returns a string representation of b. If b is valid, it returns either "true" or "false", otherwise it returns InvalidNullableString.

func (*Bool) UnmarshalJSON

func (b *Bool) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals from a JSON encoded byte string to b. If the encoded JSON data represent the JSON null value, or an error is produced, b becomes invalid. If the encoded JSON data represent a JSON boolean, b becomes valid, and the underlying value of b is set to the JSON boolean. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.

func (*Bool) UnmarshalText

func (b *Bool) UnmarshalText(text []byte) error

UnmarshalText unmarshals from a byte string to b. It behaves like Set, except that it returns an UnmarshalError instead of a ParseError in case text cannot be parsed.

func (Bool) Value

func (b Bool) Value() (v driver.Value, err error)

Value returns the underlying value of b if b is valid, otherwise nil. err is always nil.

func (Bool) Zero

func (b Bool) Zero() bool

Zero returns the underlying value of b if b is valid, otherwise returns false.

type ConversionError

type ConversionError struct {

	// SrcValue holds the value that couldn't be converted
	SrcValue interface{}

	// SrcType holds the name of SrcValue's type
	SrcType string

	// DestType holds the destination type
	DestType string
	// contains filtered or unexported fields
}

ConversionError is returned when a type conversion cannot happen without data loss.

func (ConversionError) Error

func (e ConversionError) Error() string

Error returns a string representation of e.

type Float64

type Float64 struct {
	// Float64 holds the underlying float64 value.
	Float64 float64

	// Valid holds the validity flag. If true, the underlying value is valid.
	// If false, it is invalid, and thus meaningless.
	Valid bool
}

Float64 implements a nullable float64.

func Float64From

func Float64From(v float64) Float64

Float64From creates a valid Float64 from v.

func Float64FromPtr

func Float64FromPtr(p *float64) Float64

Float64FromPtr creates a Float64 from pointer p. If p is nil, the returned Float64 is invalid.

func Float64FromZero

func Float64FromZero(v float64) Float64

Float64FromZero creates a Float64 from v. If v is 0, the returned Float64 is invalid.

func (*Float64) From

func (f *Float64) From(v float64)

From sets the underlying value of f to v. f becomes valid.

func (*Float64) FromPtr

func (f *Float64) FromPtr(p *float64)

FromPtr invalidates f if p is nil, otherwise it sets the underlying value of f to the value pointed to by p, and f becomes valid.

func (*Float64) FromZero

func (f *Float64) FromZero(v float64)

FromZero invalidates f if v is false, otherwise it sets the underlying value of f to v, and f becomes valid.

func (Float64) MarshalJSON

func (f Float64) MarshalJSON() (data []byte, err error)

MarshalJSON encodes the underlying value of f to a JSON number if f is valid, otherwise it returns the JSON null value. err is always nil.

func (Float64) MarshalText

func (f Float64) MarshalText() (data []byte, err error)

MarshalText marshals f to a byte string representation. If f is valid, it marshals the underlying value of f to a byte string representation, otherwise it returns nil. err is always nil.

func (Float64) Ptr

func (f Float64) Ptr() *float64

Ptr returns a pointer to the underlying value of f if f is valid, otherwise returns nil.

func (*Float64) Scan

func (f *Float64) Scan(obj interface{}) error

Scan assigns a value from a database driver. If obj's type is float64, f becomes valid, and the underlying value of f becomes the value of obj. If obj is nil, f becomes invalid. If obj's type is any other type, f becomes invalid, and a TypeError is returned.

func (*Float64) Set

func (f *Float64) Set(str string) error

Set invalidates f if str is the empty string, otherwise it parses str into the underlying value of f, and f becomes valid. If str is not a valid string representation of a floating point number, f becomes invalid and a ParseError is returned.

func (Float64) String

func (f Float64) String() string

String returns a string representation of f. If f is valid, it returns a string representation of the underlying value of f, otherwise it returns InvalidNullableString.

func (*Float64) UnmarshalJSON

func (f *Float64) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals from a JSON encoded byte string to f. If the encoded JSON data represent the JSON null value, or an error is produced, f becomes invalid. If the encoded JSON data represent a JSON number, f becomes valid, and the underlying value of f is set to the JSON number. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.

func (*Float64) UnmarshalText

func (f *Float64) UnmarshalText(text []byte) error

UnmarshalText unmarshals from a byte string to f. It behaves like Set, except that it returns an UnmarshalError instead of a ParseError in case text cannot be parsed.

func (Float64) Value

func (f Float64) Value() (v driver.Value, err error)

Value returns the underlying value of f if f is valid, otherwise nil. err is always nil.

func (Float64) Zero

func (f Float64) Zero() float64

Zero returns the underlying value of f if f is valid, otherwise returns 0.

type Int

type Int struct {
	// Int holds the underlying int value.
	Int int

	// Valid holds the validity flag. If true, the underlying value is valid.
	// If false, it is invalid, and thus meaningless.
	Valid bool
}

Int implements a nullable int.

func IntFrom

func IntFrom(v int) Int

IntFrom creates a valid Int from v.

func IntFromPtr

func IntFromPtr(p *int) Int

IntFromPtr creates an Int from pointer p. If p is nil, the returned Int is invalid.

func IntFromZero

func IntFromZero(v int) Int

IntFromZero creates an Int from v. If v is 0, the returned Int is invalid.

func (*Int) From

func (i *Int) From(v int)

From sets the underlying value of i to v. i becomes valid.

func (*Int) FromPtr

func (i *Int) FromPtr(p *int)

FromPtr invalidates i if p is nil, otherwise it sets the underlying value of i to the value pointed to by p, and i becomes valid.

func (*Int) FromZero

func (i *Int) FromZero(v int)

FromZero invalidates i if v is false, otherwise it sets the underlying value of i to v, and i becomes valid.

func (Int) MarshalJSON

func (i Int) MarshalJSON() (data []byte, err error)

MarshalJSON encodes the underlying value of i to a JSON number if i is valid, otherwise it returns the JSON null value. err is always nil.

func (Int) MarshalText

func (i Int) MarshalText() (data []byte, err error)

MarshalText marshals i to a byte string representation. If i is valid, it marshals the underlying value of i to a byte string representation, otherwise it returns nil. err is always nil.

func (Int) Ptr

func (i Int) Ptr() *int

Ptr returns a pointer to the underlying value of i if i is valid, otherwise returns nil.

func (*Int) Scan

func (i *Int) Scan(obj interface{}) error

Scan assigns a value from a database driver. If obj's type is int64, if the number can be stored in an int without data loss, i becomes valid, and the underlying value of i becomes the value of obj. If obj's type is int64, and the number cannot be stored in an int without data loss, i becomes invalid, and a ConversionError is returned. If obj is nil, i becomes invalid. If obj's type is any other type, i becomes invalid, and a TypeError is returned.

func (*Int) Set

func (i *Int) Set(str string) error

Set invalidates i if str is the empty string, otherwise it parses str into the underlying value of i, and i becomes valid. If str is not a valid string representation of an integer, or if the represented integer is too large to be stored in an int, i becomes invalid and a ParseError is returned.

func (Int) String

func (i Int) String() string

String returns a string representation of i. If i is valid, it returns a string representation of the underlying value of i, otherwise it returns InvalidNullableString.

func (*Int) UnmarshalJSON

func (i *Int) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals from a JSON encoded byte string to i. If the encoded JSON data represent the JSON null value, or an error is produced, i becomes invalid. If the encoded JSON data represent a JSON number, and can be stored in an int without data loss, i becomes valid, and the underlying value of i is set to the JSON number. If the encoded JSON data represent a JSON number, and cannot be stored in an int without data loss, i becomes invalid, and a ConversionError is returned. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.

func (*Int) UnmarshalText

func (i *Int) UnmarshalText(text []byte) error

UnmarshalText unmarshals from a byte string to i. It behaves like Set, except that it returns an UnmarshalError instead of a ParseError in case text cannot be parsed.

func (Int) Value

func (i Int) Value() (v driver.Value, err error)

Value returns the underlying value of i converted to int64 if i is valid, otherwise nil. err is always nil.

func (Int) Zero

func (i Int) Zero() int

Zero returns the underlying value of i if i is valid, otherwise returns 0.

type MarshalError

type MarshalError struct {

	// SrcValue holds the value that couldn't be marshaled
	SrcValue interface{}

	// SrcType holds the name of SrcValue's type
	SrcType string
	// contains filtered or unexported fields
}

MarshalError is returned if marshaling is not successful.

func (MarshalError) Error

func (e MarshalError) Error() string

Error returns a string representation of e.

type ParseError

type ParseError struct {

	// SrcString holds the string that couldn't be parsed
	SrcString string

	// DestType holds the destination type
	DestType string
	// contains filtered or unexported fields
}

ParseError is returned when a string cannot be parsed.

func (ParseError) Error

func (e ParseError) Error() string

Error returns a string representation of e.

type String

type String struct {
	// Str holds the underlying string value. The field can't be named String
	// because it conflicts with the flag.Value interface implementation.
	Str string

	// Valid holds the validity flag. If true, the underlying value is valid.
	// If false, it is invalid, and thus meaningless.
	Valid bool
}

String implements a nullable string.

func StringFrom

func StringFrom(v string) String

StringFrom creates a valid String from v.

func StringFromPtr

func StringFromPtr(p *string) String

StringFromPtr creates a String from pointer p. If p is nil, the returned String is invalid.

func StringFromZero

func StringFromZero(v string) String

StringFromZero creates a String from v. If v is the empty string, the returned String is invalid.

func (*String) From

func (s *String) From(v string)

From sets the underlying value of s to v. s becomes valid.

func (*String) FromPtr

func (s *String) FromPtr(p *string)

FromPtr invalidates s if p is nil, otherwise it sets the underlying value of s to the value pointed to by p, and s becomes valid.

func (*String) FromZero

func (s *String) FromZero(v string)

FromZero invalidates s if v is the empty string, otherwise it sets the underlying value of s to v, and s becomes valid.

func (String) MarshalJSON

func (s String) MarshalJSON() (data []byte, err error)

MarshalJSON encodes the underlying value of s to a JSON string if s is valid, otherwise it returns the JSON null value. err is always nil.

func (String) MarshalText

func (s String) MarshalText() (data []byte, err error)

MarshalText converts the underlying value of s to []byte if s is valid, and returns nil if not valid. err is always nil.

func (String) Ptr

func (s String) Ptr() *string

Ptr returns a pointer to the underlying value of s if s is valid, otherwise returns nil.

func (*String) Scan

func (s *String) Scan(obj interface{}) error

Scan assigns a value from a database driver. If obj's type is string, s becomes valid, and the underlying value of s becomes the value of obj. If obj is nil, s becomes invalid. If obj's type is any other type, s becomes invalid, and a TypeError is returned.

func (*String) Set

func (s *String) Set(str string) error

Set invalidates s if str is the empty string, otherwise it sets the underlying value of s to str, and s becomes valid. This function always returns nil.

func (String) String

func (s String) String() string

String returns the underlying value of s if s is valid, and InvalidNullableString if not valid.

func (*String) UnmarshalJSON

func (s *String) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals from a JSON encoded byte string to s. If the encoded JSON data represent the JSON null value, or an error is produced, s becomes invalid. If the encoded JSON data represent a JSON string, s becomes valid, and the underlying value of s is set to the JSON string. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.

func (*String) UnmarshalText

func (s *String) UnmarshalText(text []byte) error

UnmarshalText unmarshals from a byte string to s. If the byte string is nil, s becomes invalid, otherwise the underlying value of s is set to the converted byte string and s becomes valid. The returned error is always nil.

func (String) Value

func (s String) Value() (v driver.Value, err error)

Value returns the underlying value of s if s is valid, otherwise nil. err is always nil.

func (String) Zero

func (s String) Zero() string

Zero returns the underlying value of s if s is valid, otherwise returns an empty string.

type Time

type Time struct {
	// Time holds the underlying time.Time value.
	Time time.Time

	// Valid holds the validity flag. If true, the underlying value is valid.
	// If false, it is invalid, and thus meaningless.
	Valid bool
}

Time implements a nullable time.Time.

func TimeFrom

func TimeFrom(v time.Time) Time

TimeFrom creates a valid Time from v.

func TimeFromPtr

func TimeFromPtr(v *time.Time) Time

TimeFromPtr creates a Time from pointer p. If p is nil, the returned Time is invalid.

func TimeFromZero

func TimeFromZero(v time.Time) Time

TimeFromZero creates a Time from v. If v represents the zero time instant, the returned Time is invalid.

func (*Time) From

func (t *Time) From(v time.Time)

From sets the underlying value of t to v. t becomes valid.

func (*Time) FromPtr

func (t *Time) FromPtr(v *time.Time)

FromPtr invalidates t if p is nil, otherwise it sets the underlying value of t to the value pointed to by p, and t becomes valid.

func (*Time) FromZero

func (t *Time) FromZero(v time.Time)

FromZero invalidates t if v represents the zero time instant, otherwise it sets the underlying value of t to v, and t becomes valid.

func (Time) MarshalJSON

func (t Time) MarshalJSON() (data []byte, err error)

MarshalJSON encodes the underlying value of t to a JSON string representation if t is valid, otherwise it returns the JSON null value. The string representation is formatted according to the RFC3339 standard with nanoseconds. If the underlying value of t cannot be marshaled, a MarshalError is returned.

func (Time) MarshalText

func (t Time) MarshalText() (data []byte, err error)

MarshalText marshals t to a byte string representation. If t is valid, it formats the underlying value of t according to the RFC3339 standard with nanoseconds, otherwise it returns nil. If the underlying value of t cannot be marshaled, a MarshalError is returned.

func (Time) Ptr

func (t Time) Ptr() *time.Time

Ptr returns a pointer to the underlying value of t if t is valid, otherwise returns nil.

func (*Time) Scan

func (t *Time) Scan(obj interface{}) error

Scan assigns a value from a database driver. If obj's type is time.Time, t becomes valid, and the underlying value of t becomes the value of obj. If obj is nil, t becomes invalid. If obj's type is any other type, t becomes invalid, and a TypeError is returned.

func (*Time) Set

func (t *Time) Set(str string) error

Set invalidates t if str is the empty string, otherwise it parses str into the underlying value of t, and t becomes valid. If str is not a valid RFC3339 string, t becomes invalid and a ParseError is returned.

func (Time) String

func (t Time) String() string

String returns a string representation of t. If t is valid, it formats the underlying value of t according to the RFC3339 standard with nanoseconds. For time instants which year is beyond 10000, not allowed by the standard, String tries to print a meaningful datetime regardless. If t is not valid, it returns InvalidNullableString.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals from a JSON encoded byte string to t. If the encoded JSON data represent the JSON null value, or an error is produced, t becomes invalid. If the encoded JSON data represent a valid RFC3339 JSON string, t becomes valid, and the underlying value of t is set to the JSON string. If the encoded JSON data represent a JSON string, but not formatted according to the RFC3339 standard, a ParseError is returned. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.

func (*Time) UnmarshalText

func (t *Time) UnmarshalText(text []byte) error

UnmarshalText unmarshals from a byte string to t. It behaves like Set, except that it returns an UnmarshalError instead of a ParseError in case text cannot be parsed.

func (Time) Value

func (t Time) Value() (v driver.Value, err error)

Value returns the underlying value of t if t is valid, otherwise nil. err is always nil.

func (Time) Zero

func (t Time) Zero() time.Time

Zero returns the underlying value of t if t is valid, otherwise returns a time.Time representing the zero time instant.

type TypeError

type TypeError struct {

	// InvalidType holds the name of the type that is not valid
	InvalidType string

	// ExpectedTypes holds the names of accepted types
	ExpectedTypes []string
	// contains filtered or unexported fields
}

TypeError is returned when a value of the wrong type is supplied.

func (TypeError) Error

func (e TypeError) Error() string

Error returns a string representation of e.

type Uint

type Uint struct {
	// Uint holds the underlying uint value.
	Uint uint

	// Valid holds the validity flag. If true, the underlying value is valid.
	// If false, it is invalid, and thus meaningless.
	Valid bool
}

Uint implements a nullable uint.

func UintFrom

func UintFrom(v uint) Uint

UintFrom creates a valid Uint from v.

func UintFromPtr

func UintFromPtr(p *uint) Uint

UintFromPtr creates an Uint from pointer p. If p is nil, the returned Uint is invalid.

func UintFromZero

func UintFromZero(v uint) Uint

UintFromZero creates an Uint from v. If v is 0, the returned Uint is invalid.

func (*Uint) From

func (u *Uint) From(v uint)

From sets the underlying value of u to v. u becomes valid.

func (*Uint) FromPtr

func (u *Uint) FromPtr(p *uint)

FromPtr invalidates u if p is nil, otherwise it sets the underlying value of u to the value pointed to by p, and u becomes valid.

func (*Uint) FromZero

func (u *Uint) FromZero(v uint)

FromZero invalidates u if v is false, otherwise it sets the underlying value of u to v, and u becomes valid.

func (Uint) MarshalJSON

func (u Uint) MarshalJSON() (data []byte, err error)

MarshalJSON encodes the underlying value of u to a JSON number if u is valid, otherwise it returns the JSON null value. err is always nil.

func (Uint) MarshalText

func (u Uint) MarshalText() (data []byte, err error)

MarshalText marshals u to a byte string representation. If u is valid, it marshals the underlying value of u to a byte string representation, otherwise it returns nil. err is always nil.

func (Uint) Ptr

func (u Uint) Ptr() *uint

Ptr returns a pointer to the underlying value of u if u is valid, otherwise returns nil.

func (*Uint) Scan

func (u *Uint) Scan(obj interface{}) error

Scan assigns a value from a database driver. If obj's type is int64, if the number can be stored in an uint without data loss, u becomes valid, and the underlying value of u becomes the value of obj. If obj's type is int64, and the number cannot be stored in an uint without data loss, u becomes invalid, and a ConversionError is returned. If obj is nil, u becomes invalid. If obj's type is any other type, u becomes invalid, and a TypeError is returned.

func (*Uint) Set

func (u *Uint) Set(str string) error

Set invalidates u if str is the empty string, otherwise it parses str into the underlying value of u, and u becomes valid. If str is not a valid string representation of an unsigned integer, or if the represented integer is too large to be stored in an uint, u becomes invalid and a ParseError is returned.

func (Uint) String

func (u Uint) String() string

String returns a string representation of u. If u is valid, it returns a string representation of the underlying value of u, otherwise it returns InvalidNullableString.

func (*Uint) UnmarshalJSON

func (u *Uint) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals from a JSON encoded byte string to u. If the encoded JSON data represent the JSON null value, or an error is produced, u becomes invalid. If the encoded JSON data represent a JSON number, and can be stored in an uint without data loss, u becomes valid, and the underlying value of u is set to the JSON number. If the encoded JSON data represent a JSON number, and cannot be stored in an uint without data loss, u becomes invalid, and a ConversionError is returned. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.

func (*Uint) UnmarshalText

func (u *Uint) UnmarshalText(text []byte) error

UnmarshalText unmarshals from a byte string to u. It behaves like Set, except that it returns an UnmarshalError instead of a ParseError in case text cannot be parsed.

func (Uint) Value

func (u Uint) Value() (v driver.Value, err error)

Value returns the underlying value of u converted to int64 if u is valid, otherwise nil. If the conversion would cause data loss, a ConversionError is returned.

func (Uint) Zero

func (u Uint) Zero() uint

Zero returns the underlying value of u if u is valid, otherwise returns 0.

type UnmarshalError

type UnmarshalError struct {

	// SrcValue holds the data that couldn't be unmarshaled
	SrcValue string

	// DestType holds the destination type
	DestType string
	// contains filtered or unexported fields
}

UnmarshalError is returned if unmarshaling is not successful.

func (UnmarshalError) Error

func (e UnmarshalError) Error() string

Error returns a string representation of e.

Jump to

Keyboard shortcuts

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