generic

package module
v2.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2018 License: MIT Imports: 8 Imported by: 0

README

Generic

GoDoc License Build Status Coverage Status Go Report Card

flexible data type for Go

support: Go 1.8+

Install

standard go get:

go get -u github.com/usk81/generic

Usage

encode/decode:

package main

import (
	"encoding/json"
	"github.com/usk81/generic"
)

type User struct {
	Name String      `json:"name"`
	Age  generic.Int `json:"age"`
}

var user1 User
u1 := []byte(`{"name":"Daryl Dixon","age":"40"}`)
json.Unmarshal([]byte(u1), &user1)
b, _ := json.Marshal(user1)
Println(string(b))
// {"name":"Daryl Dixon","age":40}

var user2 User
u2 := []byte(`{"name":"Rick Grimes"}`)
json.Unmarshal([]byte(u2), &user2)
b, _ := json.Marshal(user2)
Println(string(b))
// {"name":"Rick Grimes","age":null}

set:

package main

import (
	"fmt"
	"github.com/usk81/generic"
)

func main() {
	v := 1.0

	var tb generic.Bool
	tb.Set(v)
	vb := tb.Weak()
	fmt.Printf("%v, (%T)\n", vb, vb)
	// true, (bool)

	var tf generic.Float
	tf.Set(v)
	vf := tf.Weak()
	fmt.Printf("%v, (%T)\n", vf, vf)
	// 1, (float64)

	var ti generic.Int
	ti.Set(v)
	vi := ti.Weak()
	fmt.Printf("%v, (%T)\n", vi, vi)
	// 1, (int64)

	var ts generic.String
	ts.Set(v)
	vs := ts.Weak()
	fmt.Printf("%v, (%T)\n", vs, vs)
	// 1, (string)

	var tt generic.Time
	tt.Set(v)
	vt := tt.Weak()
	fmt.Printf("%v, (%T)\n", vt.UTC(), vt)
	// 1970-01-01 09:00:01 +0900 JST, (time.Time)

	var tu generic.Uint
	tu.Set(v)
	vu := tu.Weak()
	fmt.Printf("%v, (%T)\n", vu, vu)
	// 1, (uint64)
}

Benchmarks

Marshal
Bool
version requests /op B/op allocs/op
1.0.0 5000000 240 ns 185 3
2.0.0 200000000 6.69 ns 0 0
Float
version requests /op B/op allocs/op
1.0.0 3000000 425 ns 192 3
2.0.0 5000000 260 ns 64 3
Int
version requests /op B/op allocs/op
1.0.0 5000000 265 ns 192 3
2.0.0 20000000 70.5 ns 16 2
String (small)
version requests /op B/op allocs/op
1.0.0 3000000 382 ns 200 3
2.0.0 20000000 89.0 ns 128 2
String (Large)
version requests /op B/op allocs/op
1.0.0 1000000 1056 ns 776 4
2.0.0 5000000 237 ns 896 2
Time
version requests /op B/op allocs/op
1.0.0 1000000 1122 ns 360 5
2.0.0 3000000 401 ns 48 1
TimestampMS
version requests /op B/op allocs/op
1.0.0 20000000 97.9 ns 32 2
2.0.0 20000000 91.2 ns 32 2
TimestampNano
version requests /op B/op allocs/op
1.0.0 10000000 114 ns 64 2
2.0.0 10000000 112 ns 64 2
Timestamp
version requests /op B/op allocs/op
1.0.0 20000000 88.4 ns 32 2
2.0.0 20000000 86.7 ns 32 2
Uint
version requests /op B/op allocs/op
1.0.0 5000000 277 ns 192 3
2.0.0 20000000 64.2 ns 16 2

Licence

MIT

Author

Yusuke Komatsu

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool added in v1.0.0

type Bool struct {
	ValidFlag
	// contains filtered or unexported fields
}

Bool is generic boolean type structure

func MarshalBool

func MarshalBool(x interface{}) (Bool, error)

MarshalBool return generic.Bool converting of request data

func (Bool) Bool added in v1.0.0

func (v Bool) Bool() bool

Bool returns bool value

func (Bool) MarshalJSON added in v1.0.0

func (v Bool) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Bool) Scan added in v1.0.0

func (v *Bool) Scan(x interface{}) (err error)

Scan implements the sql.Scanner interface.

func (*Bool) Set added in v1.0.0

func (v *Bool) Set(x interface{}) (err error)

Set sets a specified value.

func (Bool) String

func (v Bool) String() string

String implements the Stringer interface.

func (*Bool) UnmarshalJSON added in v1.0.0

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

UnmarshalJSON implements the json.Unmarshaler interface.

func (Bool) Value added in v1.0.0

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

