convert

package
v0.0.1-2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: MIT Imports: 6 Imported by: 62

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ArrayConverter = &_TArrayConverter{}

ArrayConverter 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]
View Source
var BooleanConverter = &_TBooleanConverter{}

BooleanConverter converts arbitrary values to boolean values using extended conversion rules:

  • Numbers: above 0, less much 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 much 0 total milliseconds are true, equal to 0 are false

Example:

value1, ok1 := convert.BooleanConverter.ToNullableBoolean(true)
value2, ok2 := convert.BooleanConverter.ToNullableBoolean("yes")
value3, ok3 := convert.BooleanConverter.ToNullableBoolean(1)
value4, ok4 := convert.BooleanConverter.ToNullableBoolean(struct{}{})
fmt.Println(value1, ok1) // true, true
fmt.Println(value2, ok2) // true, true
fmt.Println(value3, ok3) // true, true
fmt.Println(value4, ok4)  // false, false
View Source
var DateTimeConverter = &_TDateTimeConverter{}

DateTimeConverter 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, ok1 := convert.DateTimeConverter.ToNullableDateTime("ABC")
value2, ok2 := convert.DateTimeConverter.ToNullableDateTime("2019-01-01T11:30:00.0Z")
value3, ok3 := convert.DateTimeConverter.ToNullableDateTime(123)
fmt.Println(value1, ok1) // 0001-01-01 00:00:00 +0000 UTC, false
fmt.Println(value2, ok2) // 2019-01-01 11:30:00 +0000 UTC, true
fmt.Println(value3, ok3) // 1970-01-01 02:02:03 +0200 EET, true
View Source
var DoubleConverter = &_TDoubleConverter{}

DoubleConverter 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, ok1 := convert.DoubleConverter.ToNullableDouble("ABC")
value2, ok2 := convert.DoubleConverter.ToNullableDouble("123.456")
value3, ok3 := convert.DoubleConverter.ToNullableDouble(true)
value4, ok4 := convert.DoubleConverter.ToNullableDouble(time.Now())
fmt.Println(value1, ok1) // 0, false
fmt.Println(value2, ok2) // 123.456, true
fmt.Println(value3, ok3) // 1, true
fmt.Println(value4, ok4) // current milliseconds (e.g. 1.566333114e+09), true
View Source
var DurationConverter = &_TDurationConverter{}

DurationConverter Converts arbitrary values into time.Duration values.

Example:

value1, ok1 := convert.DurationConverter.ToNullableDuration("123")
value2, ok2 := convert.DurationConverter.ToNullableDuration(123)
value3, ok3 := convert.DurationConverter.ToNullableDuration(123 * time.Second)
fmt.Println(value1, ok1) // 123ms, true
fmt.Println(value2, ok2) // 123ms, true
fmt.Println(value3, ok3) // 2m3s, true
View Source
var FloatConverter = &_TFloatConverter{}

FloatConverter 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, ok1 := convert.FloatConverter.ToNullableFloat("ABC")
value2, ok2 := convert.FloatConverter.ToNullableFloat("123.456")
value3, ok3 := convert.FloatConverter.ToNullableFloat(true)
value4, ok4 := convert.FloatConverter.ToNullableFloat(time.Now())
fmt.Println(value1, ok1) // 0, false
fmt.Println(value2, ok2) // 123.456, true
fmt.Println(value3, ok3) // 1, true
fmt.Println(value4, ok4) // current milliseconds (e.g. 1.566333114e+09), true
View Source
var IntegerConverter = &_TIntegerConverter{}

IntegerConverter 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, ok1 := convert.IntegerConverter.ToNullableInteger("ABC")
value2, ok2 := convert.IntegerConverter.ToNullableInteger("123.456")
value3, ok3 := convert.IntegerConverter.ToNullableInteger(true)
value4, ok4 := convert.IntegerConverter.ToNullableInteger(time.Now())
fmt.Println(value1, ok1) // 0, false
fmt.Println(value2, ok2) // 123, true
fmt.Println(value3, ok3) // 1, true
fmt.Println(value4, ok4) // current milliseconds (e.g. 1566333428), true
View Source
var JsonConverter = &_TJsonConverter[any]{
	_jsonEngine: &defaultJsonEngine[any]{},
}

JsonConverter 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}
View Source
var LongConverter = &_TLongConverter{}

LongConverter 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, ok1 := convert.LongConverter.ToNullableLong("ABC")
value2, ok2 := convert.LongConverter.ToNullableLong("123.456")
value3, ok3 := convert.LongConverter.ToNullableLong(true)
value4, ok4 := convert.LongConverter.ToNullableLong(time.Now())
fmt.Println(value1, ok1) // 0, false
fmt.Println(value2, ok2) // 123, false
fmt.Println(value3, ok3) // 1, false
fmt.Println(value4, ok4) // current milliseconds (e.g. 1566333527), false
View Source
var MapConverter = &_TMapConverter{}

MapConverter 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, ok1 := convert.MapConverter.ToNullableMap("ABC")
value2, ok2 := convert.MapConverter.ToNullableMap(map[string]int{"key": 123})
value3, ok3 := convert.MapConverter.ToNullableMap([...]int{1, 2, 3})
fmt.Println(value1, ok1) // <nil>, false
fmt.Println(value2, ok2) // map[key:123], true
fmt.Println(value3, ok3) // map[0:1 1:2 2:3], true
View Source
var StringConverter = &_TStringConverter{}

StringConverter 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:

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

TypeConverter 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

Functions

This section is empty.

Types

type IJSONEngine

type IJSONEngine[T any] interface {
	FromJson(value string) (T, error)
	ToJson(value T) (string, error)
}

IJSONEngine interface which helps to marshal and unmarshal json

func NewDefaultCustomTypeJsonConvertor

func NewDefaultCustomTypeJsonConvertor[T any]() IJSONEngine[T]

type TypeCode

type TypeCode int

TypeCode 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
)

Jump to

Keyboard shortcuts

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