-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve converter feature for package gconv (#3211)
- Loading branch information
Showing
49 changed files
with
625 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, | ||
// You can obtain one at https://github.com/gogf/gf. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gogf/gf/v2/os/gtime" | ||
"github.com/gogf/gf/v2/util/gconv" | ||
) | ||
|
||
type MyTime = *gtime.Time | ||
|
||
type Src struct { | ||
A MyTime | ||
} | ||
|
||
type Dst struct { | ||
B string | ||
} | ||
|
||
type SrcWrap struct { | ||
Value Src | ||
} | ||
|
||
type DstWrap struct { | ||
Value Dst | ||
} | ||
|
||
// SrcToDstConverter is custom converting function for custom type. | ||
func SrcToDstConverter(src Src) (dst *Dst, err error) { | ||
return &Dst{B: src.A.Format("Y-m-d")}, nil | ||
} | ||
|
||
// SrcToDstConverter is custom converting function for custom type. | ||
func main() { | ||
// register custom converter function. | ||
err := gconv.RegisterConverter(SrcToDstConverter) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// custom struct converting. | ||
var src = Src{A: gtime.Now()} | ||
dst := gconv.ConvertWithRefer(src, &Dst{}) | ||
fmt.Println("src:", src) | ||
fmt.Println("dst:", dst) | ||
|
||
// custom struct attributes converting. | ||
var srcWrap = SrcWrap{Src{A: gtime.Now()}} | ||
dstWrap := gconv.ConvertWithRefer(srcWrap, &DstWrap{}) | ||
fmt.Println("srcWrap:", srcWrap) | ||
fmt.Println("dstWrap:", dstWrap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, | ||
// You can obtain one at https://github.com/gogf/gf. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gogf/gf/v2/os/gtime" | ||
"github.com/gogf/gf/v2/util/gconv" | ||
) | ||
|
||
type MyTime = *gtime.Time | ||
|
||
type Src struct { | ||
A MyTime | ||
} | ||
|
||
type Dst struct { | ||
B string | ||
} | ||
|
||
type SrcWrap struct { | ||
Value Src | ||
} | ||
|
||
type DstWrap struct { | ||
Value Dst | ||
} | ||
|
||
// SrcToDstConverter is custom converting function for custom type. | ||
func SrcToDstConverter(src Src) (dst *Dst, err error) { | ||
return &Dst{B: src.A.Format("Y-m-d")}, nil | ||
} | ||
|
||
// SrcToDstConverter is custom converting function for custom type. | ||
func main() { | ||
// register custom converter function. | ||
err := gconv.RegisterConverter(SrcToDstConverter) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// custom struct converting. | ||
var ( | ||
src = Src{A: gtime.Now()} | ||
dst *Dst | ||
) | ||
err = gconv.Scan(src, &dst) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("src:", src) | ||
fmt.Println("dst:", dst) | ||
|
||
// custom struct attributes converting. | ||
var ( | ||
srcWrap = SrcWrap{Src{A: gtime.Now()}} | ||
dstWrap *DstWrap | ||
) | ||
err = gconv.Scan(srcWrap, &dstWrap) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("srcWrap:", srcWrap) | ||
fmt.Println("dstWrap:", dstWrap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, | ||
// You can obtain one at https://github.com/gogf/gf. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gogf/gf/v2/util/gconv" | ||
) | ||
|
||
type Src struct { | ||
A int | ||
} | ||
|
||
type Dst struct { | ||
B int | ||
} | ||
|
||
type SrcWrap struct { | ||
Value Src | ||
} | ||
|
||
type DstWrap struct { | ||
Value Dst | ||
} | ||
|
||
// SrcToDstConverter is custom converting function for custom type. | ||
func SrcToDstConverter(src Src) (dst *Dst, err error) { | ||
return &Dst{B: src.A}, nil | ||
} | ||
|
||
func main() { | ||
// register custom converter function. | ||
err := gconv.RegisterConverter(SrcToDstConverter) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// custom struct converting. | ||
var src = Src{A: 1} | ||
dst := gconv.ConvertWithRefer(src, Dst{}) | ||
fmt.Println("src:", src) | ||
fmt.Println("dst:", dst) | ||
|
||
// custom struct attributes converting. | ||
var srcWrap = SrcWrap{Src{A: 1}} | ||
dstWrap := gconv.ConvertWithRefer(srcWrap, &DstWrap{}) | ||
fmt.Println("srcWrap:", srcWrap) | ||
fmt.Println("dstWrap:", dstWrap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, | ||
// You can obtain one at https://github.com/gogf/gf. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gogf/gf/v2/util/gconv" | ||
) | ||
|
||
type Src struct { | ||
A int | ||
} | ||
|
||
type Dst struct { | ||
B int | ||
} | ||
|
||
type SrcWrap struct { | ||
Value Src | ||
} | ||
|
||
type DstWrap struct { | ||
Value Dst | ||
} | ||
|
||
func SrcToDstConverter(src Src) (dst *Dst, err error) { | ||
return &Dst{B: src.A}, nil | ||
} | ||
|
||
// SrcToDstConverter is custom converting function for custom type. | ||
func main() { | ||
// register custom converter function. | ||
err := gconv.RegisterConverter(SrcToDstConverter) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// custom struct converting. | ||
var ( | ||
src = Src{A: 1} | ||
dst *Dst | ||
) | ||
err = gconv.Scan(src, &dst) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("src:", src) | ||
fmt.Println("dst:", dst) | ||
|
||
// custom struct attributes converting. | ||
var ( | ||
srcWrap = SrcWrap{Src{A: 1}} | ||
dstWrap *DstWrap | ||
) | ||
err = gconv.Scan(srcWrap, &dstWrap) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("srcWrap:", srcWrap) | ||
fmt.Println("dstWrap:", dstWrap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.