nan

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2021 License: MIT Imports: 15 Imported by: 3

README

nan - No Allocations Nevermore

Package nan - Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers.

Godoc Coverage Status Go Report Card Go

Features:

  • short name "nan"
  • handy conversion functions
  • select which marshalers you want and limit dependencies to only those you actually need
  • ability to convert your custom structs to nan compatible type with Valid field and all requested encoders/decoders

Supported types:

  • bool
  • float32
  • float64
  • int
  • int8
  • int16
  • int32
  • int64
  • string
  • time.Time
  • more types will be added as necessary

Supported marshallers:

  • Standart JSON
  • jsoniter
  • easyjson
  • go-json
  • Scylla and Cassandra. Compatible with gocql
  • SQL

Usage

Simply create struct field or variable with one of the exported types and use it without any changes to external API.

JSON input/output will be converted to null or non null values. Scylla and Cassandra will use wire format compatible with gocql.

	var data struct {
		Code nan.NullString `json:"code"`
	}

	b, err := jsoniter.Marshal(data)
	if err != nil {
		panic(err)
	}

	// {"code":null}
	fmt.Println(string(b))

	data.Code = nan.String("1")
	// Or
	// data.Code = nan.NullString{String: "1", Valid: true}

	b, err = jsoniter.Marshal(data)
	if err != nil {
		panic(err)
	}

	// {"code":"1"}
	fmt.Println(string(b))

Generate marshalers

# go get github.com/kak-tus/nan/cmd/nan
# nan -help

Instead of depending on the whole github.com/kak-tus/nan you can also use nan command to select which marshalers you want. Simply run nan with one or more arguments and it will generate implementations for the specified marshalers in the current directory. For example, running

# nan gen -json -jsoniter

will generate nan.go, json.go, jsoniter.go files in the current working directory that contain only encoding/json and jsoniter marshalers. Nothing else will be generated so you don't have to depend on all the marshalers that github.com/kak-tus/nan supports. Generated files will use current directory name as its package name. You can also specify your own package name with -pkg argument.

Custom structs generator

Imagine, that you have custom struct

type MyStruct struct {
	ID   int
	Name string
}

Use nan command on its file

# nan extra -json -jsoniter example/structs.go

This will generate *_nan.go near source files with json (or any other supported marshallers). And now you have nan compatible struct with all needed marshallers

var val MyStruct

nullVal := NanMyStruct(val)
// Or
// nullVal := NullMyStruct{MyStruct: val, Valid: true}

fmt.Println(nullVal.ID)

See example to specific of easyjson, cql, sql generation.

nan extra coommand supports any number of file names at command line.

Benchmarks

See here.

Documentation

Overview

Package nan - Zero allocation Nullable structures in one library with handy conversion functions, marshallers and unmarshallers.

Features: - short name "nan" - handy conversion functions - select which marshalers you want and limit dependencies to only those you actually need

Supported types: - bool - float32 - float64 - int - int8 - int16 - int32 - int64 - string - time.Time - more types will be added at necessary

Supported marshallers: - Standart JSON - jsoniter - go-json - easyjson - Scylla and Cassandra. Compatible with gocql - SQL

Usage

Simply create struct field or variable with one of the exported types and use it without any changes to external API.

JSON input/output will be converted to null or non null values. Scylla and Cassandra will use wire format compatible with gocql.

var data struct {
	Code nan.NullString `json:"code"`
}

b, err := jsoniter.Marshal(data)
if err != nil {
	panic(err)
}

// {"code":null}
fmt.Println(string(b))

data.Code = nan.String("1")
// Or
// data.Code = nan.NullString{String: "1", Valid: true}

b, err = jsoniter.Marshal(data)
if err != nil {
	panic(err)
}

// {"code":"1"}
fmt.Println(string(b))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NullBool

type NullBool struct {
	Bool  bool
	Valid bool // Valid is true if Bool is not NULL
}

NullBool - nullable bool

func Bool added in v0.1.9

func Bool(v bool) NullBool

Bool - converts bool to NullBool

func BoolToNullBool deprecated

func BoolToNullBool(v bool) NullBool

BoolToNullBool - converts bool to NullBool

Deprecated: use shorter variant

func (NullBool) IsDefined added in v0.1.10

func (n NullBool) IsDefined() bool

func (NullBool) IsValid added in v0.1.7

func (n NullBool) IsValid() bool

func (NullBool) MarshalCQL

func (n NullBool) MarshalCQL(info gocql.TypeInfo) ([]byte, error)

MarshalCQL - marshaller for cql

func (NullBool) MarshalEasyJSON

func (n NullBool) MarshalEasyJSON(out *jwriter.Writer)

