convert

package
v1.1.4-4 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2022 License: MIT Imports: 6 Imported by: 55

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromJson

func FromJson(value string) (interface{}, error)

Converts value from JSON string Parameters: "value" - the JSON string to convert. Returns: converted object value or null when value is null.

func FromJsonAs added in v1.1.0

func FromJsonAs(result interface{}, value string) (interface{}, error)

Converts value from JSON string to an object with specified type Parameters:

result - a references to the object that will receive a converted value.
value - the JSON string to convert.

Returns: converted object value or null when value is null.

func JsonToMap

func JsonToMap(value string) map[string]interface{}

Converts JSON string into map object or returns empty map when conversion is not possible. Parameters: "value" - the JSON string to convert. Returns: Map object value or empty map when conversion is not supported.

func JsonToMapWithDefault

func JsonToMapWithDefault(value string, defaultValue map[string]interface{}) map[string]interface{}

Converts JSON string into map object or returns default map when conversion is not possible. Parameters:

"value" - the JSON string to convert.
"defaultValue" - the default value.

Returns: Map object value or default map when conversion is not supported.

func JsonToNullableMap

func JsonToNullableMap(value string) *map[string]interface{}

Converts JSON string into map object or returns null when conversion is not possible. Parameters: "value" - the JSON string to convert. Returns: Map object value or null when conversion is not supported.

func ListToArray

func ListToArray(value interface{}) []interface{}

Converts value into array object with empty array as default. Strings with comma-delimited values are split into array of strings. Parameters:

"value" - the list to convert.

Returns: array object or empty array when value is null

func ToArray

func ToArray(value interface{}) []interface{}

Converts value into array object with empty array as default. Single values are converted into arrays with single element. Parameters:

"value" - the value to convert.

Returns: array object or empty array when value is null.

func ToArrayWithDefault

func ToArrayWithDefault(value interface{}, defaultValue []interface{}) []interface{}

Converts value into array object with empty array as default. Single values are converted into arrays with single element. Parameters:

"value" - the value to convert.
"defaultValue" - default array object.

Returns: array object or empty array when value is null.

func ToBoolean

func ToBoolean(value interface{}) bool

Converts value into boolean or returns false when conversion is not possible. Parameters: "value" - the value to convert. Returns: boolean value or false when conversion is not supported.

func ToBooleanWithDefault

func ToBooleanWithDefault(value interface{}, defaultValue bool) bool

Converts value into boolean or returns default value when conversion is not possible Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: boolean value or default when conversion is not supported.

func ToDateTime

func ToDateTime(value interface{}) time.Time

Converts value into Date or returns current when conversion is not possible. Parameters: "value" - the value to convert. Returns: Date value or current when conversion is not supported.

func ToDateTimeWithDefault

func ToDateTimeWithDefault(value interface{}, defaultValue time.Time) time.Time

Converts value into Date or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: Date value or default when conversion is not supported.

func ToDouble

func ToDouble(value interface{}) float64

Converts value into doubles or returns 0 when conversion is not possible. Parameters: "value" - the value to convert. Returns: double value or 0 when conversion is not supported.

func ToDoubleWithDefault

func ToDoubleWithDefault(value interface{}, defaultValue float64) float64

Converts value into doubles or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: double value or default when conversion is not supported.

func ToDuration

func ToDuration(value interface{}) time.Duration

Converts value into time.Duration or returns current when conversion is not possible. Parameters: "value" - the value to convert. Returns: time.Duration value or current when conversion is not supported.

func ToDurationWithDefault

func ToDurationWithDefault(value interface{}, defaultValue time.Duration) time.Duration

Converts value into time.Duration or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: time.Duration value or default when conversion is not supported.

func ToFloat

func ToFloat(value interface{}) float32

Converts value into float or returns 0 when conversion is not possible. Parameters: "value" - the value to convert. Returns: float value or 0 when conversion is not supported.

func ToFloatWithDefault

func ToFloatWithDefault(value interface{}, defaultValue float32) float32

Converts value into float or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: float value or default when conversion is not supported.

func ToInteger

func ToInteger(value interface{}) int

