as

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package as helps by converting various data types to various other types

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(valuea ...interface{}) bool

Bool returns a boolean value. It mainly depends on the output of strconv.ParseBool, but also checks for integer values.

Example
package main

import (
	"fmt"

	"simonwaldherr.de/go/golibs/as"
)

func main() {
	values := []interface{}{"foobar", "true", 1, true}

	fmt.Println("as.Bool()")
	for _, v := range values {
		fmt.Printf("%v => %v\n", v, as.Bool(v))
	}

}
Output:

as.Bool()
foobar => false
true => true
1 => true
true => true

func Bytes

func Bytes(valuea ...interface{}) []byte

Bytes returns a slice of bytes.

Example
package main

import (
	"fmt"

	"simonwaldherr.de/go/golibs/as"
)

func main() {
	values := []interface{}{"foobar", "true", 1, true}

	fmt.Println("as.Bytes()")
	for _, v := range values {
		fmt.Printf("%v => %v\n", v, as.Bytes(v))
	}

}
Output:

as.Bytes()
foobar => [102 111 111 98 97 114]
true => [116 114 117 101]
1 => [49]
true => [116 114 117 101]

func DBType

func DBType(str string) string

DBType returns a Database Type of a string.

func DBTypeMultiple

func DBTypeMultiple(val []string) string

DBTypeMultiple returns the lowest common denominator of a type for all inserted DBTypes

func Duration

func Duration(valuea ...interface{}) time.Duration

Duration converts input values to time.Duration. It mainly depends on time.ParseDuration.

func FixedLengthAfter

func FixedLengthAfter(str string, spacer string, length int) string

FixedLengthAfter appends spacer chars after a string

func FixedLengthBefore

func FixedLengthBefore(str string, spacer string, length int) string

FixedLengthBefore prepends spacer chars before a string

func FixedLengthCenter

func FixedLengthCenter(str string, spacer string, length int) string

FixedLengthCenter adds spacer chars after and before a string

func Float

func Float(valuea ...interface{}) float64

Float converts it's input to type float64. int, uint and float gets converted as expected, time is transformed to a float of the corresponding timestamp. strings and byte slices gets converted via strconv.ParseFloat.

func FloatFromXString

func FloatFromXString(valuea ...string) float64

FloatFromXString converts strings to float64. Most values can be converted to float via Float(), but floats as strings in e.g. german spelling should be converted with this function.

func Int

func Int(valuea ...interface{}) int64

Int returns an int64 of the input value. Float values and float values in strings will be rounded via "round half towards positive infinity". strings get converted via strconv.ParseFloat.

func String

func String(valuea ...interface{}) string

String converts input values to string. Time and Duration gets converted via standard functions. Most types gets "converted" via fmt.Sprintf.

Example
package main

import (
	"fmt"
	"time"

	"simonwaldherr.de/go/golibs/as"
)

func main() {
	values := []interface{}{[]byte("foobar"), 1, true, 5 * time.Second, 0xfefe}

	fmt.Println("as.String()")
	for _, v := range values {
		fmt.Printf("%v => %v\n", v, as.String(v))
	}

}
Output:

as.String()
[102 111 111 98 97 114] => foobar
1 => 1
true => true
5s => 5s
65278 => 65278

func Time

func Time(valuea ...interface{}) time.Time

Time converts inputs values to time.Time. Time formats in the variable timeformats can be used.

Example
package main

import (
	"fmt"

	"simonwaldherr.de/go/golibs/as"
)

func main() {
	values := []interface{}{"01.01.1970", "01.01.1999", "2000/10/30"}

	fmt.Println("as.Time()")
	for _, v := range values {
		fmt.Printf("%v => %v\n", v, as.Time(v))
	}

}
Output:

as.Time()
01.01.1970 => 1970-01-01 00:00:00 +0000 UTC
01.01.1999 => 1999-01-01 00:00:00 +0000 UTC
2000/10/30 => 2000-10-30 00:00:00 +0000 UTC

func Trimmed

func Trimmed(valuea ...interface{}) string

Trimmed takes the first given value, converts it to a string, trims the whitespace an returns it.

func Type

func Type(valuea ...interface{}) (string, error)

Type returns a type (string) of a string.

Example
package main

import (
	"fmt"

	"simonwaldherr.de/go/golibs/as"
)

func main() {
	values := []string{
		"false",
		"0",
		"3",
		"5€",
		"23$",
		"42¢",
		"1.337£",
		"020161586X",
		"13,37",
		"https://simonwaldherr.de/",
		"contact@simonwaldherr.de",
		"127.0.0.1",
		"rgb(255, 231, 200)",
		"#ffffff",
		"#03a",
		"06.06.1989",
		"2010/07/29",
		"2006-01-02",
		"¢«∑€®†Ω¨⁄ø𕱑æœ@∆ºª©ƒ∂‚å≤¥≈ç√∫~µ∞…–¡“≠¿'¬”fi",
	}

	fmt.Println("as.Type()")
	for _, v := range values {
		str, _ := as.Type(v)
		fmt.Printf("%v => %v\n", v, str)
	}

}
Output:

as.Type()
false => bool
0 => bool
3 => int
5€ => price
23$ => price
42¢ => price
1.337£ => price
020161586X => isbn
13,37 => float
https://simonwaldherr.de/ => url
contact@simonwaldherr.de => email
127.0.0.1 => ipv4
rgb(255, 231, 200) => color
#ffffff => color
#03a => color
06.06.1989 => date
2010/07/29 => date
2006-01-02 => date
¢«∑€®†Ω¨⁄ø𕱑æœ@∆ºª©ƒ∂‚å≤¥≈ç√∫~µ∞…–¡“≠¿'¬”fi =>
Example (Json)
package main

import (
	"encoding/json"
	"fmt"

	"simonwaldherr.de/go/golibs/as"
)

type dynStruct struct {
	First  as.Dynamic `json:"first"`
	Second as.Dynamic `json:"second"`
	Third  as.Dynamic `json:"third"`
}

func main() {
	jsonStr := `
{
	"first": "2019-01-04 00:00:00",
	"second": "foobar@example.tld",
	"third": "15€"
}`

	x := dynStruct{}

	if err := json.Unmarshal([]byte(jsonStr), &x); err != nil {
		fmt.Println(err)
	}

	fmt.Printf("%v: %v\n", x.First.Type, x.First.Data)
	fmt.Printf("%v: %v\n", x.Second.Type, x.Second.Data)
	fmt.Printf("%v: %v\n", x.Third.Type, x.Third.Data)

}
Output:

date: 2019-01-04 00:00:00 +0000 UTC
email: foobar@example.tld
price: 15€

func Uint

func Uint(valuea ...interface{}) uint64

Uint returns an uint64 of the input value. Float values and float values in strings will be rounded via "round half towards positive infinity". strings get converted via strconv.ParseFloat.

Types

type Dynamic added in v0.10.2

type Dynamic struct {
	Type string
	Data interface{}
}

func (*Dynamic) UnmarshalJSON added in v0.10.2

func (d *Dynamic) UnmarshalJSON(buf []byte) error

UnmarshalJSON implements json.Unmarshaler inferface for the Dynamic Type.

type Int64 added in v0.10.2

type Int64 int64

func (Int64) MarshalJSON added in v0.10.2

func (i Int64) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface for the Int64 Type.

func (*Int64) UnmarshalJSON added in v0.10.2

func (i *Int64) UnmarshalJSON(buf []byte) error

UnmarshalJSON implements json.Unmarshaler inferface for the Int64 Type.

Jump to

Keyboard shortcuts

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