Documentation
¶
Index ¶
- func DuckParse[Raw encoded](raw Raw, opts ...Option) (any, error)
- func Format[T any](v T, opts ...Option) (string, error)
- func FormatReflect(v reflect.Value, opts ...Option) (string, error)
- func IsRegistered[T any](i ...T) bool
- func Marshal[T any](v T, opts ...Option) ([]byte, error)
- func MarshalReflect(val reflect.Value, opts ...Option) ([]byte, error)
- func Parse[T any, Raw encoded](raw Raw, opts ...Option) (T, error)
- func ParseReflect[Raw encoded](typ reflect.Type, raw Raw, opts ...Option) (reflect.Value, error)
- func Register[T any](c TextCodec[T]) func()
- func Unmarshal[T any](data []byte, p *T, opts ...Option) error
- func UnmarshalReflect(typ reflect.Type, data []byte, ptr reflect.Value, opts ...Option) error
- type MarshalFunc
- type Option
- type Options
- type TextCodec
- type UnmarshalFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Format ¶
Format allows you to format a value into a string format
Example ¶
package main
import (
"go.llib.dev/frameless/pkg/convkit"
)
func main() {
formatted, err := convkit.Format(42.24)
_, _ = formatted, err // "42.24", nil
}
Output:
func FormatReflect ¶ added in v0.310.0
FormatReflect allows you to Format a value into a string, passed as a reflect.Value.
func IsRegistered ¶
func MarshalReflect ¶ added in v0.310.0
func Parse ¶
Example ¶
package main
import (
"go.llib.dev/frameless/pkg/convkit"
)
func main() {
_, _ = convkit.Parse[string]("hello")
_, _ = convkit.Parse[int]("42")
_, _ = convkit.Parse[float64]("42.24")
_, _ = convkit.Parse[[]string]("a,b,c", convkit.Options{Separator: ","})
_, _ = convkit.Parse[[]string]([]byte(`["a","b","c"]`), convkit.Options{Separator: ","})
}
Output:
func ParseReflect ¶
func Unmarshal ¶ added in v0.310.0
Example (BasicType) ¶
package main
import (
"fmt"
"go.llib.dev/frameless/pkg/convkit"
)
func main() {
var num int
convkit.Unmarshal([]byte("42"), &num)
fmt.Println(num)
}
Output: 42
Example (CustomType) ¶
package main
import (
"fmt"
"strconv"
"strings"
"go.llib.dev/frameless/pkg/convkit"
)
func main() {
type Config struct {
Host string
Port int
}
var cfg Config
convkit.Unmarshal([]byte("host=localhost,port=8080"), &cfg,
convkit.UnmarshalWith(func(data []byte, c *Config) error {
parts := strings.SplitN(string(data), ",", 2)
if len(parts) != 2 {
return fmt.Errorf("invalid format")
}
for _, part := range parts {
kv := strings.SplitN(part, "=", 2)
if len(kv) != 2 {
continue
}
switch kv[0] {
case "host":
c.Host = kv[1]
case "port":
var err error
c.Port, err = strconv.Atoi(kv[1])
if err != nil {
return fmt.Errorf("invalid port: %w", err)
}
}
}
return nil
}))
fmt.Printf("%s:%d\n", cfg.Host, cfg.Port)
}
Output: localhost:8080
Example (Json) ¶
package main
import (
"fmt"
"go.llib.dev/frameless/pkg/convkit"
)
func main() {
var data map[string]interface{}
convkit.Unmarshal([]byte(`{"key":"value"}`), &data)
fmt.Println(data["key"])
}
Output: value
Example (SliceWithSeparator) ¶
package main
import (
"fmt"
"go.llib.dev/frameless/pkg/convkit"
)
func main() {
var nums []int
convkit.Unmarshal([]byte("1;2;3"), &nums, convkit.Options{Separator: ";"})
fmt.Println(nums)
}
Output: [1 2 3]
Example (TimeWithLayout) ¶
package main
import (
"fmt"
"time"
"go.llib.dev/frameless/pkg/convkit"
)
func main() {
var t time.Time
layout := "2006-01-02"
convkit.Unmarshal([]byte("2023-05-15"), &t, convkit.Options{TimeLayout: layout})
fmt.Println(t.Format(layout))
}
Output: 2023-05-15
Example (Url) ¶
package main
import (
"fmt"
"net/url"
"go.llib.dev/frameless/pkg/convkit"
)
func main() {
var url url.URL
convkit.Unmarshal([]byte("https://example.com/path?query=value"), &url)
fmt.Println(url.String())
}
Output: https://example.com/path?query=value
func UnmarshalReflect ¶ added in v0.310.0
Example ¶
package main
import (
"fmt"
"reflect"
"go.llib.dev/frameless/pkg/convkit"
"go.llib.dev/frameless/pkg/reflectkit"
)
func main() {
typ := reflectkit.TypeOf[int]()
ptr := reflect.New(typ)
convkit.UnmarshalReflect(typ, []byte("42"), ptr)
fmt.Println(ptr.Elem().Interface())
}
Output: 42
Example (SliceWithSeparator) ¶
package main
import (
"fmt"
"reflect"
"go.llib.dev/frameless/pkg/convkit"
"go.llib.dev/frameless/pkg/reflectkit"
)
func main() {
typ := reflectkit.TypeOf[[]int]()
ptr := reflect.New(typ)
convkit.UnmarshalReflect(typ, []byte("1;2;3"), ptr, convkit.Options{Separator: ";"})
fmt.Println(ptr.Elem().Interface())
}
Output: [1 2 3]
Example (TimeWithLayout) ¶
package main
import (
"fmt"
"reflect"
"time"
"go.llib.dev/frameless/pkg/convkit"
"go.llib.dev/frameless/pkg/reflectkit"
)
func main() {
typ := reflectkit.TypeOf[time.Time]()
ptr := reflect.New(typ)
layout := "2006-01-02"
convkit.UnmarshalReflect(typ, []byte("2023-05-15"), ptr, convkit.Options{TimeLayout: layout})
fmt.Println(ptr.Elem().Interface().(time.Time).Format(layout))
}
Output: 2023-05-15
Types ¶
type MarshalFunc ¶ added in v0.310.0
type Option ¶
func UnmarshalWith ¶ added in v0.310.0
func UnmarshalWith[T any](parser UnmarshalFunc[T]) Option
type Options ¶
type Options struct {
// Separator is the separator character which will be used to detect list elements.
Separator string
// TimeLayout is the time layout format which will be used to parse time values.
TimeLayout string
// ParseFunc is used to Parse the input data. It follows the signature of the json.Unmarshal function.
ParseFunc func(data []byte, ptr any) error
}
type UnmarshalFunc ¶ added in v0.310.0
Click to show internal directories.
Click to hide internal directories.