dynamic

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

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

Go to latest
Published: Dec 10, 2021 License: Apache-2.0 Imports: 10 Imported by: 1

README

dynamic

dynamic is a collection of dynamic data types to supoprt picker

All types have a Value method which return interface{}as it can be nil or potentially various types. Values that are nil are json encoded to null. To avoid this behavior, use a pointer to the dynamic type.

dynamic types are not thread safe.

dynamic.Bool

You can set Bool with any of the following:

  • bool, *bool
  • dynamic.Bool
  • *dynamic.Bool
  • string
  • *string
  • []byte
  • fmt.Stringer
  • nil
package main
import (
    "fmt"
    "github.com/chanced/dynamic"
)

boolean, err := dynamic.NewBool("true")
_ = err
if b, ok := boolean.Bool(); ok {
    fmt.Println(b)
}
err = boolean.Set("false")
_ = err

boolean, err = dynamic.NewBool(true)
_ = err // handle err
if b, ok := boolean.Bool(); ok {
    fmt.Println(b)
}
err = boolean.Set("1")
_ = err

dynamic.Number

You can set Number with any of the following:

  • string,
  • *string
  • json.Number
  • fmt.Stringer
  • int,
  • int, int64, int32, int16, int8
  • uint, uint64, uint32, uint16, uint8
  • float64, float32
  • complex128, complex64
  • *int, *int64, *int32, *int16, *int8
  • *uint, *uint64, *uint32, *uint16, *uint8
  • *float64, *float32,
  • *complex128, *complex64
  • []byte,
  • fmt.Stringer
  • nil
package main
import (
    "fmt"
    "github.com/chanced/dynamic"
    "math"
)
func main(){
    number, err := dynamic.NewNumber(34)
    _ = err
    if u, ok := number.Uint64(); ok {
        fmt.Println(u)
    }
    err = n.Set("34.34")
    _ = err
    if u, ok := number.Uint64(); ok {
        // this wont be reached because 34.34 can not be
        // converted to a float without losing data
        fmt.Println(u)
    }
    if f, ok := number.Float32(); ok {
      fmt.Println(f)
    }

    err = number.Set(math.MaxFloat64)
    _ = err // no err but demonstrating

    if f, ok := number.Float32(); ok {
      // this won't be reached because number exceeds
      // MaxFloat32
    }
}

dynamic.String

String accepts any of the following types:

  • string, *string
  • []byte
  • dynamic.String, *dynamic.String
  • fmt.Stringer
  • []string (joined with ",")
  • int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
  • uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
  • float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
  • bool, *bool
  • nil
package main
import (
    "fmt"
    "github.com/chanced/dynamic"
)

func main() {
    str, err := dynamic.NewString("str")
    val := str.ToLower().String() // "str"

    if str.Equal("true") {
        fmt.Println("equal")
    }
    fmt.Println(str.ToLower().String())
}

dynamic.StringNumberBoolOrTime

StringNumberBoolOrTime accepts any of the following types:

  • string, *string
  • []byte
  • time.Time, *time.Time
  • fmt.Stringer
  • []string (joined with ",")
  • int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
  • uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
  • float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
  • bool, *bool
  • nil
package main
import (
    "fmt"
    "time"
    "github.com/chanced/dynamic"
)
func main() {
    now := dynamic.NewStringNumberBoolOrTime(time.Now())

    if n, ok := now.Time(); {
        fmt.Println(n)
    }
}

dynamic.StringNumberOrTime

StringNumberOrTime accepts any of the following types:

  • string, *string
  • []byte
  • time.Time, *time.Time
  • fmt.Stringer
  • []string (joined with ",")
  • int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
  • uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
  • float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
  • bool, *bool
  • nil
package main
import (
    "log"
    "github.com/chanced/dynamic"
)
func main() {
    now, _ := dynamic.NewStringNumberOrTime(34.34)

    if n, ok := now.Float64(); {
        log.Println(n)
    }

    if n, ok := now.Int64(); {
        // this won't be reached because it isn't possible to
        // convert the float value without losing data
        log.Fatal("was able to cast a float as int64")
    }
}

dynamic.BoolOrString

BoolOrString accepts any of the following types:

  • bool, *bool
  • string, *string
  • []byte
  • fmt.Stringer
  • []string (joined with ",")
  • int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
  • uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
  • float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
  • nil
package main
import (
    "fmt"
    "time"
    "github.com/chanced/dynamic"
)
func main() {
    v, _ := dynamic.NewBoolOrString("true")

    if n, ok := v.Bool(); {
        fmt.Println(n)
    }
}

dynamic.StringOrArrayOfStrings

This is essentially a []string except it'll Unmarshal either a string or a []string. It always marshals into []string though.

NewStringOrArrayOfStrings and Set accept:

  • string, *string
  • []byte
  • dynamic.String, *dynamic.String
  • fmt.Stringer
  • []string (joined with ",")
  • int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
  • uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
  • float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
  • bool, *bool
  • nil

or you can use it like a slice:

package main