Converts value into integer or returns 0 when conversion is not possible. Parameters: "value" - the value to convert Returns: integer value or 0 when conversion is not supported.

func ToIntegerWithDefault

func ToIntegerWithDefault(value interface{}, defaultValue int) int

Converts value into integer or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: integer value or default when conversion is not supported.

func ToJson

func ToJson(value interface{}) (string, error)

Converts value into JSON string. Parameters: "value" - the value to convert. Returns: JSON string or null when value is null.

func ToLong

func ToLong(value interface{}) int64

Converts value into long or returns 0 when conversion is not possible. Parameters: "value" - the value to convert Returns: long value or 0 when conversion is not supported.

func ToLongWithDefault

func ToLongWithDefault(value interface{}, defaultValue int64) int64

Converts value into long or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value..

Returns: long value or default when conversion is not supported.

func ToMap

func ToMap(value interface{}) map[string]interface{}

Converts value into map object or returns empty map when conversion is not possible. Parameters: "value" - the value to convert Returns: map object or empty map when conversion is not supported.

func ToMapWithDefault

func ToMapWithDefault(value interface{}, defaultValue map[string]interface{}) map[string]interface{}

Converts value into map object or returns default map when conversion is not possible. Parameters:

"value" - the value to convert
"defaultValue" - the default value.

Returns: map object or default map when conversion is not supported.

func ToNullableArray

func ToNullableArray(value interface{}) *[]interface{}

Converts value into array object. Single values are converted into arrays with a single element. Parameters:

"value" - the value to convert.

Returns: array object or null when value is null.

func ToNullableBoolean

func ToNullableBoolean(value interface{}) *bool

Converts value into boolean or returns null when conversion is not possible. Parameters: "value" - the value to convert. Returns: boolean value or null when conversion is not supported.

func ToNullableDateTime

func ToNullableDateTime(value interface{}) *time.Time

Converts value into Date or returns null when conversion is not possible. Parameters: "value" - the value to convert. Returns: Date value or null when conversion is not supported.

func ToNullableDouble

func ToNullableDouble(value interface{}) *float64

Converts value into doubles or returns null when conversion is not possible. Parameters: "value" - the value to convert. Returns: double value or null when conversion is not supported.

func ToNullableDuration

func ToNullableDuration(value interface{}) *time.Duration

Converts value into time.Duration or returns null when conversion is not possible. Parameters: "value" - the value to convert. Returns: time.Duration value or null when conversion is not supported.

func ToNullableFloat

func ToNullableFloat(value interface{}) *float32

Converts value into float or returns null when conversion is not possible. Parameters: "value" - the value to convert. Returns: float value or null when conversion is not supported.

func ToNullableInteger

func ToNullableInteger(value interface{}) *int

Converts value into integer or returns null when conversion is not possible. Parameters: "value" - the value to convert Returns: integer value or null when conversion is not supported.

func ToNullableLong

func ToNullableLong(value interface{}) *int64

Converts value into long or returns null when conversion is not possible. Parameters: "value" - the value to convert Returns: long value or null when conversion is not supported.

func ToNullableMap

func ToNullableMap(value interface{}) *map[string]interface{}

Converts value into map object or returns null when conversion is not possible. Parameters: "value" - the value to convert Returns: map object or null when conversion is not supported.

func ToNullableString

func ToNullableString(value interface{}) *string

Converts value into string or returns null when value is null. Parameters: "value" - the value to convert Returns: string value or null when value is null.

func ToNullableType

func ToNullableType(typ TypeCode, value interface{}) interface{}

Converts value into an object type specified by Type Code or returns null when conversion is not possible. Parameters:

"typ" - the TypeCode for the data type.
"value" - the value to convert.

Returns: object value of type corresponding to TypeCode, or null when conversion is not supported.

func ToNullableUInteger added in v1.1.1

func ToNullableUInteger(value interface{}) *uint

Converts value into unsigned integer or returns null when conversion is not possible. Parameters: "value" - the value to convert Returns: integer value or null when conversion is not supported.

func ToNullableULong added in v1.1.1

func ToNullableULong(value interface{}) *uint64

Converts value into unsigned long or returns null when conversion is not possible. Parameters: "value" - the value to convert Returns: long value or null when conversion is not supported.