MarshalEasyJSON - marshaller for easyjson

func (NullBool) MarshalJSON

func (n NullBool) MarshalJSON() ([]byte, error)

MarshalJSON - marshaller for json

func (*NullBool) Scan added in v0.1.5

func (n *NullBool) Scan(value interface{}) error

Scan - scan value from sql driver

func (*NullBool) UnmarshalCQL

func (n *NullBool) UnmarshalCQL(info gocql.TypeInfo, data []byte) error

UnmarshalCQL - unmarshaller for cql

func (*NullBool) UnmarshalEasyJSON

func (n *NullBool) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON - unmarshaller for easyjson

func (*NullBool) UnmarshalJSON

func (n *NullBool) UnmarshalJSON(data []byte) error

UnmarshalJSON - unmarshaller for json

func (NullBool) Value added in v0.1.5

func (n NullBool) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullFloat32 added in v0.1.2

type NullFloat32 struct {
	Float32 float32
	Valid   bool // Valid is true if Float32 is not NULL
}

NullFloat32 - nullable float32

func Float32 added in v0.1.9

func Float32(v float32) NullFloat32

Float32 - converts float32 to NullFloat32

func Float32ToNullFloat32 deprecated added in v0.1.3

func Float32ToNullFloat32(v float32) NullFloat32

Float32ToNullFloat32 - converts float32 to NullFloat32

Deprecated: use shorter variant

func (NullFloat32) IsDefined added in v0.1.10

func (n NullFloat32) IsDefined() bool

func (NullFloat32) IsValid added in v0.1.7

func (n NullFloat32) IsValid() bool

func (NullFloat32) MarshalCQL added in v0.1.2

func (n NullFloat32) MarshalCQL(info gocql.TypeInfo) ([]byte, error)

MarshalCQL - marshaller for cql

func (NullFloat32) MarshalEasyJSON added in v0.1.2

func (n NullFloat32) MarshalEasyJSON(out *jwriter.Writer)

MarshalEasyJSON - marshaller for easyjson

func (NullFloat32) MarshalJSON added in v0.1.2

func (n NullFloat32) MarshalJSON() ([]byte, error)

MarshalJSON - marshaller for json

func (*NullFloat32) Scan added in v0.1.5

func (n *NullFloat32) Scan(value interface{}) error

Scan - scan value from sql driver

func (*NullFloat32) UnmarshalCQL added in v0.1.2

func (n *NullFloat32) UnmarshalCQL(info gocql.TypeInfo, data []byte) error

UnmarshalCQL - unmarshaller for cql

func (*NullFloat32) UnmarshalEasyJSON added in v0.1.2

func (n *NullFloat32) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON - unmarshaller for easyjson

func (*NullFloat32) UnmarshalJSON added in v0.1.2

func (n *NullFloat32) UnmarshalJSON(data []byte) error

UnmarshalJSON - unmarshaller for json

func (NullFloat32) Value added in v0.1.5

func (n NullFloat32) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullFloat64 added in v0.1.1

type NullFloat64 struct {
	Float64 float64
	Valid   bool // Valid is true if Float64 is not NULL
}

NullFloat64 - nullable float64

func Float64 added in v0.1.9

func Float64(v float64) NullFloat64

Float64 - converts float64 to NullFloat64

func Float64ToNullFloat64 deprecated added in v0.1.1

func Float64ToNullFloat64(v float64) NullFloat64

Float64ToNullFloat64 - converts float64 to NullFloat64

Deprecated: use shorter variant

func (NullFloat64) IsDefined added in v0.1.10

func (n NullFloat64) IsDefined() bool

func (NullFloat64) IsValid added in v0.1.7

func (n NullFloat64) IsValid() bool

func (NullFloat64) MarshalCQL added in v0.1.1

func (n NullFloat64) MarshalCQL(info gocql.TypeInfo) ([]byte, error)

MarshalCQL - marshaller for cql

func (NullFloat64) MarshalEasyJSON added in v0.1.1

func (n NullFloat64) MarshalEasyJSON(out *jwriter.Writer)

MarshalEasyJSON - marshaller for easyjson

func (NullFloat64) MarshalJSON added in v0.1.1

func (n NullFloat64) MarshalJSON() ([]byte, error)

MarshalJSON - marshaller for json

func (*NullFloat64) Scan added in v0.1.5

func (n *NullFloat64) Scan(value interface{}) error

Scan - scan value from sql driver

func (*NullFloat64) UnmarshalCQL added in v0.1.1

func (n *NullFloat64) UnmarshalCQL(info gocql.TypeInfo, data []byte) error

UnmarshalCQL - unmarshaller for cql