import (
  "github.com/chanced/dynamic"
  "log"
)
func main() {
    strs, _ := dynamic.StringOrArrayOfStrings{"value", "value2"}

    err := strs.Iterate(func(v string) error{
        if v == "value" {
            return dynamic.Done
        }
    })

    if err != nil {
        // err will be nil because Iterate checks for dynamic.Done
        // (or an error which returns "done" from Error())
        log.fatal(err)
    }

    err = strs.Iterate(func(v string) error) {
        if v == "value" {
            return fmt.Errorf("some error")
        }
    })
    if err != nil {
        // err will be "some error"
        log.Println(err)
    }

}

dynamic.JSON

JSON is basically []byte with helper methods as well as satisfying json.Marshaler and json.Unmarshaler

  import(
      "fmt"
      "encoding/json"
      "github.com/chanced/dynamic"
  )

  func main() {
       data, _ := json.Marshal("str")
      d := dynamic.JSON(data)
      fmt.Println(d.IsString()) // true
      fmt.Println(d.IsBool()) // false


      // dynamic.JSON does not parse strings for potential
      // values:
      data, _ = json.Marshal("true")
      d = dynamic.JSON(data)
      fmt.Println(d.IsString()) // true
      fmt.Println(d.IsBool()) // false

      fmt.Println(d.UnquotedString()) // prints true

      data, _ = json.Marshal(true)
      d = dynamic.JSON(data)
      fmt.Println(d.IsString()) // false
      fmt.Println(d.IsBool()) // true
      fmt.Println(d.IsNumber()) // false
      fmt.Println(d.IsObject()) // false
      fmt.Println(d.IsArray()) // false

      data, = json.Marshal(map[string]string{"key":"value"})
      d = dynamic.JSON(data)
      fmt.Println(d.IsObject()) // true
      fmt.Println(d.IsEmptyObject()) // false

      data, = json.Marshal(map[string]string{})
      fmt.Println(d.IsObject()) // true
      fmt.Println(d.IsEmptyObject()) // true
  }

Other types and mentions:

dynamic.JSONObject

dynamic.JSONObject is a map[string]dynamic.JSON

dynamic.Null

dynamic.Null is []byte("null") for json purposes

dynamic.Done

dynamic.Done is an error that indicates an iterator should stop but not return the error to the caller.

TODO

  • Add math functions as methods to Number
  • Add dynamic.String methods to all types which could be string
  • Lot more testing to do
  • Comments

License

Apache 2.0

Documentation

Overview

Package dynamic contains dynamic types that can be various values

Index

Constants

This section is empty.

Variables

View Source
var (
	False = Bool{/* contains filtered or unexported fields */}
	True  = Bool{/* contains filtered or unexported fields */}
)
View Source
var (
	ErrInvalidValue = errors.New("dynamic: invalid value")
	ErrInvalidType  = errors.New("dynamic: invalid type")
)

errors

View Source
var (
	ErrIndexOutOfBounds = errors.New("index out of bounds")
	Done                = errors.New("done")
)
View Source
var DefaultTimeLayout = func() string {
	return DefaultTimeLayouts[0]
}
View Source
var DefaultTimeLayouts = []string{time.RFC3339}
View Source
var Null = JSON("null")

Functions

This section is empty.

Types

type Bool

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

func NewBool

func NewBool(value interface{}) (Bool, error)

NewBool returns a new Bool value initialized to the first, if any, value passed in.

You can set Bool to any of the following:

bool, dynamic.Bool, *bool, *dynamic.Bool
string, []byte, fmt.Stringer, *string
nil

func NewBoolPtr

func NewBoolPtr(value interface{}) (*Bool, error)

NewBoolPtr returns a pointer to a new Bool

See NewBool for valid options, usage and warnings

func (*Bool) Bool

func (b *Bool) Bool() (bool, bool)

func (*Bool) Clear

func (b *Bool) Clear()

func (Bool) Equal

func (b Bool) Equal(value interface{}) bool

func (Bool) HasValue

func (b Bool) HasValue() bool

func (*Bool) IsFalse

func (b *Bool) IsFalse() bool

func (*Bool) IsNil

func (b *Bool) IsNil() bool

func (*Bool) IsNull

func (b *Bool) IsNull() bool

func (*Bool) IsTrue

func (b *Bool) IsTrue() bool

func (Bool) MarshalJSON

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

func (*Bool) Parse

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

func (*Bool) Set

func (b *Bool) Set(value interface{}) error

func (*Bool) SetValue

func (b *Bool) SetValue(v bool)

func (*Bool) String

func (b *Bool) String() string

func (*Bool) UnmarshalJSON

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

func (Bool) Value

func (b Bool) Value() interface{}

type BoolOrString

type BoolOrString struct {
	// contains filtered or unexported fields
}

BoolOrString is a dynamic type that is a string, bool, or nil. It stores the value as a string and decodes json into either a bool or a string

func NewBoolOrString

func NewBoolOrString(value interface{}) (BoolOrString, error)

NewBoolOrString returns a new BoolOrString

Valid value types

string, []byte, dynamic.String, dynamic.Bool, fmt.Stringer, []string, *string,
int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
bool, *bool
nil

func NewBoolOrStringPtr

func NewBoolOrStringPtr(value interface{}) (*BoolOrString, error)

NewBoolOrStringPtr returns a pointer to a new BoolOrString

Valid value types:

string, []byte, dynamic.String, dynamic.Bool, fmt.Stringer, []string, *string,
int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
bool, *bool
nil

func (*BoolOrString) Bool

func (bs *BoolOrString) Bool() (value bool, isBool bool)

func (*BoolOrString) Dereference

func (bs *BoolOrString) Dereference() BoolOrString

func (*BoolOrString) EncodeToEmptyString

func (bs *BoolOrString) EncodeToEmptyString() *BoolOrString

func (*BoolOrString) EncodeToNull

func (bs *BoolOrString) EncodeToNull() *BoolOrString

func (*BoolOrString) IsBool

func (bs *BoolOrString) IsBool() bool

func (*BoolOrString) IsEmpty

func (bs *BoolOrString) IsEmpty() bool

func (*BoolOrString) IsNil

func (bs *BoolOrString) IsNil() bool

func (BoolOrString) MarshalJSON

func (bs BoolOrString) MarshalJSON() ([]byte, error)

func (*BoolOrString) Reference

func (bs *BoolOrString) Reference() *BoolOrString

func (*BoolOrString) Set

func (bs *BoolOrString) Set(value interface{}) error

Set sets the BoolOrString's value

Valid value types:

You can set String to any of the following:

string, []byte, dynamic.String, dynamic.Bool, fmt.Stringer, []string, *string,
int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
bool, *bool
nil

func (BoolOrString) String

func (bs BoolOrString) String() string

func (*BoolOrString) UnmarshalJSON

func (bs *BoolOrString) UnmarshalJSON(data []byte) error

type JSON

type JSON []byte

func (JSON) ContainsEscapeRune

func (d JSON) ContainsEscapeRune() bool

ContainsEscapeRune reports whether the string value of d contains "\" It returns false if d is not a quoted string.

func (JSON) Equal

func (d JSON) Equal(data []byte) bool

func (JSON) IsArray

func (d JSON) IsArray() bool

IsArray reports whether the data is a json array. It does not check whether the json is malformed.

func (JSON) IsBool

func (d JSON) IsBool() bool

IsBool reports true if data appears to be a json boolean value. It is possible that it will report false positives of malformed json.

IsBool does not parse strings

func (JSON) IsEmptyArray

func (d JSON) IsEmptyArray() bool

func (JSON) IsEmptyObject

func (d JSON) IsEmptyObject() bool

func (JSON) IsFalse

func (d JSON) IsFalse() bool

IsFalse reports true if data appears to be a json boolean value of false. It is possible that it will report false positives of malformed json as it only checks the first character and length.

IsFalse does not parse strings

func (JSON) IsNull

func (d JSON) IsNull() bool

func (JSON) IsNumber

func (d JSON) IsNumber() bool

func (JSON) IsObject

func (d JSON) IsObject() bool

func (JSON) IsString

func (d JSON) IsString() bool

func (JSON) IsTrue

func (d JSON) IsTrue() bool

IsTrue reports true if data appears to be a json boolean value of true. It is possible that it will report false positives of malformed json as it only checks the first character and length.

IsTrue does not parse strings

func (JSON) Len

func (d JSON) Len() int

func (JSON) MarshalJSON

func (d JSON) MarshalJSON() ([]byte, error)

func (*JSON) UnmarshalJSON

func (d *JSON) UnmarshalJSON(data []byte) error

func (JSON) UnquotedString

func (d JSON) UnquotedString() string

UnquotedString trims double quotes from the bytes. It does not parse for escaped characters

type JSONObject

type JSONObject map[string]JSON

func (JSONObject) MarshalJSON

func (obj JSONObject) MarshalJSON() ([]byte, error)

func (*JSONObject) UnmarshalJSON

func (obj *JSONObject) UnmarshalJSON(data []byte) error

type Map

type Map map[string]interface{}

type Number

type Number struct {
	// contains filtered or unexported fields
}

func NewNumber

func NewNumber(value interface{}) (Number, error)

NewNumber returns a new Number set to the first, if any, parameters.

You can set String to any of the following:

string, *string, json.Number, fmt.Stringer,
int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
float64, float32, *float64, *float32

func NewNumberPtr

func NewNumberPtr(value interface{}) (*Number, error)

NewNumberPtr returns a pointer to a new Number. See NewNumber for information & warnings.

func (Number) Bytes

func (n Number) Bytes() []byte

func (*Number) Clear

func (n *Number) Clear()

func (Number) Float32

func (n Number) Float32() (float32, bool)

func (Number) Float64

func (n Number) Float64() (float64, bool)

func (Number) HasValue

func (n Number) HasValue() bool

func (Number) Int

func (n Number) Int() (int, bool)

func (Number) Int16

func (n Number) Int16() (int16, bool)

func (Number) Int32

func (n Number) Int32() (int32, bool)

func (Number) Int64

func (n Number) Int64() (int64, bool)

func (Number) Int8

func (n Number) Int8() (int8, bool)

func (Number) IsNil

func (n Number) IsNil() bool

func (Number) MarshalJSON

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

func (*Number) Parse

func (n *Number) Parse(s string) error

func (*Number) Set

func (n *Number) Set(value interface{}) error

func (Number) String

func (n Number) String() string

func (Number) Uint