func ToString

func ToString(value interface{}) string

Converts value into string or returns "" when value is null. Parameters: "value" - the value to convert Returns: string value or "" when value is null.

func ToStringWithDefault

func ToStringWithDefault(value interface{}, defaultValue string) string

Converts value into string or returns default when value is null. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: string value or default when value is null.

func ToType

func ToType(typ TypeCode, value interface{}) interface{}

Converts value into an object type specified by Type Code or returns default value when conversion is not possible. Parameters:

"typ" - the TypeCode for the data type into which 'value' is to be converted.
"value" - the value to convert.

Returns: object value of type corresponding to TypeCode, or default value when conversion is not supported

func ToTypeWithDefault

func ToTypeWithDefault(typ TypeCode, value interface{}, defaultValue interface{}) interface{}

Converts value into an object type specified by Type Code or returns default value when conversion is not possible. Parameters:

"typ" - the TypeCode for the data type into which 'value' is to be converted.
"value" - the value to convert.
"defaultValue" - the default value to return if conversion is not possible
(returns null).

Returns: object value of type corresponding to TypeCode, or default value when conversion is not supported

func ToUInteger added in v1.1.1

func ToUInteger(value interface{}) uint

Converts value into unsigned integer or returns 0 when conversion is not possible. Parameters: "value" - the value to convert Returns: integer value or 0 when conversion is not supported.

func ToUIntegerWithDefault added in v1.1.1

func ToUIntegerWithDefault(value interface{}, defaultValue uint) uint

Converts value into unsigned integer or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: integer value or default when conversion is not supported.

func ToULong added in v1.1.1

func ToULong(value interface{}) uint64

Converts value into unsigned long or returns 0 when conversion is not possible. Parameters: "value" - the value to convert Returns: long value or 0 when conversion is not supported.

func ToULongWithDefault added in v1.1.1

func ToULongWithDefault(value interface{}, defaultValue uint64) uint64

Converts value into unsigned long or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value..

Returns: long value or default when conversion is not supported.

func TypeCodeToString

func TypeCodeToString(typ TypeCode) string

Converts a TypeCode into its string name. Parameters: "typ" - the TypeCode to convert into a string. Returns: the name of the TypeCode passed as a string value.

Types

type TArrayConverter

type TArrayConverter struct{}

Converts arbitrary values into array objects.

Example:

value1 := convert.ArrayConverter.ToArray([...]int{1, 2})
value2 := convert.ArrayConverter.ToArray(1)
value3 := convert.ArrayConverter.ListToArray("1,2,3")
fmt.Println(value1) // [1 2]
fmt.Println(value2) // [1]
fmt.Println(value3) // [1 2 3]
var ArrayConverter *TArrayConverter = &TArrayConverter{}

func (*TArrayConverter) ListToArray

func (c *TArrayConverter) ListToArray(value interface{}) []interface{}

Converts value into array object with empty array as default. Strings with comma-delimited values are split into array of strings. Parameters:

"value" - the list to convert.

Returns: array object or empty array when value is null

func (*TArrayConverter) ToArray

func (c *TArrayConverter) ToArray(value interface{}) []interface{}

Converts value into array object with empty array as default. Single values are converted into arrays with single element. Parameters: "value" - the value to convert. Returns: array object or empty array when value is null.

func (*TArrayConverter) ToArrayWithDefault

func (c *TArrayConverter) ToArrayWithDefault(value interface{}, defaultValue []interface{}) []interface{}

Converts value into array object with empty array as default. Single values are converted into arrays with single element. Parameters:

"value" - the value to convert.
"defaultValue" - default array object.

Returns: array object or empty array when value is null.

func (*TArrayConverter) ToNullableArray

func (c *TArrayConverter) ToNullableArray(value interface{}) *[]interface{}

Converts value into array object. Single values are converted into arrays with a single element. Parameters: "value" - the value to convert. Returns: array object or null when value is null.

type TBooleanConverter

type TBooleanConverter struct{}

Converts arbitrary values to boolean values using extended conversion rules: - Numbers: above 0, less more 0 are true; equal to 0 are false - Strings: "true", "yes", "T", "Y", "1" are true, "false", "no", "F", "N" are false - DateTime: above 0, less more 0 total milliseconds are true, equal to 0 are false

