cmstar / go-conv Goto Github PK
View Code? Open in Web Editor NEWConvert between Go types, including bool, string, numbers, time, map, struct, slice.
License: MIT License
Convert between Go types, including bool, string, numbers, time, map, struct, slice.
License: MIT License
Currently, the time-zone of output time.Time is always time.Local. It's not compatible to the standard lib.
In the standard lib:
// When converting a timestamp (a number) to a time.Time, the standard lib use time.Local as the location.
time.Unix(0) // Returns with time.Local.
// When converting a string to a time.Time, the location is read from the given value.
time.Parse(time.RFC3339, "2022-04-20T11:30:40+12:00") // Returns a Time with offset 12:00.
time.Parse(time.RFC3339, "2022-04-20T11:30:40Z") // Returns a Timewith time.UTC.
Consider:
// Returns a Time with time.Local, behaves like time.Unix().
conv.ConvertType(123, reflect.TypeOf(time.Time{}))
// Returns a Time with offset 12:00, like time.Parse().
conv.ConvertType("2022-04-20T11:30:40+12:00", reflect.TypeOf(time.Time{}))
// Returns a Time with time.UTC, like time.Parse().
conv.ConvertType("2022-04-20T11:30:40+12:00", reflect.TypeOf(time.Time{}))
// When converting a Time to a Time, keeps the location.
conv.ConvertType(time.Now(), reflect.TypeOf(time.Time{})) // Returns Local.
conv.ConvertType(time.Now().UTC(), reflect.TypeOf(time.Time{})) // Returns UTC.
Currently embedded structs are not supported.
type Embedded struct {
B int
}
type Demo struct {
A int
Embedded
}
m := map[string]interface{}{
"A": 1,
"B": 2,
}
var demo Demo
conv.Convert(m, &demo) // -> demo.B == 0 , it should be 2.
We got an error "cannot convert int to interface {}" from the code below:
var aa any
err := conv.Convert(1, &aa)
fmt.Println(err)
Consider support converting a value to any
(the empty interface), just returns the cloned value like:
conv.ConvertType(value, reflect.TypeOf(value))
This code panics:
type V int
var v V
_ = conv.Convert(3, &v)
with the message: "reflect.Set: value of type int is not assignable to type conv_test.V".
And this code returns an int instead of V:
type V int
out, _ := conv.ConvertType(1, reflect.TypeOf(V(0)))
fmt.Println(reflect.ValueOf(out).Type()) // -> int
The basetype is not properly processed.
time.Duration can be converted from/to integers.
The code outputs an error: "conv.Convert: when converting a map to a struct, the map must be map[string]interface{}, got map[string]int".
type S struct {
A, B int
}
var n S
err := conv.Convert(map[string]int{"A": 1, "B": 2}, &n)
fmt.Println(err)
Consider support converting a map other than map[string]interface{}
to a struct.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.