Value implements the driver Valuer interface.

func (Bool) Weak

func (v Bool) Weak() interface{}

Weak returns Bool.Bool, but if Bool.ValidFlag is false, returns nil.

type ErrInvalidGenericValue

type ErrInvalidGenericValue struct {
	Value interface{}
}

ErrInvalidGenericValue is used as error in generic types

func (ErrInvalidGenericValue) Error

func (e ErrInvalidGenericValue) Error() string

Error returns error message

type Float added in v1.0.0

type Float struct {
	ValidFlag
	// contains filtered or unexported fields
}

Float is generic float type structure

func MarshalFloat

func MarshalFloat(x interface{}) (Float, error)

MarshalFloat return generic.Float converting of request data

func (Float) Float32

func (v Float) Float32() float32

Float32 returns float32 value

func (Float) Float64

func (v Float) Float64() float64

Float64 returns float64 value

func (Float) MarshalJSON added in v1.0.0

func (v Float) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Float) Scan added in v1.0.0

func (v *Float) Scan(x interface{}) (err error)

Scan implements the sql.Scanner interface.

func (*Float) Set added in v1.0.0

func (v *Float) Set(x interface{}) (err error)

Set sets a specified value.

func (Float) String

func (v Float) String() string

String implements the Stringer interface.

func (*Float) UnmarshalJSON added in v1.0.0

func (v *Float) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (Float) Value added in v1.0.0

func (v Float) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Float) Weak

func (v Float) Weak() interface{}

Weak returns Float.float, but if Float.ValidFlag is false, returns nil.

type Int added in v1.0.0

type Int struct {
	ValidFlag
	// contains filtered or unexported fields
}

Int is generic integer type structure

func MarshalInt

func MarshalInt(x interface{}) (Int, error)

MarshalInt return generic.Int converting of request data

func (Int) Int added in v1.0.0

func (v Int) Int() int

Int return int value

func (Int) Int32

func (v Int) Int32() int32

Int32 return int32 value

func (Int) Int64

func (v Int) Int64() int64

Int64 return int64 value

func (Int) MarshalJSON added in v1.0.0

func (v Int) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Int) Scan added in v1.0.0

func (v *Int) Scan(x interface{}) (err error)

Scan implements the sql.Scanner interface.

func (*Int) Set added in v1.0.0

func (v *Int) Set(x interface{}) (err error)

Set sets a specified value.

func (Int) String

func (v Int) String() string

String implements the Stringer interface.

func (*Int) UnmarshalJSON added in v1.0.0

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

UnmarshalJSON implements the json.Unmarshaler interface.

func (Int) Value added in v1.0.0

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

Value implements the driver Valuer interface.

func (Int) Weak

func (v Int) Weak() interface{}

Weak returns Int.Int, but if Int.ValidFlag is false, returns nil.

type String added in v1.0.0

type String struct {
	ValidFlag
	// contains filtered or unexported fields
}

String is generic string type structure

func MarshalString

func MarshalString(x interface{}) (String, error)

MarshalString return generic.String converting of request data

func (String) MarshalJSON added in v1.0.0

func (v String) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*String) Scan added in v1.0.0

func (v *String) Scan(x interface{}) (err error)

Scan implements the sql.Scanner interface.

func (*String) Set added in v1.0.0

func (v *String) Set(x interface{}) (err error)

Set sets a specified value.

func (String) String added in v1.0.0

func (v String) String() string

String implements the Stringer interface.

func (*String) UnmarshalJSON added in v1.0.0

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

UnmarshalJSON implements the json.Unmarshaler interface.

func (String) Value added in v1.0.0

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

Value implements the driver Valuer interface.

func (String) Weak

func (v String) Weak() interface{}

Weak returns string, but if String.ValidFlag is false, returns nil.

type Time added in v1.0.0

type Time struct {
	ValidFlag
	// contains filtered or unexported fields
}

Time is generic time type structure

func MarshalTime

func MarshalTime(x interface{}) (Time, error)

MarshalTime return generic.Time converting of request data

func (Time) MarshalJSON added in v1.0.0

func (v Time) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Time) Scan added in v1.0.0

func (v *Time) Scan(x interface{}) (err error)

Scan implements the sql.Scanner interface.

func (*Time) Set added in v1.0.0

func (v *Time) Set(x interface{}) (err error)

Set sets a specified value.

func (Time) String

func (v Time) String() string

String implements the Stringer interface.

func (Time) Time added in v1.0.0

func (v Time) Time() time.Time

Time returns value as time.Time

func (*Time) UnmarshalJSON added in v1.0.0

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

UnmarshalJSON implements the json.Unmarshaler interface.

func (Time) Value added in v1.0.0

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

