在循环中的结构体映射

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

Mapping between structs in a loop

问题

我有一个函数,用于在两个结构体之间进行一些映射:

Struct1 => Struct2

其中 Struct1 如下所示:

type Struct1 struct {
A Transaction `json:"transaction"`
B string      `json:"name"`
...

}

Struct2 如下所示:

type Struct2 struct {
C AnotherTransaction `json:"transaction"`
D string      `json:"name"`
...

}

我有一个函数,将 "内部" 类型 Transaction => AnotherTransaction 进行映射,但我遇到的问题是,有一个外部结构体,为了方便起见命名为 Struct3,如下所示:

type Struct3 struct {
	Failed   []Struct2 `json:"failed"` // 一系列交易
	Success  []Struct2 `json:"success"`
}

func mapTo(st3 Struct3) Struct1 {
  st1 := Transaction{}
  // A => C 的映射非常冗长
  st1.someField = st3.struct2.anotherField

  return st1 // 现在已映射

}

我的问题是,我需要从 Struct3 中访问 Struct2 的每个元素,并启动上面的映射函数,但我不确定如何操作。我该如何遍历 []Struct2 的每个元素,将每个元素追加并 返回 已填充了 mapTo() 映射的 Struct3

英文:

I have a function that does some mapping between 2 structs:

Struct1 => Struct2

where Struct1 is as follows:

type Struct1 struct {
A Transaction `json:"transaction"`
B string      `json:"name"`
...

}

whereas Struct2 looks like this:

type Struct2 struct {
C AnotherTransaction `json:"transaction"`
D string      `json:"name"`
...

}

I have a function that maps the "inner" type Transaction => AnotherTransaction, but the issue I have is that there is an outer Struct, named Struct3 for convenience, that is as follows:

type Struct3 struct {
	Failed   []Struct2 `json:"failed"` // a list of transactions
	Success  []Struct2 `json:"success"`
}

func mapTo(st3 Struct3) Struct1 {
  st1 := Transaction{}
  // the mapping between A => C is quite lengthy
  st1.someField = st3.struct2.anotherField

  return st1 // now mapped

}

My issue is that from Struct3 I need to access each element of Struct2 and fire up the mapping function above, but I am not sure how to go about it. How can I loop through each element of []Struct2 append each element and return Struct3 now populated with the mapping from mapTo()?

答案1

得分: 2

你可以将map函数的参数更新为struct2,并在main函数中循环遍历struct3的数组字段,并将每个字段发送到toMap函数。

func main() {
	type Struct3 struct {
		Failed   []Struct2 `json:"failed"` // 交易列表
		Success  []Struct2 `json:"success"`
	}
	s3 := &Struct3{
		Failed: make([]Struct2, 0),
		Success: make([]Struct2, 0),
	}

	for i := range s3.Success {
		// 对结果值进行处理
		_ = toMap(s3.Success[i])
	}

	for i := range s3.Failed {
		// 对结果值进行处理
		_ = toMap(s3.Failed[i])
	}
}

func mapTo(st2 Struct2) Struct1 {
  st1 := Transaction{}
  // A到C的映射非常冗长
  st1.someField = st2.anotherField

  return st1 // 现在已映射完成

}
英文:

You can update the map function's argument to struct2 and loop through the struct3's fields of array from main function and send each of them to the toMap function.

func main() {
	type Struct3 struct {
		Failed   []Struct2 `json:"failed"` // a list of transactions
		Success  []Struct2 `json:"success"`
	}
	s3 := &Struct3{
		Failed: make([]Struct2, 0),
		Success: make([]Struct2, 0),
	}

	for i := range s3.Success {
		// do something with the result value
		_ = toMap(s3.Success[i])
	}

	for i := range s3.Failed {
		// do something with the result value
		_ = toMap(s3.Failed[i])
	}
}

func mapTo(st2 Struct2) Struct1 {
  st1 := Transaction{}
  // the mapping between A => C is quite lengthy
  st1.someField = st2.anotherField

  return st1 // now mapped

}

huangapple
  • 本文由 发表于 2021年10月7日 00:10:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/69469134.html
匿名

发表评论

匿名网友

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

确定