func (n Number) Uint() (uint, bool)

func (Number) Uint16

func (n Number) Uint16() (uint16, bool)

func (Number) Uint32

func (n Number) Uint32() (uint32, bool)

func (Number) Uint64

func (n Number) Uint64() (uint64, bool)

func (Number) Uint8

func (n Number) Uint8() (uint8, bool)

func (*Number) UnmarshalJSON

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

func (*Number) Value

func (n *Number) Value() interface{}

type String

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

func NewString

func NewString(value interface{}) (String, error)

NewString returns a new String. Only the first parameter passed in is considered.

String can be set to any of the following:

string, []byte, dynamic.String, fmt.Stringer, []string, *string,
int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
bool, *bool
nil

func NewStringPtr

func NewStringPtr(value interface{}) (*String, error)

NewStringPtr returns a pointer to a new String

See NewString for information on valid values and usage

func (*String) Bool

func (s *String) Bool() (bool, error)

func (*String) Bytes

func (s *String) Bytes() []byte

func (*String) Clear

func (s *String) Clear()

func (*String) Compare

func (s *String) Compare(value interface{}) (int, error)

Compare returns an integer comparing two strings lexicographically.

The result will be 0 if s==value, -1 if s < value, and +1 if s > value.

func (*String) Complex

func (s *String) Complex() (complex128, error)

func (*String) Complex128

func (s *String) Complex128() (complex128, error)

func (*String) Complex64

func (s *String) Complex64() (complex64, error)

func (*String) Contains

func (s *String) Contains(value interface{}) (bool, error)

Contains reports whether the formatted value is within s.

func (*String) ContainsAny

func (s *String) ContainsAny(value interface{}) (bool, error)

ContainsAny reports whether any Unicode code points in chars are within s.

func (*String) ContainsRune

func (s *String) ContainsRune(r rune) bool

func (*String) Copy

func (s *String) Copy() (*String, error)

func (*String) Count

func (s *String) Count(value interface{}) int

Count counts the number of non-overlapping instances of substr in s.

If substr is an empty string, Count returns 1 + the number of Unicode code points in s.

func (*String) Dereference

func (s *String) Dereference() String

func (*String) Duration

func (s *String) Duration() (time.Duration, error)

func (*String) EncodeNilToEmptyString

func (s *String) EncodeNilToEmptyString()

func (*String) EncodeNilToNull

func (s *String) EncodeNilToNull()

func (*String) Equal

func (s *String) Equal(value interface{}) (bool, error)

Equal reports whether the formatted value is equal to the underlying value of *String s.

func (*String) EqualFold

func (s *String) EqualFold(value interface{}) (bool, error)

EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding, which is a more general form of case-insensitivity.

func (*String) Fields

func (s *String) Fields() ([]string, error)

Fields splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an empty slice if s contains only white space.

func (*String) FieldsFunc

func (s *String) FieldsFunc(f func(rune) bool) []string

func (*String) Float

func (s *String) Float() (float64, error)

func (*String) Float32

func (s *String) Float32() (float32, error)

func (*String) Float64

func (s *String) Float64() (float64, error)

func (*String) HasPrefix

func (s *String) HasPrefix(value interface{}, timeLayouts ...string) (bool, error)

HasPrefix tests whether the string s begins with prefix.

func (*String) HasSuffix

func (s *String) HasSuffix(value interface{}) (bool, error)

HasSuffix tests whether the string s ends with suffix.

func (*String) HasValue

func (s *String) HasValue() bool

func (*String) Index

func (s *String) Index(value interface{}) (int, error)

Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.

func (*String) IndexAny

func (s *String) IndexAny(value interface{}) (int, error)

IndexAny returns the index of the first instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.

func (*String) IndexByte

func (s *String) IndexByte(c byte) int

IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.

func (*String) IndexFunc

func (s *String) IndexFunc(fn func(r rune) bool) int

IndexFunc returns the index into s of the first Unicode code point satisfying f(c), or -1 if none do.

func (*String) IndexRune

func (s *String) IndexRune(r rune) int

IndexRune returns the index of the first instance of the Unicode code point r, or -1 if rune is not present in s. If r is utf8.RuneError, it returns the first instance of any invalid UTF-8 byte sequence.

func (*String) Int

func (s *String) Int() (int64, error)

func (*String) IsEmpty

func (s *String) IsEmpty() bool

func (*String) IsNil

func (s *String) IsNil() bool

func (*String) IsNull

func (s *String) IsNull() bool

IsNull reports whether s is nil or equal to "null"

func (*String) LastIndex

func (s *String) LastIndex(value interface{}) (int, error)

LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.

func (*String) LastIndexByte

func (s *String) LastIndexByte(c byte) int

LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.

func (*String) LastIndexFunc

func (s *String) LastIndexFunc(fn func(r rune) bool) int

LastIndexFunc returns the index into s of the last Unicode code point satisfying f(c), or -1 if none do.

func (*String) Len

func (s *String) Len() int

func (*String) Map

func (s *String) Map(mapping func(rune) rune) (*String, error)

Map returns a copy of *String s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.

func (String) MarshalJSON

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

func (*String) NewReader

func (s *String) NewReader() *strings.Reader

func (*String) Reference

