Git Product home page Git Product logo

go-conv's People

Contributors

cmstar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

go-conv's Issues

Make the time-zone variable when converting to time.Time

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.

Support embedded structs

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.

Support convert to interface{} (any)

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

The convert panics when converting to a type with a basetype

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.

Support converting a map other than `map[string]interface{}` to a struct

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.