func (*NullFloat64) UnmarshalEasyJSON added in v0.1.1

func (n *NullFloat64) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON - unmarshaller for easyjson

func (*NullFloat64) UnmarshalJSON added in v0.1.1

func (n *NullFloat64) UnmarshalJSON(data []byte) error

UnmarshalJSON - unmarshaller for json

func (NullFloat64) Value added in v0.1.5

func (n NullFloat64) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullInt added in v0.1.4

type NullInt struct {
	Int   int
	Valid bool // Valid is true if Int8 is not NULL
}

NullInt - nullable int

func Int added in v0.1.9

func Int(v int) NullInt

Int - converts int to NullInt

func IntToNullInt deprecated added in v0.1.5

func IntToNullInt(v int) NullInt

IntToNullInt - converts int to NullInt

Deprecated: use shorter variant

func (NullInt) IsDefined added in v0.1.10

func (n NullInt) IsDefined() bool

func (NullInt) IsValid added in v0.1.7

func (n NullInt) IsValid() bool

func (NullInt) MarshalCQL added in v0.1.4

func (n NullInt) MarshalCQL(info gocql.TypeInfo) ([]byte, error)

MarshalCQL - marshaller for cql

func (NullInt) MarshalEasyJSON added in v0.1.4

func (n NullInt) MarshalEasyJSON(out *jwriter.Writer)

MarshalEasyJSON - marshaller for easyjson

func (NullInt) MarshalJSON added in v0.1.4

func (n NullInt) MarshalJSON() ([]byte, error)

MarshalJSON - marshaller for json

func (*NullInt) Scan added in v0.1.5

func (n *NullInt) Scan(value interface{}) error

Scan - scan value from sql driver

func (*NullInt) UnmarshalCQL added in v0.1.4

func (n *NullInt) UnmarshalCQL(info gocql.TypeInfo, data []byte) error

UnmarshalCQL - unmarshaller for cql

func (*NullInt) UnmarshalEasyJSON added in v0.1.4

func (n *NullInt) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON - unmarshaller for easyjson

func (*NullInt) UnmarshalJSON added in v0.1.4

func (n *NullInt) UnmarshalJSON(data []byte) error

UnmarshalJSON - unmarshaller for json

func (NullInt) Value added in v0.1.5

func (n NullInt) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullInt16 added in v0.1.2

type NullInt16 struct {
	Int16 int16
	Valid bool // Valid is true if Int16 is not NULL
}

NullInt16 - nullable int16

func Int16 added in v0.1.9

func Int16(v int16) NullInt16

Int16 - converts int16 to NullInt16

func Int16ToNullInt16 deprecated added in v0.1.3

func Int16ToNullInt16(v int16) NullInt16

Int16ToNullInt16 - converts int16 to NullInt16

Deprecated: use shorter variant

func (NullInt16) IsDefined added in v0.1.10

func (n NullInt16) IsDefined() bool

func (NullInt16) IsValid added in v0.1.7

func (n NullInt16) IsValid() bool

func (NullInt16) MarshalCQL added in v0.1.2

func (n NullInt16) MarshalCQL(info gocql.TypeInfo) ([]byte, error)

MarshalCQL - marshaller for cql

func (NullInt16) MarshalEasyJSON added in v0.1.2

func (n NullInt16) MarshalEasyJSON(out *jwriter.Writer)

MarshalEasyJSON - marshaller for easyjson

func (NullInt16) MarshalJSON added in v0.1.2

func (n NullInt16) MarshalJSON() ([]byte, error)

MarshalJSON - marshaller for json

func (*NullInt16) Scan added in v0.1.5

func (n *NullInt16) Scan(value interface{}) error

Scan - scan value from sql driver

func (*NullInt16) UnmarshalCQL added in v0.1.2

func (n *NullInt16) UnmarshalCQL(info gocql.TypeInfo, data []byte) error

UnmarshalCQL - unmarshaller for cql

func (*NullInt16) UnmarshalEasyJSON added in v0.1.2

func (n *NullInt16) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON - unmarshaller for easyjson

func (*NullInt16) UnmarshalJSON added in v0.1.2

func (n *NullInt16) UnmarshalJSON(data []byte) error

UnmarshalJSON - unmarshaller for json

func (NullInt16) Value added in v0.1.5

func (n NullInt16) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullInt32 added in v0.1.2

type NullInt32 struct {
	Int32 int32
	Valid bool // Valid is true if Int32 is not NULL
}

NullInt32 - nullable int32

func Int32 added in v0.1.9

func Int32(v int32) NullInt32

Int32 - converts int32 to NullInt32