Value implements the driver Valuer interface.

func (Time) Weak

func (v Time) Weak() interface{}

Weak returns Time.Time, but if Time.ValidFlag is false, returns nil.

type Timestamp added in v1.1.0

type Timestamp struct {
	ValidFlag
	// contains filtered or unexported fields
}

Timestamp is a wrapped time type structure

func MarshalTimestamp

func MarshalTimestamp(x interface{}) (Timestamp, error)

MarshalTimestamp return generic.Timestamp converting of request data

func (Timestamp) Int

func (v Timestamp) Int() int

Int return int value

func (Timestamp) Int64

func (v Timestamp) Int64() int64

Int64 return int64 value

func (Timestamp) MarshalJSON added in v1.1.0

func (v Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Timestamp) Scan added in v1.1.0

func (v *Timestamp) Scan(x interface{}) (err error)

Scan implements the sql.Scanner interface.

func (*Timestamp) Set added in v1.1.0

func (v *Timestamp) Set(x interface{}) (err error)

Set sets a specified value.

func (Timestamp) String

func (v Timestamp) String() string

String implements the Stringer interface.

func (*Timestamp) UnmarshalJSON added in v1.1.0

func (v *Timestamp) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (Timestamp) Value added in v1.1.0

func (v Timestamp) Value() (driver.Value, error)

Value returns Time.Time, but if Time.ValidFlag is false, returns nil.

func (Timestamp) Weak

func (v Timestamp) Weak() interface{}

Weak returns timestamp, but if Timestamp.ValidFlag is false, returns nil.

type TimestampMS added in v1.1.0

type TimestampMS struct {
	ValidFlag
	// contains filtered or unexported fields
}

TimestampMS is a wrapped time type structure

func MarshalTimestampMS

func MarshalTimestampMS(x interface{}) (TimestampMS, error)

MarshalTimestampMS return generic.TimestampMS converting of request data

func (TimestampMS) Int

func (v TimestampMS) Int() int

Int return int value

func (TimestampMS) Int64

func (v TimestampMS) Int64() int64

Int64 return int64 value

func (TimestampMS) MarshalJSON added in v1.1.0

func (v TimestampMS) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*TimestampMS) Scan added in v1.1.0

func (v *TimestampMS) Scan(x interface{}) (err error)

Scan implements the sql.Scanner interface.

func (*TimestampMS) Set added in v1.1.0

func (v *TimestampMS) Set(x interface{}) (err error)

Set sets a specified value.

func (TimestampMS) String

func (v TimestampMS) String() string

String implements the Stringer interface.

func (*TimestampMS) UnmarshalJSON added in v1.1.0

func (v *TimestampMS) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (TimestampMS) Value added in v1.1.0

func (v TimestampMS) Value() (driver.Value, error)

Value returns timestamp with milliseconds, but if TimestampMS.ValidFlag is false, returns nil.

func (TimestampMS) Weak

func (v TimestampMS) Weak() interface{}

Weak returns timestamp int value, but if TimestampMS.ValidFlag is false, returns nil.

type TimestampNano added in v1.1.0

type TimestampNano struct {
	ValidFlag
	// contains filtered or unexported fields
}

TimestampNano is a wrapped time type structure

func MarshalTimestampNano

func MarshalTimestampNano(x interface{}) (TimestampNano, error)

MarshalTimestampNano return generic.TimestampNano converting of request data

func (TimestampNano) Int

func (v TimestampNano) Int() int

Int return int value

func (TimestampNano) Int64

func (v TimestampNano) Int64() int64

Int64 return int64 value

func (TimestampNano) MarshalJSON added in v1.1.0

func (v TimestampNano) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*TimestampNano) Scan added in v1.1.0

func (v *TimestampNano) Scan(x interface{}) (err error)

Scan implements the sql.Scanner interface.

func (*TimestampNano) Set added in v1.1.0

func (v *TimestampNano) Set(x interface{}) (err error)

Set sets a specified value.

func (TimestampNano) String

func (v TimestampNano) String() string

String implements the Stringer interface.

func (*TimestampNano) UnmarshalJSON added in v1.1.0

func (v *TimestampNano) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (TimestampNano) Value added in v1.1.0

func (v TimestampNano) Value() (driver.Value, error)

Value returns timestamp with nanoseconds, but if TimestampNano.ValidFlag is false, returns nil.

func (TimestampNano) Weak

func (v TimestampNano) Weak() interface{}

Weak returns timestamp with nano seconds, but if TimestampNano.ValidFlag is false, returns nil.

type Type

type Type interface {
	Valid() bool
	Value() (driver.Value, error)
	Scan(interface{}) error
	Set(interface{}) error
	Reset()
}