Example:

value1 := convert.BooleanConverter.ToNullableBoolean(true)
value2 := convert.BooleanConverter.ToNullableBoolean("yes")
value3 := convert.BooleanConverter.ToNullableBoolean(1)
value4 := convert.BooleanConverter.ToNullableBoolean(struct{}{})
fmt.Println(*value1) // true
fmt.Println(*value2) // true
fmt.Println(*value3) // true
fmt.Println(value4)  // <nil>
var BooleanConverter *TBooleanConverter = &TBooleanConverter{}

func (*TBooleanConverter) ToBoolean

func (c *TBooleanConverter) ToBoolean(value interface{}) bool

Converts value into boolean or returns false when conversion is not possible. Parameters: "value" - the value to convert. Returns: boolean value or false when conversion is not supported.

func (*TBooleanConverter) ToBooleanWithDefault

func (c *TBooleanConverter) ToBooleanWithDefault(value interface{}, defaultValue bool) bool

Converts value into boolean or returns default value when conversion is not possible Parameters:"value" - the value to convert.

"defaultValue" - the default value

Returns: boolean value or default when conversion is not supported.

func (*TBooleanConverter) ToNullableBoolean

func (c *TBooleanConverter) ToNullableBoolean(value interface{}) *bool

Converts value into boolean or returns null when conversion is not possible. Parameters: "value" - the value to convert. Returns: boolean value or null when conversion is not supported.

type TDateTimeConverter

type TDateTimeConverter struct{}

Converts arbitrary values into Date values using extended conversion rules: - Strings: converted using ISO time format - Numbers: converted using milliseconds since unix epoch

Example:

value1 := convert.DateTimeConverter.ToNullableDateTime("ABC")
value2 := convert.DateTimeConverter.ToNullableDateTime("2019-01-01T11:30:00.0Z")
value3 := convert.DateTimeConverter.ToNullableDateTime(123)
fmt.Println(value1) // <nil>
fmt.Println(value2) // 2019-01-01 11:30:00 +0000 UTC
fmt.Println(value3) // 1970-01-01 02:02:03 +0200 EET
var DateTimeConverter *TDateTimeConverter = &TDateTimeConverter{}

func (*TDateTimeConverter) ToDateTime

func (c *TDateTimeConverter) ToDateTime(value interface{}) time.Time

Converts value into Date or returns current when conversion is not possible. Parameters: "value" - the value to convert. Returns: Date value or current when conversion is not supported.

func (*TDateTimeConverter) ToDateTimeWithDefault

func (c *TDateTimeConverter) ToDateTimeWithDefault(value interface{}, defaultValue time.Time) time.Time

Converts value into Date or returns default when conversion is not possible. Parameters: "value" - the value to convert. "defaultValue" - the default value. Returns: Date value or default when conversion is not supported.

func (*TDateTimeConverter) ToNullableDateTime

func (c *TDateTimeConverter) ToNullableDateTime(value interface{}) *time.Time

Converts value into Date or returns null when conversion is not possible. Parameters: "value" - the value to convert. Returns: Date value or null when conversion is not supported.

type TDoubleConverter

type TDoubleConverter struct{}

Converts arbitrary values into double using extended conversion rules: - Strings are converted to double values - DateTime: total number of milliseconds since unix epoсh - Boolean: 1 for true and 0 for false

Example:

value1 := convert.DoubleConverter.ToNullableDouble("ABC")
value2 := convert.DoubleConverter.ToNullableDouble("123.456")
value3 := convert.DoubleConverter.ToNullableDouble(true)
value4 := convert.DoubleConverter.ToNullableDouble(time.Now())
fmt.Println(value1)  // <nil>
fmt.Println(*value2) // 123.456
fmt.Println(*value3) // 1
fmt.Println(*value4) // current milliseconds (e.g. 1.566333114e+09)
var DoubleConverter *TDoubleConverter = &TDoubleConverter{}

func (*TDoubleConverter) ToDouble

func (c *TDoubleConverter) ToDouble(value interface{}) float64