func Int32ToNullInt32 deprecated added in v0.1.3

func Int32ToNullInt32(v int32) NullInt32

Int32ToNullInt32 - converts int32 to NullInt32

Deprecated: use shorter variant

func (NullInt32) IsDefined added in v0.1.10

func (n NullInt32) IsDefined() bool

func (NullInt32) IsValid added in v0.1.7

func (n NullInt32) IsValid() bool

func (NullInt32) MarshalCQL added in v0.1.2

func (n NullInt32) MarshalCQL(info gocql.TypeInfo) ([]byte, error)

MarshalCQL - marshaller for cql

func (NullInt32) MarshalEasyJSON added in v0.1.2

func (n NullInt32) MarshalEasyJSON(out *jwriter.Writer)

MarshalEasyJSON - marshaller for easyjson

func (NullInt32) MarshalJSON added in v0.1.2

func (n NullInt32) MarshalJSON() ([]byte, error)

MarshalJSON - marshaller for json

func (*NullInt32) Scan added in v0.1.5

func (n *NullInt32) Scan(value interface{}) error

Scan - scan value from sql driver

func (*NullInt32) UnmarshalCQL added in v0.1.2

func (n *NullInt32) UnmarshalCQL(info gocql.TypeInfo, data []byte) error

UnmarshalCQL - unmarshaller for cql

func (*NullInt32) UnmarshalEasyJSON added in v0.1.2

func (n *NullInt32) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON - unmarshaller for easyjson

func (*NullInt32) UnmarshalJSON added in v0.1.2

func (n *NullInt32) UnmarshalJSON(data []byte) error

UnmarshalJSON - unmarshaller for json

func (NullInt32) Value added in v0.1.5

func (n NullInt32) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullInt64

type NullInt64 struct {
	Int64 int64
	Valid bool // Valid is true if Int64 is not NULL
}

NullInt64 - nullable int64

func Int64 added in v0.1.9

func Int64(v int64) NullInt64

Int64 - converts int64 to NullInt64

func Int64ToNullInt64 deprecated

func Int64ToNullInt64(v int64) NullInt64

Int64ToNullInt64 - converts int64 to NullInt64

Deprecated: use shorter variant

func (NullInt64) IsDefined added in v0.1.10

func (n NullInt64) IsDefined() bool

func (NullInt64) IsValid added in v0.1.7

func (n NullInt64) IsValid() bool

func (NullInt64) MarshalCQL

func (n NullInt64) MarshalCQL(info gocql.TypeInfo) ([]byte, error)

MarshalCQL - marshaller for cql

func (NullInt64) MarshalEasyJSON

func (n NullInt64) MarshalEasyJSON(out *jwriter.Writer)

MarshalEasyJSON - marshaller for easyjson

func (NullInt64) MarshalJSON

func (n NullInt64) MarshalJSON() ([]byte, error)

MarshalJSON - marshaller for json

func (*NullInt64) Scan added in v0.1.5

func (n *NullInt64) Scan(value interface{}) error

Scan - scan value from sql driver

func (*NullInt64) UnmarshalCQL

func (n *NullInt64) UnmarshalCQL(info gocql.TypeInfo, data []byte) error

UnmarshalCQL - unmarshaller for cql

func (*NullInt64) UnmarshalEasyJSON

func (n *NullInt64) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON - unmarshaller for easyjson

func (*NullInt64) UnmarshalJSON

func (n *NullInt64) UnmarshalJSON(data []byte) error

UnmarshalJSON - unmarshaller for json

func (NullInt64) Value added in v0.1.5

func (n NullInt64) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullInt8 added in v0.1.2

type NullInt8 struct {
	Int8  int8
	Valid bool // Valid is true if Int8 is not NULL
}

NullInt8 - nullable int8

func Int8 added in v0.1.9

func Int8(v int8) NullInt8

Int8 - converts int8 to NullInt8

func Int8ToNullInt8 deprecated added in v0.1.3

func Int8ToNullInt8(v int8) NullInt8

Int8ToNullInt8 - converts int8 to NullInt8

Deprecated: use shorter variant

func (NullInt8) IsDefined added in v0.1.10

func (n NullInt8) IsDefined() bool

func (NullInt8) IsValid added in v0.1.7

func (n NullInt8) IsValid() bool

func (NullInt8) MarshalCQL added in v0.1.2

func (n NullInt8) MarshalCQL(info gocql.TypeInfo) ([]byte, error)

MarshalCQL - marshaller for cql

func (NullInt8) MarshalEasyJSON added in v0.1.2

func (n NullInt8) MarshalEasyJSON(out *jwriter.Writer)

MarshalEasyJSON - marshaller for easyjson

