ft

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2023 License: MIT Imports: 7 Imported by: 2

README

mozey/ft

Flexible types[1] for un-marshaling any JSON[2] value.

This package builds on

Numeric types (Int, Float) wrap 64bit base types, e.g. int64 and float64.

For each basic type, this package implements two flexible types[3], for example

  • required struct fields may use ft.String, it supports flexible types when un-marshaling.
  • optional fields may use ft.NString, it supports both flexible types and allows NULL. Think of the N-prefix as meaning "allows null", or "not-required"
type data struct {
    Required ft.String `json:"required"`
    Optional ft.NString `json:"optional"`
}

Above data struct can be used to un-marshal the following JSON

{
    "required": 123,
    "optional": null
}

NULL is not considered valid, for example

b := []byte(`{"required": 123,"optional": null}`)
d := data{}
err = json.Unmarshal(b, &d)
fmt.Println(d.Required.Valid)  // true
fmt.Println(d.Required.String) // "123"
fmt.Println(d.Optional.Valid)  // false
fmt.Println(d.Optional.String) // ""

Both custom types in this package, allowing NULL or not, embed structs with the same fields. For example

// String
s := ft.StringForm(123)
fmt.Println(string(s.Valid)) // true
fmt.Println(s.String)        // 123
// NString
s := ft.NStringForm(123)
fmt.Println(string(s.Valid)) // true
fmt.Println(s.String)        // 123

After un-marshaling, the Valid field may be toggled by your custom validation code.

Flexible types can be used with the templating packages. Either of the types above, String or NString, could be used in this text template example

t1 = template.Must(t1.Parse(`{{if .Valid }} The value is .String {{end}}`))
t1.Execute(os.Stdout, s) // The value is 123

Tests

See tests for more usage examples

cd ${PRO_PATH}
git clone https://github.com/mozey/ft.git
cd ${PRO_PATH}/ft
go clean -testcache && go test -v ./...

Reference

[1] Not quite the same concept as Flexible typing in SQLite. Note that "as of SQLite version 3.37.0 (2021-11-27), SQLite supports this development style [Rigid Type Enforcement] using STRICT tables"

Previously this repo used the term "fuzzy types". It was renamed, and the term replaced with "flexible types". To avoid confusion with fuzzing, a new feature added to the "standard toolchain beginning in Go 1.18"

[2] JSON spec

[3] This repo does not use generics, and will compile with older versions of Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clean

func Clean(s string) string

Clean removes non-graphic characters from the given string, see https://github.com/icza/gox/blob/master/stringsx/stringsx.go#L9 and https://stackoverflow.com/a/58994297/639133 However, above function also removes newlines, that is not desired?

Types

type Bool

type Bool struct {
	Bool bool
}

Bool can be used to decode any JSON value to bool. Empty strings as well as "false" and "0" evaluate to false, all other strings are true. Numbers equal to 0 will evaluate to false, all other numbers are true.

func BoolFrom

func BoolFrom(fb bool) Bool

func (Bool) MarshalJSON

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

MarshalJSON method for Bool

func (Bool) MarshalText added in v1.1.0

func (fb Bool) MarshalText() (text []byte, err error)

func (*Bool) UnmarshalJSON

func (fb *Bool) UnmarshalJSON(bArr []byte) (err error)

UnmarshalJSON method for Bool

func (*Bool) UnmarshalText added in v1.1.2

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

type Float

type Float struct {
	Float64 float64
}

Float can be used to decode any JSON value to int64. Strings that are not valid representation of a number will error. Boolean values will error

func FloatFrom

func FloatFrom(ff float64) Float

func (Float) MarshalJSON

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

MarshalJSON method for Float

func (Float) MarshalText added in v1.1.0

func (ff Float) MarshalText() (text []byte, err error)

func (*Float) UnmarshalJSON

func (ff *Float) UnmarshalJSON(bArr []byte) (err error)

UnmarshalJSON method for Float

func (*Float) UnmarshalText added in v1.1.2

func (ff *Float) UnmarshalText(text []byte) error

type Int

type Int struct {
	Int64 int64
}

Int can be used to decode any JSON value to int64. Strings that are not valid representation of a number will error. Boolean values will error

func IntFrom

func IntFrom(fi int64) Int

func (Int) MarshalJSON

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

MarshalJSON method for Int

func (Int) MarshalText added in v1.1.0