Type is the interface used as the basis for generic types

type URL

type URL struct {
	ValidFlag
	// contains filtered or unexported fields
}

URL is generic url.URL type structure

func MarshalURL

func MarshalURL(x interface{}) (URL, error)

MarshalURL return generic.URL converting of request data

func (URL) EscapedPath

func (v URL) EscapedPath() string

EscapedPath returns the escaped form of v.url.Path. In general there are multiple possible escaped forms of any path.

EscapedPath returns v.url.RawPath when it is a valid escaping of v.url.Path. Otherwise EscapedPath ignores v.url.RawPath and computes an escaped form on its own. The String and RequestURI methods use EscapedPath to construct their results. In general, code should call EscapedPath instead of reading v.url.RawPath directly.

func (URL) Hostname

func (v URL) Hostname() string

Hostname returns v.url.Host, without any port number.

If Host is an IPv6 literal with a port number, Hostname returns the IPv6 literal without the square brackets. IPv6 literals may include a zone identifier.

func (URL) IsAbs

func (v URL) IsAbs() bool

IsAbs reports whether the URL is absolute. Absolute means that it has a non-empty scheme.

func (URL) MarshalJSON

func (v URL) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (URL) Parse

func (v URL) Parse(ref string) (result URL, err error)

Parse parses a URL in the context of the receiver. The provided URL may be relative or absolute. Parse returns nil, err on parse failure, otherwise its return value is the same as ResolveReference.

func (URL) Port

func (v URL) Port() string

Port returns the port part of u.Host, without the leading colon. If u.Host doesn't contain a port, Port returns an empty string.

func (URL) Query

func (v URL) Query() url.Values

Query parses RawQuery and returns the corresponding values. It silently discards malformed value pairs. To check errors use ParseQuery.

func (URL) RequestURI

func (v URL) RequestURI() string

RequestURI returns the encoded path?query or opaque?query string that would be used in an HTTP request for v.

func (URL) ResolveReference

func (v URL) ResolveReference(ref *url.URL) URL

ResolveReference resolves a URI reference to an absolute URI from an absolute base URI, per RFC 3986 Section 5.2. The URI reference may be relative or absolute. ResolveReference always returns a new URL instance, even if the returned URL is identical to either the base or reference. If ref is an absolute URL, then ResolveReference ignores base and returns a copy of ref.

func (*URL) Scan

func (v *URL) Scan(x interface{}) (err error)

Scan implements the sql.Scanner interface.

func (*URL) Set

func (v *URL) Set(x interface{}) (err error)

Set sets a specified value.

func (URL) String

func (v URL) String() string

String implements the Stringer interface.

func (URL) URL

func (v URL) URL() *url.URL

URL returns *url.URL, but if String.ValidFlag is false, returns nil.

func (*URL) UnmarshalJSON

func (v *URL) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (URL) Value

func (v URL) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (URL) Weak

func (v URL) Weak() interface{}

Weak returns *url.URL, but if String.ValidFlag is false, returns nil.

type Uint added in v1.0.0

type Uint struct {
	ValidFlag
	// contains filtered or unexported fields
}

Uint is generic uint type structure

func MarshalUint

func MarshalUint(x interface{}) (Uint, error)

MarshalUint return generic.Uint converting of request data

func (Uint) MarshalJSON added in v1.0.0

func (v Uint) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Uint) Scan added in v1.0.0

func (v *Uint) Scan(x interface{}) (err error)

Scan implements the sql.Scanner interface.

func (*Uint) Set added in v1.0.0

func (v *Uint) Set(x interface{}) (err error)

Set sets a specified value.

func (Uint) String

func (v Uint) String() string

String implements the Stringer interface.

func (Uint) Uint added in v1.0.0

func (v Uint) Uint() uint

Uint return uint value

func (Uint) Uint32

func (v Uint) Uint32() uint32

Uint32 return uint32 value

func (Uint) Uint64

func (v Uint) Uint64() uint64

Uint64 return uint64 value

func (*Uint) UnmarshalJSON added in v1.0.0

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

UnmarshalJSON implements the json.Unmarshaler interface.

func (Uint) Value added in v1.0.0

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

Value implements the driver Valuer interface.

func (Uint) Weak

func (v Uint) Weak() interface{}

Weak returns Uint.Uint, but if Uint.ValidFlag is false, returns nil.

type ValidFlag

type ValidFlag bool

ValidFlag is the flag to check that value is valid

func (*ValidFlag) Reset

func (v *ValidFlag) Reset()

Reset resets ValidFlag

func (ValidFlag) Valid

func (v ValidFlag) Valid() bool

Valid validates the specified value is nil or not

Jump to

Keyboard shortcuts

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