func (s *String) Reference() *String

func (*String) Replace

func (s *String) Replace(old interface{}, new interface{}, n int) (*String, error)

Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no limit on the number of replacements.

func (*String) ReplaceAll

func (s *String) ReplaceAll(old interface{}, new interface{}) (*String, error)

ReplaceAll returns a copy of the string s with all non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.

func (*String) Set

func (s *String) Set(value interface{}, timeLayout ...string) error

func (*String) Split

func (s *String) Split(sep interface{}) ([]string, error)

Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.

If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.

If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice.

It is equivalent to SplitN with a count of -1.

func (*String) SplitAfter

func (s *String) SplitAfter(sep interface{}) ([]string, error)

SplitAfter slices s into all substrings after each instance of sep and returns a slice of those substrings.

If s does not contain sep and sep is not empty, SplitAfter returns a slice of length 1 whose only element is s.

If sep is empty, SplitAfter splits after each UTF-8 sequence. If both s and sep are empty, SplitAfter returns an empty slice.

It is equivalent to SplitAfterN with a count of -1.

func (*String) SplitAfterN

func (s *String) SplitAfterN(sep interface{}, n int) ([]string, error)

func (*String) SplitN

func (s *String) SplitN(sep interface{}, n int) ([]string, error)

SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators.

The count determines the number of substrings to return:

func (*String) String

func (s *String) String() string

func (*String) Time

func (s *String) Time(layout string) (time.Time, error)

func (*String) Title

func (s *String) Title() (*String, error)

Title returns a copy of the *String s with all Unicode letters that begin words mapped to their Unicode title case.

BUG(rsc): The rule Title uses for word boundaries does not handle Unicode punctuation properly.

func (*String) ToLower

func (s *String) ToLower() (*String, error)

ToLower returns a new *String with all Unicode letters mapped to their lower case.

func (*String) ToLowerSpecial

func (s *String) ToLowerSpecial(c unicode.SpecialCase) (*String, error)

ToLowerSpecial returns a copy of the *String s with all Unicode letters mapped to their lower case using the case mapping specified by c.

func (*String) ToTitle

func (s *String) ToTitle() (*String, error)

ToTitle returns a copy of the *String s with all Unicode letters mapped to their Unicode title case.

func (*String) ToTitleSpecial

func (s *String) ToTitleSpecial(c unicode.SpecialCase) (*String, error)

ToTitleSpecial returns a copy of the *String s with all Unicode letters mapped to their Unicode title case, giving priority to the special casing rules.

func (*String) ToUpper

func (s *String) ToUpper() (*String, error)

ToUpper returns s with all Unicode letters mapped to their upper case.

func (*String) ToUpperSpecial

func (s *String) ToUpperSpecial(c unicode.SpecialCase) (*String, error)

ToUpperSpecial returns a copy of the *String s with all Unicode letters mapped to their upper case using the case mapping specified by c.

func (*String) ToValidUTF8

func (s *String) ToValidUTF8(replacement interface{}) (*String, error)

ToValidUTF8 returns a copy of the string s with each run of invalid UTF-8 byte sequences replaced by the replacement string, which may be empty.

func (*String) Uint

func (s *String) Uint() (uint64, error)

func (*String) UnmarshalJSON

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

func (*String) Value

func (s *String) Value() interface{}

type StringNumberBoolOrTime

type StringNumberBoolOrTime struct {
	// contains filtered or unexported fields
}

func NewStringNumberBoolOrTime

func NewStringNumberBoolOrTime(value interface{}) (StringNumberBoolOrTime, error)

NewStringNumberBoolOrTime returns a new StringNumberBoolOrTime set to the first value, if any.

String can be set to any of the following:

string, []byte, dynamic.String, fmt.Stringer, []string, *string,
time.Time, *time.Time,
int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
bool, *bool
nil

func NewStringNumberBoolOrTimePtr

func NewStringNumberBoolOrTimePtr(value interface{}) (*StringNumberBoolOrTime, error)

func (*StringNumberBoolOrTime) Bool

func (snbt *StringNumberBoolOrTime) Bool() (bool, bool)

func (*StringNumberBoolOrTime) Clear

func (snbt *StringNumberBoolOrTime) Clear()

Clear sets the value of StringNumberBoolOrTime to nil

func (*StringNumberBoolOrTime) Float32

func (snbt *StringNumberBoolOrTime) Float32() (float32, bool)

func (*StringNumberBoolOrTime) Float64

func (snbt *StringNumberBoolOrTime) Float64() (float64, bool)

func (StringNumberBoolOrTime) Format

func (snbt StringNumberBoolOrTime) Format(layout string) string

Format is used only with time. If the value of StringNumberBoolOrTime is anything else, the string representation will be returned

func (*StringNumberBoolOrTime) Int

func (snbt *StringNumberBoolOrTime) Int() (int, bool)

func (*StringNumberBoolOrTime) Int16

func (snbt *StringNumberBoolOrTime) Int16() (int16, bool)

func (*StringNumberBoolOrTime) Int32

func (snbt *StringNumberBoolOrTime) Int32() (int32, bool)

func (*StringNumberBoolOrTime) Int64

func (snbt *StringNumberBoolOrTime) Int64() (int64, bool)

