Skip to content

Commit

Permalink
improve converter feature for package gconv (#3211)
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn authored Jan 22, 2024
1 parent 383937f commit ba39323
Show file tree
Hide file tree
Showing 49 changed files with 625 additions and 30 deletions.
6 changes: 6 additions & 0 deletions example/config/apollo/boot/boot.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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 boot

import (
Expand Down
6 changes: 6 additions & 0 deletions example/config/apollo/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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 (
Expand Down
11 changes: 9 additions & 2 deletions example/config/consul/boot/boot.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
// 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 boot

import (
consul "github.com/gogf/gf/contrib/config/consul/v2"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/go-cleanhttp"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)

func init() {
Expand Down
6 changes: 6 additions & 0 deletions example/config/consul/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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 (
Expand Down
6 changes: 6 additions & 0 deletions example/config/kubecm/boot_in_pod/boot.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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 boot

import (
Expand Down
9 changes: 8 additions & 1 deletion example/config/kubecm/boot_out_pod/boot.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// 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 boot

import (
"k8s.io/client-go/kubernetes"

"github.com/gogf/gf/contrib/config/kubecm/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"k8s.io/client-go/kubernetes"
)

const (
Expand Down
6 changes: 6 additions & 0 deletions example/config/kubecm/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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 (
Expand Down
11 changes: 9 additions & 2 deletions example/config/nacos/boot/boot.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// 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 boot

import (
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"

"github.com/gogf/gf/contrib/config/nacos/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
)

func init() {
Expand Down
6 changes: 6 additions & 0 deletions example/config/nacos/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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 nacos

import (
Expand Down
58 changes: 58 additions & 0 deletions example/converter/alias_type_convert_with_refer/main.go
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)
}
72 changes: 72 additions & 0 deletions example/converter/alias_type_scan/main.go
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)
}
54 changes: 54 additions & 0 deletions example/converter/struct_convert_with_refer/main.go
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)
}
68 changes: 68 additions & 0 deletions example/converter/struct_scan/main.go
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)
}
6 changes: 6 additions & 0 deletions example/httpserver/default_value/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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 (
Expand Down
6 changes: 6 additions & 0 deletions example/httpserver/proxy/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// 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 (
Expand Down
Loading

0 comments on commit ba39323

Please sign in to comment.