Converts value into doubles or returns 0 when conversion is not possible. Parameters: "value" - the value to convert. Returns: double value or 0 when conversion is not supported.

func (*TDoubleConverter) ToDoubleWithDefault

func (c *TDoubleConverter) ToDoubleWithDefault(value interface{}, defaultValue float64) float64

Converts value into doubles or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value

Returns: double value or default when conversion is not supported.

func (*TDoubleConverter) ToNullableDouble

func (c *TDoubleConverter) ToNullableDouble(value interface{}) *float64

Converts value into doubles or returns null when conversion is not possible. Parameters: "value" - the value to convert. Returns: double value or null when conversion is not supported.

type TDurationConverter

type TDurationConverter struct{}

Converts arbitrary values into time.Duration values.

Example:

value1 := convert.DurationConverter.ToNullableDuration("123")
value2 := convert.DurationConverter.ToNullableDuration(123)
value3 := convert.DurationConverter.ToNullableDuration(123 * time.Second)
fmt.Println(value1) // 123ms
fmt.Println(value2) // 123ms
fmt.Println(value3) // 2m3s
var DurationConverter *TDurationConverter = &TDurationConverter{}

func (*TDurationConverter) ToDuration

func (c *TDurationConverter) ToDuration(value interface{}) time.Duration

Converts value into time.Duration or returns current when conversion is not possible. Parameters: "value" - the value to convert. Returns: time.Duration value or current when conversion is not supported.

func (*TDurationConverter) ToDurationWithDefault

func (c *TDurationConverter) ToDurationWithDefault(value interface{}, defaultValue time.Duration) time.Duration

Converts value into time.Duration or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: time.Duration value or default when conversion is not supported.

func (*TDurationConverter) ToNullableDuration

func (c *TDurationConverter) ToNullableDuration(value interface{}) *time.Duration

Converts value into time.Duration or returns null when conversion is not possible. Parameters: "value" - the value to convert. Returns: time.Duration value or null when conversion is not supported.

type TFloatConverter

type TFloatConverter struct{}

Converts arbitrary values into float using extended conversion rules: - Strings are converted to float values - DateTime: total number of milliseconds since unix epoсh - Boolean: 1 for true and 0 for false

Example:

value1 := convert.FloatConverter.ToNullableFloat("ABC")
value2 := convert.FloatConverter.ToNullableFloat("123.456")
value3 := convert.FloatConverter.ToNullableFloat(true)
value4 := convert.FloatConverter.ToNullableFloat(time.Now())
fmt.Println(value1)  // <nil>
fmt.Println(*value2) // 123.456
fmt.Println(*value3) // 1
fmt.Println(*value4) // current milliseconds (e.g. 1.566333114e+09)
var FloatConverter *TFloatConverter = &TFloatConverter{}

func (*TFloatConverter) ToFloat

func (c *TFloatConverter) ToFloat(value interface{}) float32

Converts value into float or returns 0 when conversion is not possible. Parameters: "value" - the value to convert. Returns: float value or 0 when conversion is not supported.

func (*TFloatConverter) ToFloatWithDefault

func (c *TFloatConverter) ToFloatWithDefault(value interface{}, defaultValue float32) float32

Converts value into float or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: float value or default when conversion is not supported.

func (*TFloatConverter) ToNullableFloat

func (c *TFloatConverter) ToNullableFloat(value interface{}) *float32

Converts value into float or returns null when conversion is not possible. Parameters: "value" - the value to convert. Returns: float value or null when conversion is not supported.

type TIntegerConverter

type TIntegerConverter struct{}

Converts arbitrary values into integer using extended conversion rules: - Strings are converted to integer values - DateTime: total number of milliseconds since unix epoсh - Boolean: 1 for true and 0 for false

Example:

value1 := convert.IntegerConverter.ToNullableInteger("ABC")
value2 := convert.IntegerConverter.ToNullableInteger("123.456")
value3 := convert.IntegerConverter.ToNullableInteger(true)
value4 := convert.IntegerConverter.ToNullableInteger(time.Now())
fmt.Println(value1)  // <nil>
fmt.Println(*value2) // 123
fmt.Println(*value3) // 1
fmt.Println(*value4) // current milliseconds (e.g. 1566333428)
var IntegerConverter *TIntegerConverter = &TIntegerConverter{}