func (*StringNumberBoolOrTime) Int8

func (snbt *StringNumberBoolOrTime) Int8() (int8, bool)

func (*StringNumberBoolOrTime) IsBool

func (snbt *StringNumberBoolOrTime) IsBool() bool

IsBool reports whether the value is a boolean or a string representation of a boolean.

The underlying type of StringNumberBoolOrTime is altered from a string to a bool if the value is successfully parsed.

func (*StringNumberBoolOrTime) IsEmptyString

func (snbt *StringNumberBoolOrTime) IsEmptyString() bool

IsEmptyString returns true if snbt is nil or an empty string

func (StringNumberBoolOrTime) IsNil

func (snbt StringNumberBoolOrTime) IsNil() bool

func (*StringNumberBoolOrTime) IsNumber

func (snbt *StringNumberBoolOrTime) IsNumber() bool

func (StringNumberBoolOrTime) IsString

func (snbt StringNumberBoolOrTime) IsString() bool

IsString reports whether the value is a string

Note: if you've used any of the other type checks before this that reported true, the string may have been cast and is no longer a string.

func (*StringNumberBoolOrTime) IsTime

func (snbt *StringNumberBoolOrTime) IsTime(layout ...string) bool

func (StringNumberBoolOrTime) MarshalJSON

func (snbt StringNumberBoolOrTime) MarshalJSON() ([]byte, error)

func (*StringNumberBoolOrTime) Number

func (snbt *StringNumberBoolOrTime) Number() interface{}

Number returns the underlying value of Number. It could be: int64, uint64, float64, or nil. If you need specific types, use their corresponding methods.

func (*StringNumberBoolOrTime) Set

func (snbt *StringNumberBoolOrTime) Set(value interface{}) error

Set sets the value of StringNumberBoolOrTime to value. The type of value can be any of the following:

time.Time, *time.Time
string, *string
json.Number, *json.Number
float64, *float64, float32, *float32
int, *int, int64, *int64, int32, *int32, int16, *int16, int8, *int8
uint, *uint, uint64, *uint64, uint32, *uint32, uint16, *uint16, uint8, *uint8
bool, *bool
fmt.Stringer
nil

All pointer values are dereferenced.

Set returns an error if value is not one of the aforementioned types

func (StringNumberBoolOrTime) String

func (snbt StringNumberBoolOrTime) String() string

func (*StringNumberBoolOrTime) Time

func (snbt *StringNumberBoolOrTime) Time(layout ...string) (time.Time, bool)

Time returns the Time value and true. If the original value is a string, Time attempts to parse it with the DefaultTimeLayouts or the provided layouts

func (*StringNumberBoolOrTime) Uint

func (snbt *StringNumberBoolOrTime) Uint() (uint, bool)

func (*StringNumberBoolOrTime) Uint16

func (snbt *StringNumberBoolOrTime) Uint16() (uint16, bool)

func (*StringNumberBoolOrTime) Uint32

func (snbt *StringNumberBoolOrTime) Uint32() (uint32, bool)

func (*StringNumberBoolOrTime) Uint64

func (snbt *StringNumberBoolOrTime) Uint64() (uint64, bool)

func (*StringNumberBoolOrTime) Uint8

func (snbt *StringNumberBoolOrTime) Uint8() (uint8, bool)

func (*StringNumberBoolOrTime) UnmarshalJSON

func (snbt *StringNumberBoolOrTime) UnmarshalJSON(data []byte) error

func (StringNumberBoolOrTime) Value

func (snbt StringNumberBoolOrTime) Value() interface{}

type StringNumberOrTime

type StringNumberOrTime struct {
	// contains filtered or unexported fields
}

func NewStringNumberOrTime

func NewStringNumberOrTime(value interface{}) (StringNumberOrTime, error)

NewStringNumberOrTime returns a new StringNumberOrTime set to the first value, if any.

You can set String to any of the following:

string, []byte, dynamic.String, fmt.Stringer, []string, *string,
time.Time, *time.Time,
int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
bool, *bool
nil

If you need type checking, use Set

func NewStringNumberOrTimePtr

func NewStringNumberOrTimePtr(value interface{}) (*StringNumberOrTime, error)

NewStringNumberOrTimePtr returns a pointer to a new NewStringNumberOrTimePtr

See NewNewStringNumberOrTime for info & warnings

func (*StringNumberOrTime) Clear

func (snt *StringNumberOrTime) Clear()

Clear sets the value of StringNumberOrTime to nil

func (*StringNumberOrTime) Float32

func (snt *StringNumberOrTime) Float32() (float32, bool)

func (*StringNumberOrTime) Float64

func (snt *StringNumberOrTime) Float64() (float64, bool)

func (StringNumberOrTime) Format

func (snt StringNumberOrTime) Format(layout string) string

Format is used only with time. If the value of StringNumberOrTime is anything else, the string representation will be returned

func (*StringNumberOrTime) Int

func (snt *StringNumberOrTime) Int() (int, bool)

func (*StringNumberOrTime) Int16

func (snt *StringNumberOrTime) Int16() (int16, bool)

func (*StringNumberOrTime) Int32

func (snt *StringNumberOrTime) Int32() (int32, bool)