func (NullInt8) MarshalJSON added in v0.1.2

func (n NullInt8) MarshalJSON() ([]byte, error)

MarshalJSON - marshaller for json

func (*NullInt8) Scan added in v0.1.5

func (n *NullInt8) Scan(value interface{}) error

Scan - scan value from sql driver

func (*NullInt8) UnmarshalCQL added in v0.1.2

func (n *NullInt8) UnmarshalCQL(info gocql.TypeInfo, data []byte) error

UnmarshalCQL - unmarshaller for cql

func (*NullInt8) UnmarshalEasyJSON added in v0.1.2

func (n *NullInt8) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON - unmarshaller for easyjson

func (*NullInt8) UnmarshalJSON added in v0.1.2

func (n *NullInt8) UnmarshalJSON(data []byte) error

UnmarshalJSON - unmarshaller for json

func (NullInt8) Value added in v0.1.5

func (n NullInt8) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullString

type NullString struct {
	String string
	Valid  bool // Valid is true if String is not NULL
}

NullString - nullable string

func String added in v0.1.9

func String(v string) NullString

String - converts string to NullString

func StringToNullString deprecated

func StringToNullString(v string) NullString

StringToNullString - converts string to NullString

Deprecated: use shorter variant

func (NullString) IsDefined added in v0.1.10

func (n NullString) IsDefined() bool

func (NullString) IsValid added in v0.1.7

func (n NullString) IsValid() bool

func (NullString) MarshalCQL

func (n NullString) MarshalCQL(info gocql.TypeInfo) ([]byte, error)

MarshalCQL - marshaller for cql

func (NullString) MarshalEasyJSON

func (n NullString) MarshalEasyJSON(out *jwriter.Writer)

MarshalEasyJSON - marshaller for easyjson

func (NullString) MarshalJSON

func (n NullString) MarshalJSON() ([]byte, error)

MarshalJSON - marshaller for json

func (*NullString) Scan added in v0.1.5

func (ns *NullString) Scan(value interface{}) error

Scan - scan value from sql driver

func (*NullString) UnmarshalCQL

func (n *NullString) UnmarshalCQL(info gocql.TypeInfo, data []byte) error

UnmarshalCQL - unmarshaller for cql

func (*NullString) UnmarshalEasyJSON

func (n *NullString) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON - unmarshaller for easyjson

func (*NullString) UnmarshalJSON

func (n *NullString) UnmarshalJSON(data []byte) error

UnmarshalJSON - unmarshaller for json

func (NullString) Value added in v0.1.5

func (ns NullString) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullTime

type NullTime struct {
	Time  time.Time
	Valid bool // Valid is true if Time is not NULL
}

NullTime - nullable time.Time

func Time added in v0.1.9

func Time(v time.Time) NullTime

Time - converts time.Time to NullTime

func TimeToNullTime deprecated

func TimeToNullTime(v time.Time) NullTime

TimeToNullTime - converts time.Time to NullTime

Deprecated: use shorter variant

func (NullTime) IsDefined added in v0.1.10

func (n NullTime) IsDefined() bool

func (NullTime) IsValid added in v0.1.7

func (n NullTime) IsValid() bool

func (NullTime) MarshalCQL

func (n NullTime) MarshalCQL(info gocql.TypeInfo) ([]byte, error)

MarshalCQL - marshaller for cql

func (NullTime) MarshalEasyJSON

func (n NullTime) MarshalEasyJSON(out *jwriter.Writer)

MarshalEasyJSON - marshaller for easyjson

func (NullTime) MarshalJSON

func (n NullTime) MarshalJSON() ([]byte, error)

MarshalJSON - marshaller for json

func (*NullTime) Scan added in v0.1.5

func (n *NullTime) Scan(value interface{}) error

Scan - scan value from sql driver

func (*NullTime) UnmarshalCQL

func (n *NullTime) UnmarshalCQL(info gocql.TypeInfo, data []byte) error

UnmarshalCQL - unmarshaller for cql

func (*NullTime) UnmarshalEasyJSON

func (n *NullTime) UnmarshalEasyJSON(in *jlexer.Lexer)

UnmarshalEasyJSON - unmarshaller for easyjson

func (*NullTime) UnmarshalJSON

func (n *NullTime) UnmarshalJSON(data []byte) error

UnmarshalJSON - unmarshaller for json

func (NullTime) Value added in v0.1.5

func (n NullTime) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Validator added in v0.1.7

type Validator interface {
	IsValid() bool
}

Validator is implemented by all nan types and returns Valid field

Directories

Path Synopsis
cmd
nan

Jump to

Keyboard shortcuts

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