func (*TIntegerConverter) ToInteger

func (c *TIntegerConverter) ToInteger(value interface{}) int

Converts value into integer or returns 0 when conversion is not possible. Parameters: "value" - the value to convert Returns: integer value or 0 when conversion is not supported.

func (*TIntegerConverter) ToIntegerWithDefault

func (c *TIntegerConverter) ToIntegerWithDefault(value interface{}, defaultValue int) int

Converts value into integer or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: integer value or default when conversion is not supported.

func (*TIntegerConverter) ToNullableInteger

func (c *TIntegerConverter) ToNullableInteger(value interface{}) *int

Converts value into integer or returns null when conversion is not possible. Parameters: "value" - the value to convert Returns: integer value or null when conversion is not supported.

func (*TIntegerConverter) ToNullableUInteger added in v1.1.1

func (c *TIntegerConverter) ToNullableUInteger(value interface{}) *uint

Converts value into unsigned integer or returns null when conversion is not possible. Parameters: "value" - the value to convert Returns: integer value or null when conversion is not supported.

func (*TIntegerConverter) ToUInteger added in v1.1.1

func (c *TIntegerConverter) ToUInteger(value interface{}) uint

Converts value into unsigned integer or returns 0 when conversion is not possible. Parameters: "value" - the value to convert Returns: integer value or 0 when conversion is not supported.

func (*TIntegerConverter) ToUIntegerWithDefault added in v1.1.1

func (c *TIntegerConverter) ToUIntegerWithDefault(value interface{}, defaultValue uint) uint

Converts value into unsigned integer or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: integer value or default when conversion is not supported.

type TJsonConverter

type TJsonConverter struct{}

Converts arbitrary values from and to JSON (JavaScript Object Notation) strings.

Example:

value1, _ := convert.FromJson("{\"key\":123}")
value2 := convert.JsonConverter.ToMap("{\"key\":123}")
value3, _ := convert.ToJson(map[string]int{"key": 123})
fmt.Println(value1) // map[key:123]
fmt.Println(value2) // map[key:123]
fmt.Println(value3) // {"key":123}
var JsonConverter *TJsonConverter = &TJsonConverter{}

func (*TJsonConverter) ToMap

func (c *TJsonConverter) ToMap(value string) map[string]interface{}

Converts JSON string into map object or returns empty map when conversion is not possible. Parameters: "value" - the JSON string to convert. Returns: Map object value or empty map when conversion is not supported.

func (*TJsonConverter) ToMapWithDefault

func (c *TJsonConverter) ToMapWithDefault(value string, defaultValue map[string]interface{}) map[string]interface{}

Converts JSON string into map object or returns default map when conversion is not possible. Parameters:

"value" - the JSON string to convert.
"defaultValue" - the default value.

Returns: Map object value or default map when conversion is not supported.

func (*TJsonConverter) ToNullableMap

func (c *TJsonConverter) ToNullableMap(value string) *map[string]interface{}

Converts JSON string into map object or returns null when conversion is not possible. Parameters: "value" - the JSON string to convert. Returns: Map object value or null when conversion is not supported.

func (*TJsonConverter) ToObject added in v1.1.0

func (c *TJsonConverter) ToObject(value string) interface{}

Converts JSON string into an object. Parameters: "value" - the JSON string to convert. Returns: Map object value or empty map when conversion is not supported.

func (*TJsonConverter) ToObjectAs added in v1.1.0

func (c *TJsonConverter) ToObjectAs(result interface{}, value string) (interface{}, error)

Converts JSON string into an object with specified type. Parameters:

result - a references to an object that will receive the result
value - the JSON string to convert.

Returns: Map object value or empty map when conversion is not supported.

type TLongConverter

type TLongConverter struct{}

Converts arbitrary values into long using extended conversion rules: - Strings are converted to long values - DateTime: total number of milliseconds since unix epoсh - Boolean: 1 for true and 0 for false

Example:

value1 := convert.LongConverter.ToNullableLong("ABC")
value2 := convert.LongConverter.ToNullableLong("123.456")
value3 := convert.LongConverter.ToNullableLong(true)
value4 := convert.LongConverter.ToNullableLong(time.Now())
fmt.Println(value1)  // <nil>
fmt.Println(*value2) // 123
fmt.Println(*value3) // 1
fmt.Println(*value4) // current milliseconds (e.g. 1566333527)
var LongConverter *TLongConverter = &TLongConverter{}

func (*TLongConverter) ToLong

func (c *TLongConverter) ToLong(value interface{}) int64

Converts value into long or returns 0 when conversion is not possible. Parameters: "value" - the value to convert Returns: long value or 0 when conversion is not supported.

func (*TLongConverter) ToLongWithDefault

func (c *TLongConverter) ToLongWithDefault(value interface{}, defaultValue int64) int64

Converts value into long or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value..

Returns: long value or default when conversion is not supported.

func (*TLongConverter) ToNullableLong

func (c *TLongConverter) ToNullableLong(value interface{}) *int64

Converts value into long or returns null when conversion is not possible. Parameters: "value" - the value to convert Returns: long value or null when conversion is not supported.

func (*TLongConverter) ToNullableULong added in v1.1.1

func (c *TLongConverter) ToNullableULong(value interface{}) *uint64

Converts value into unsigned long or returns null when conversion is not possible. Parameters: "value" - the value to convert Returns: long value or null when conversion is not supported.

func (*TLongConverter) ToULong added in v1.1.1

func (c *TLongConverter) ToULong(value interface{}) uint64

Converts value into unsigned long or returns 0 when conversion is not possible. Parameters: "value" - the value to convert Returns: long value or 0 when conversion is not supported.

func (*TLongConverter) ToULongWithDefault added in v1.1.1

func (c *TLongConverter) ToULongWithDefault(value interface{}, defaultValue uint64) uint64

Converts value into unsigned long or returns default when conversion is not possible. Parameters:

"value" - the value to convert.
"defaultValue" - the default value..

Returns: long value or default when conversion is not supported.

type TMapConverter

type TMapConverter struct{}

Converts arbitrary values into map objects using extended conversion rules: - Objects: property names as keys, property values as values - Arrays: element indexes as keys, elements as values

Example:

value1 := convert.MapConverter.ToNullableMap("ABC")
value2 := convert.MapConverter.ToNullableMap(map[string]int{"key": 123})
value3 := convert.MapConverter.ToNullableMap([...]int{1, 2, 3})
fmt.Println(value1)  // <nil>
fmt.Println(*value2) // map[key:123]
fmt.Println(*value3) // map[0:1 1:2 2:3]
var MapConverter *TMapConverter = &TMapConverter{}

func (*TMapConverter) ToMap

func (c *TMapConverter) ToMap(value interface{}) map[string]interface{}

Converts value into map object or returns empty map when conversion is not possible. Parameters: "value" - the value to convert Returns: map object or empty map when conversion is not supported.

func (*TMapConverter) ToMapWithDefault

func (c *TMapConverter) ToMapWithDefault(value interface{}, defaultValue map[string]interface{}) map[string]interface{}

Converts value into map object or returns default map when conversion is not possible. Parameters:

"value" - the value to convert
"defaultValue" - the default value.

Returns: map object or default map when conversion is not supported.

func (*TMapConverter) ToNullableMap

func (c *TMapConverter) ToNullableMap(value interface{}) *map[string]interface{}

Converts value into map object or returns null when conversion is not possible. Parameters: "value" - the value to convert Returns: map object or null when conversion is not supported.

type TStringConverter

type TStringConverter struct{}

Converts arbitrary values into strings using extended conversion rules: - Numbers: are converted with '.' as decimal point - DateTime: using ISO format - Boolean: "true" for true and "false" for false - Arrays: as comma-separated list - Other objects: using toString() method

Example:

var value1 = convert.StringConverter.ToString(123.456)
var value2 = convert.StringConverter.ToString(true)
var value3 = convert.StringConverter.ToString(time.Now())
var value4 = convert.StringConverter.ToString([...]int{1, 2, 3})
fmt.Println(value1) // 123.456
fmt.Println(value2) // true
fmt.Println(value3) // 2019-08-20T23:54:47+03:00
fmt.Println(value4) // 1,2,3
var StringConverter *TStringConverter = &TStringConverter{}