func (*StringNumberOrTime) Int64

func (snt *StringNumberOrTime) Int64() (int64, bool)

func (*StringNumberOrTime) Int8

func (snt *StringNumberOrTime) Int8() (int8, bool)

func (StringNumberOrTime) IsNil

func (snt StringNumberOrTime) IsNil() bool

func (*StringNumberOrTime) IsNilOrEmpty

func (snt *StringNumberOrTime) IsNilOrEmpty() bool

func (*StringNumberOrTime) IsNilOrZero

func (snt *StringNumberOrTime) IsNilOrZero() bool

IsNilOrZero indiciates whether SNT is nil, empty string, or zero value

func (*StringNumberOrTime) IsNumber

func (snt *StringNumberOrTime) IsNumber() bool

func (StringNumberOrTime) IsString

func (snt StringNumberOrTime) IsString() bool

IsString reports whether the value is a string

func (*StringNumberOrTime) IsTime

func (snt *StringNumberOrTime) IsTime(layout ...string) bool

func (StringNumberOrTime) MarshalJSON

func (snt StringNumberOrTime) MarshalJSON() ([]byte, error)

func (*StringNumberOrTime) Number

func (snt *StringNumberOrTime) Number() interface{}

Number returns the underlying value of Number. It could be: int64, uint64, float64, or nil. If you need specific types, use their corresponding methods.

func (*StringNumberOrTime) Set

func (snt *StringNumberOrTime) Set(value interface{}) error

Set sets the value of StringNumberOrTime to value. The type of value can be any of the following:

string, []byte, dynamic.String, fmt.Stringer, []string, *string,
time.Time, *time.Time,
int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
bool, *bool
nil

All pointer values are dereferenced.

Set returns an error if value is not one of the aforementioned types

func (StringNumberOrTime) String

func (snt StringNumberOrTime) String() string

func (*StringNumberOrTime) Time

func (snt *StringNumberOrTime) Time(layout ...string) (time.Time, bool)

Time returns the Time value and true. If the original value is a string, Time attempts to parse it with the DefaultTimeLayouts or the provided layouts

func (*StringNumberOrTime) Uint

func (snt *StringNumberOrTime) Uint() (uint, bool)

func (*StringNumberOrTime) Uint16

func (snt *StringNumberOrTime) Uint16() (uint16, bool)

func (*StringNumberOrTime) Uint32

func (snt *StringNumberOrTime) Uint32() (uint32, bool)

func (*StringNumberOrTime) Uint64

func (snt *StringNumberOrTime) Uint64() (uint64, bool)

func (*StringNumberOrTime) Uint8

func (snt *StringNumberOrTime) Uint8() (uint8, bool)

func (*StringNumberOrTime) UnmarshalJSON

func (snt *StringNumberOrTime) UnmarshalJSON(data []byte) error

func (StringNumberOrTime) Value

func (snt StringNumberOrTime) Value() interface{}

type StringOrArrayOfStrings

type StringOrArrayOfStrings []string

func (*StringOrArrayOfStrings) Add

func (sas *StringOrArrayOfStrings) Add(v string)

func (StringOrArrayOfStrings) GetIndex

func (sas StringOrArrayOfStrings) GetIndex(i int) (string, error)

func (StringOrArrayOfStrings) IndexOf

func (sas StringOrArrayOfStrings) IndexOf(v string) int

func (*StringOrArrayOfStrings) IsEmpty

func (sas *StringOrArrayOfStrings) IsEmpty() bool

func (*StringOrArrayOfStrings) IsNil

func (sas *StringOrArrayOfStrings) IsNil() bool

func (*StringOrArrayOfStrings) Iterate

func (sas *StringOrArrayOfStrings) Iterate(fn func(string) error) error

Iterate calls fn for each value of sas until an error is returned. If the error returned is dynamic.Done, nil is returned to the caller. Otherwise the error returned from fn is passed along.

func (StringOrArrayOfStrings) MarshalJSON

func (sas StringOrArrayOfStrings) MarshalJSON() ([]byte, error)

MarshalJSON satisfies json.Marshaler interface

func (*StringOrArrayOfStrings) RemoveIndex

func (sas *StringOrArrayOfStrings) RemoveIndex(i int) error

func (*StringOrArrayOfStrings) Set

func (sas *StringOrArrayOfStrings) Set(v interface{}) error

func (*StringOrArrayOfStrings) UnmarshalJSON

func (sas *StringOrArrayOfStrings) UnmarshalJSON(data []byte) error

UnmarshalJSON satisfies json.Unmarshaler interface

type StringOrNumber

type StringOrNumber struct {
	// contains filtered or unexported fields
}

func NewStringOrNumber

func NewStringOrNumber(value interface{}) (StringOrNumber, error)

NewStringOrNumber returns a new StringOrNumber set to the first value, if any.

String can be set to any of the following:

string, []byte, dynamic.String, fmt.Stringer, []string, *string,
time.Time, *time.Time,
int, int64, int32, int16, int8, *int, *int64, *int32, *int16, *int8,
uint, uint64, uint32, uint16, uint8, *uint, *uint64, *uint32, *uint16, *uint8
float64, float32, complex128, complex64, *float64, *float32, *complex128, *complex64
bool, *bool
nil

