辅助函数,将相应的数据分配给其键。

huangapple go评论95阅读模式
英文:

Helper func to assign respective data to its key

问题

所以我有这个数据结构:

type Parent struct {
    A ChildA
    B ChildB
    C ChildC
    D ChildD
}

type ChildA struct {
    ...

}

我正在尝试创建一个辅助函数,以便在变量赋值时减少代码行数。

我想要做的是:

func SomeHelper( SomeChild Child? ) Parent {
    return Parent{
        ?: SomeChild
    }
}

“?”可以是 A、B、C、D 中的任意一个键。

英文:

So I have this data struct:

type Parent struct {
    A ChildA
    B ChildB
    C ChildC
    D ChildD
}

type ChildA struct {
    ...

}

I am trying to create a helper funct such that I can reduce my LOC when it comes to variable assignment.

What I am trying to do:

func SomeHelper( SomeChild Child? ) Parent {
    return Parent{
        ?: SomeChild
    }
}

"?" can be any of the key A B C D

答案1

得分: 1

我们可以使用可变参数函数和反射。

这是示例代码

package main

import (
	"errors"
	"fmt"
	"reflect"
)

type Parent struct {
	A ChildA
	B ChildB
	C ChildC
	D ChildD
}

type ChildA struct {
	x string
}

type ChildB struct {
	x string
}

type ChildC struct {
}

type ChildD struct {
}

func helper(childs ...interface{}) (Parent, error) {
	check := make(map[string]int)
	var p Parent

	for _, v := range childs {
		if v == nil {
			continue
		}
		childType := reflect.TypeOf(v)

		check[childType.String()]++

		if check[childType.String()] > 1 {
			return p, errors.New("child must be unique")
		}

		switch childType.String() {
		case "main.ChildA":
			p.A = v.(ChildA)
		case "main.ChildB":
			p.B = v.(ChildB)
		case "main.ChildC":
			p.C = v.(ChildC)
		case "main.ChildD":
			p.D = v.(ChildD)
		}
	}

	return p, nil
}

func main() {
	p, err := helper(ChildA{"hello"}, ChildB{"world"}, ChildC{})
	if err != nil {
		panic(err)
	}

	fmt.Println(p)
}
英文:

We can use variadic function and reflection.

this is the example code:

package main

import (
	"errors"
	"fmt"
	"reflect"
)

type Parent struct {
	A ChildA
	B ChildB
	C ChildC
	D ChildD
}

type ChildA struct {
	x string
}

type ChildB struct {
	x string
}

type ChildC struct {
}

type ChildD struct {
}

func helper(childs ...any) (Parent, error) {
	check := make(map[string]int)
	var p Parent

	for _, v := range childs {
		if v == nil {
			continue
		}
		childType := reflect.TypeOf(v)

		check[childType.String()]++

		if check[childType.String()] > 1 {
			return p, errors.New("child must be unique")
		}

		switch childType.String() {
		case "main.ChildA":
			p.A = v.(ChildA)
		case "main.ChildB":
			p.B = v.(ChildB)
		case "main.ChildC":
			p.C = v.(ChildC)
		case "main.ChildD":
			p.D = v.(ChildD)
		}
	}

	return p, nil
}

func main() {
	p, err := helper(ChildA{"hello"}, ChildB{"world"}, ChildC{})
	if err != nil {
		panic(err)
	}

	fmt.Println(p)
}

答案2

得分: 0

你可以使用访问者模式。

type Parent struct {
	A ChildA
	B ChildB
	C ChildC
}

type Child interface {
	VisitParent(*Parent)
}

type ChildA struct{}

func (c ChildA) VisitParent(parent *Parent) {
	parent.A = c
}

type ChildB struct{}

func (c ChildB) VisitParent(parent *Parent) {
	parent.B = c
}

type ChildC struct{}

func (c ChildC) VisitParent(parent *Parent) {
	parent.C = c
}

func SomeHelper(someChild Child) Parent {
	parent := Parent{}
	someChild.VisitParent(&parent)

	return parent
}

请注意,我已经将您提供的代码翻译成了中文。

英文:

You can use Visitor pattern.

type Parent struct {
A ChildA
B ChildB
C ChildC
}
type Child interface {
VisitParent(*Parent)
}
type ChildA struct{}
func (c ChildA) VisitParent(parent *Parent) {
parent.A = c
}
type ChildB struct{}
func (c ChildB) VisitParent(parent *Parent) {
parent.B = c
}
type ChildC struct{}
func (c ChildC) VisitParent(parent *Parent) {
parent.C = c
}
func SomeHelper(someChild Child) Parent {
parent := Parent{}
someChild.VisitParent(&parent)
return parent
}

huangapple
  • 本文由 发表于 2022年8月16日 17:13:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/73371538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定