func (fi Int) MarshalText() (text []byte, err error)

func (*Int) UnmarshalJSON

func (fi *Int) UnmarshalJSON(bArr []byte) (err error)

UnmarshalJSON method for Int

func (*Int) UnmarshalText added in v1.1.2

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

type NBool added in v1.0.1

type NBool null.Bool

NBool can be used to decode any JSON value to bool. Empty strings as well as "false" and "0" evaluate to false, all other strings are true. Numbers equal to 0 will evaluate to false, all other numbers are true.

func NBoolFrom added in v1.0.1

func NBoolFrom(fb bool) NBool

func (NBool) MarshalJSON added in v1.0.1

func (fb NBool) MarshalJSON() ([]byte, error)

MarshalJSON method for Bool

func (NBool) MarshalText added in v1.1.0

func (fb NBool) MarshalText() (text []byte, err error)

func (*NBool) UnmarshalJSON added in v1.0.1

func (fb *NBool) UnmarshalJSON(bArr []byte) (err error)

UnmarshalJSON method for Bool

func (*NBool) UnmarshalText added in v1.1.2

func (fb *NBool) UnmarshalText(text []byte) error

type NFloat added in v1.0.1

type NFloat null.Float

NFloat can be used to decode any JSON value to int64. Strings that are not valid representation of a number will error. Boolean values will error

func NFloatFrom added in v1.0.1

func NFloatFrom(ff float64) NFloat

func (NFloat) MarshalJSON added in v1.0.1

func (fi NFloat) MarshalJSON() ([]byte, error)

MarshalJSON method for Float

func (NFloat) MarshalText added in v1.1.0

func (ff NFloat) MarshalText() (text []byte, err error)

func (*NFloat) UnmarshalJSON added in v1.0.1

func (fi *NFloat) UnmarshalJSON(bArr []byte) (err error)

UnmarshalJSON method for Float

func (*NFloat) UnmarshalText added in v1.1.2

func (ff *NFloat) UnmarshalText(text []byte) error

type NInt added in v1.0.1

type NInt null.Int

NInt can be used to decode any JSON value to int64. Strings that are not valid representation of a number will error. Boolean values will error

func NIntFrom added in v1.0.1

func NIntFrom(fi int64) NInt

func NIntFromString added in v1.1.2

func NIntFromString(s string) (NInt, error)

NIntFromString returns an Int for the given string

func (NInt) MarshalJSON added in v1.0.1

func (fi NInt) MarshalJSON() ([]byte, error)

MarshalJSON method for Int

func (NInt) MarshalText added in v1.1.0

func (fi NInt) MarshalText() (text []byte, err error)

func (*NInt) UnmarshalJSON added in v1.0.1

func (fi *NInt) UnmarshalJSON(bArr []byte) (err error)

UnmarshalJSON method for Int

func (*NInt) UnmarshalText added in v1.1.2

func (fi *NInt) UnmarshalText(text []byte) error

type NString added in v1.0.1

type NString null.String

NString can be used to decode any JSON value to string

func NStringFrom added in v1.0.1

func NStringFrom(fs string) NString

func (NString) MarshalJSON added in v1.0.1

func (fs NString) MarshalJSON() ([]byte, error)

MarshalJSON method with value receiver for String Method must not have a pointer receiver! See https://stackoverflow.com/a/21394657/639133

func (NString) MarshalText added in v1.1.0

func (fs NString) MarshalText() (text []byte, err error)

func (*NString) UnmarshalJSON added in v1.0.1

func (fs *NString) UnmarshalJSON(bArr []byte) (err error)

UnmarshalJSON for String

func (*NString) UnmarshalText added in v1.1.2

func (fs *NString) UnmarshalText(text []byte) error

type String

type String struct {
	String string
}

String can be used to decode any JSON value to string

func StringFrom

func StringFrom(fs string) String

func (String) MarshalJSON

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

MarshalJSON method with value receiver for String Method must not have a pointer receiver! See https://stackoverflow.com/a/21394657/639133

func (String) MarshalText added in v1.1.0

func (fs String) MarshalText() (text []byte, err error)

func (*String) UnmarshalJSON

func (fs *String) UnmarshalJSON(bArr []byte) (err error)

UnmarshalJSON for String

func (*String) UnmarshalText added in v1.1.2

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

Jump to

Keyboard shortcuts

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