checking, use:

v := &dynamic.StringOrNumber{}
err := v.Set(myType)

func NewStringOrNumberPtr

func NewStringOrNumberPtr(value interface{}) (*StringOrNumber, error)

func (*StringOrNumber) Bool

func (sn *StringOrNumber) Bool() (bool, bool)

func (*StringOrNumber) Clear

func (sn *StringOrNumber) Clear()

Clear sets the value of StringOrNumber to nil

func (*StringOrNumber) Duration

func (sn *StringOrNumber) Duration() (time.Duration, bool)

func (*StringOrNumber) Float32

func (sn *StringOrNumber) Float32() (float32, bool)

func (*StringOrNumber) Float64

func (sn *StringOrNumber) Float64() (float64, bool)

func (*StringOrNumber) Int

func (sn *StringOrNumber) Int() (int, bool)

func (*StringOrNumber) Int16

func (sn *StringOrNumber) Int16() (int16, bool)

func (*StringOrNumber) Int32

func (sn *StringOrNumber) Int32() (int32, bool)

func (*StringOrNumber) Int64

func (sn *StringOrNumber) Int64() (int64, bool)

func (*StringOrNumber) Int8

func (sn *StringOrNumber) Int8() (int8, bool)

func (*StringOrNumber) IsBool

func (sn *StringOrNumber) IsBool() bool

IsBool reports whether the value is a boolean or a string representation of a boolean.

The underlying type of StringOrNumber is altered from a string to a bool if the value is successfully parsed.

func (*StringOrNumber) IsEmptyString

func (sn *StringOrNumber) IsEmptyString() bool

IsEmptyString returns true if sn is nil or an empty string

func (StringOrNumber) IsNil

func (sn StringOrNumber) IsNil() bool

func (*StringOrNumber) IsNumber

func (sn *StringOrNumber) IsNumber() bool

func (StringOrNumber) IsString

func (sn StringOrNumber) IsString() bool

IsString reports whether the value is a string

Note: if you've used any of the other type checks before this that reported true, the string may have been cast and is no longer a string.

func (*StringOrNumber) IsTime

func (sn *StringOrNumber) IsTime(layout ...string) bool

func (StringOrNumber) MarshalJSON

func (sn StringOrNumber) MarshalJSON() ([]byte, error)

func (*StringOrNumber) Number

func (sn *StringOrNumber) Number() interface{}

Number returns the underlying value of Number. It could be: int64, uint64, float64, or nil. If you need specific types, use their corresponding methods.

func (*StringOrNumber) Set

func (sn *StringOrNumber) Set(value interface{}) error

Set sets the value of StringOrNumber to value. The type of value can be any of the following:

time.Time, *time.Time
string, *string
json.Number, *json.Number
float64, *float64, float32, *float32
int, *int, int64, *int64, int32, *int32, int16, *int16, int8, *int8
uint, *uint, uint64, *uint64, uint32, *uint32, uint16, *uint16, uint8, *uint8
bool, *bool
fmt.Stringer
nil

All pointer values are dereferenced.

Set returns an error if value is not one of the aforementioned types

func (StringOrNumber) String

func (sn StringOrNumber) String() string

func (*StringOrNumber) Time

func (sn *StringOrNumber) Time(layout ...string) (time.Time, bool)

Time returns the Time value and true. If the original value is a string, Time attempts to parse it with the DefaultTimeLayouts or the provided layouts

func (*StringOrNumber) Uint

func (sn *StringOrNumber) Uint() (uint, bool)

func (*StringOrNumber) Uint16

func (sn *StringOrNumber) Uint16() (uint16, bool)

func (*StringOrNumber) Uint32

func (sn *StringOrNumber) Uint32() (uint32, bool)

func (*StringOrNumber) Uint64

func (sn *StringOrNumber) Uint64() (uint64, bool)

func (*StringOrNumber) Uint8

func (sn *StringOrNumber) Uint8() (uint8, bool)

func (*StringOrNumber) UnmarshalJSON

func (sn *StringOrNumber) UnmarshalJSON(data []byte) error

func (StringOrNumber) Value

func (sn StringOrNumber) Value() interface{}

type Time

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

func (*Time) Clear

func (t *Time) Clear()

func (*Time) Format

func (t *Time) Format(layout string) string

func (Time) HasValue

func (t Time) HasValue() bool

func (Time) IsNil

func (t Time) IsNil() bool

func (Time) MarshalJSON

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

func (*Time) Parse

func (t *Time) Parse(s string, layouts ...string) error

func (*Time) Set

func (t *Time) Set(value interface{}, layout ...string) error

func (*Time) SetFormat

func (t *Time) SetFormat(str string)

func (Time) String

func (t Time) String() string

func (Time) Time

func (t Time) Time() (time.Time, bool)

Time returns the underlying value and true if not nil. If it is nil, a zero value time is returned and false, indicating as such.

func (*Time) UnmarshalJSON

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

func (Time) Value

func (t Time) Value() interface{}

Value returns the underlying time.Time pointer.

Notes

Bugs

  • The rule Title uses for word boundaries does not handle Unicode punctuation properly.

Jump to

Keyboard shortcuts

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