func (*TStringConverter) ToNullableString

func (c *TStringConverter) ToNullableString(value interface{}) *string

Converts value into string or returns null when value is null. Parameters: "value" - the value to convert Returns: string value or null when value is null.

func (*TStringConverter) ToString

func (c *TStringConverter) ToString(value interface{}) string

Converts value into string or returns "" when value is null. Parameters: "value" - the value to convert Returns: string value or "" when value is null.

func (*TStringConverter) ToStringWithDefault

func (c *TStringConverter) ToStringWithDefault(value interface{}, defaultValue string) string

Converts value into string or returns default when value is null. Parameters:

"value" - the value to convert.
"defaultValue" - the default value.

Returns: string value or default when value is null.

type TTypeConverter

type TTypeConverter struct{}

Converts arbitrary values into objects specific by TypeCodes. For each TypeCode this class calls corresponding converter which applies extended conversion rules to convert the values.

Example:

value1 := convert.TypeConverter.ToType(convert.Integer, "123.456")
value2 := convert.TypeConverter.ToType(convert.DateTime, 123)
value3 := convert.TypeConverter.ToType(convert.Boolean, "F")
fmt.Println(value1) // 123
fmt.Println(value2) // 1970-01-01 02:02:03 +0200 EET
fmt.Println(value3) // false
var TypeConverter *TTypeConverter = &TTypeConverter{}

func (*TTypeConverter) ToNullableType

func (c *TTypeConverter) ToNullableType(typ TypeCode, value interface{}) interface{}

Converts value into an object type specified by Type Code or returns null when conversion is not possible. Parameters:

"typ" - the TypeCode for the data type.
"value" - the value to convert.

Returns: object value of type corresponding to TypeCode, or null when conversion is not supported.

func (*TTypeConverter) ToString

func (c *TTypeConverter) ToString(typ TypeCode) string

Converts a TypeCode into its string name. Parameters: "typ" - the TypeCode to convert into a string. Returns: the name of the TypeCode passed as a string value.

func (*TTypeConverter) ToType

func (c *TTypeConverter) ToType(typ TypeCode, value interface{}) interface{}

Converts value into an object type specified by Type Code or returns default value when conversion is not possible. Parameters:

"typ" - the TypeCode for the data type into which 'value' is to be converted.
"value" - the value to convert.

Returns: object value of type corresponding to TypeCode, or default value when conversion is not supported

func (*TTypeConverter) ToTypeCode

func (c *TTypeConverter) ToTypeCode(value interface{}) TypeCode

Gets TypeCode for specific value. Parameters: "value" - value whose TypeCode is to be resolved. Returns: the TypeCode that corresponds to the passed object's type.

func (*TTypeConverter) ToTypeWithDefault

func (c *TTypeConverter) ToTypeWithDefault(typ TypeCode, value interface{}, defaultValue interface{}) interface{}

Converts value into an object type specified by Type Code or returns default value when conversion is not possible. Parameters:

"typ" - the TypeCode for the data type into which 'value' is to be converted.
"value" - the value to convert.
"defaultValue" - the default value to return if conversion is not possible
(returns null).

Returns: object value of type corresponding to TypeCode, or default value when conversion is not supported

type TypeCode

type TypeCode int

Codes for the data types that can be converted using TypeConverter.

const (
	Unknown  TypeCode = iota
	String   TypeCode = iota
	Boolean  TypeCode = iota
	Integer  TypeCode = iota
	Long     TypeCode = iota
	Float    TypeCode = iota
	Double   TypeCode = iota
	DateTime TypeCode = iota
	Duration TypeCode = iota
	Object   TypeCode = iota
	Enum     TypeCode = iota
	Array    TypeCode = iota
	Map      TypeCode = iota
)

func ToTypeCode

func ToTypeCode(value interface{}) TypeCode

Gets TypeCode for specific value. Parameters: "value" - value whose TypeCode is to be resolved. Returns: the TypeCode that corresponds to the passed object's type.

Jump to

Keyboard shortcuts

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