Can 2 fields in struct have same struct tags in Go

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

Can 2 fields in struct have same struct tags in Go

问题

我有一个用于将 SQL 数据绑定到结构体变量的 Struct 类型。
它有两个字段,分别是 CenterCode 和 ActualCenterCode。

type LearnerModel struct {
    CenterCode       dbr.NullString `json:"centerCode" db:"LEARNINGCENTERCODE"`
    ActualCenterCode dbr.NullString `json:"actualCenterCode" db:"LEARNINGCENTERCODE"`
    SomeOtherField   dbr.NullString `json:"someOtherField" db:"SOMEOTHERFIELD"`
}

如果 SomeOtherField 为 Null,那么 ActualCenterCode 将与 CenterCode 相同。
否则,ActualCenterCode 将被设置为 SomeOtherField。

我的问题是,当我使用以下代码绑定数据时:

"github.com/gocraft/dbr"

session.Select(selectQuery).From(tableName).Where("LearnerID IN ?", learnerIDs).Load(&learnerModelObj)

我从数据库中获取到了 CenterCode 的数据,但 ActualCenterCode 被设置为 null。我需要通过 API 将这些数据发送到另一个服务器,在那里我可以处理这个条件,但是否有办法通过结构体来处理这个问题呢?

英文:

I have Struct Type for binding SQL data to my struct variable.
It has 2 Feilds as CenterCode and ActualCenterCode

type LearnerModel struct {
	CenterCode dbr.NullString  `json:"centerCode" db:"LEARNINGCENTERCODE"`
	ActualCenterCode dbr.NullString  `json:"actualCenterCode" db:"LEARNINGCENTERCODE"`
	SomeOtherFeild dbr.NullString  `json:"someOtherFeild" db:"SOMEOTHERFEILD"`
}
  • The ActualCenterCode would be the same as CenterCode if SomeOtherFeild is Null.
  • Else ActualCenterCode will be set to SomeOtherFeild.

My question is when I bind this data using

"github.com/gocraft/dbr"

session.Select(selectQuery).From(tableName).Where("LearnerID IN ?", learnerIDs).Load(&learnerModelObj)

I get CenterCode as the data in Database but the ActualCenterCode is set to null. I have to send this data to another server via API where I can handle this condition, but is there a way to handle this via struct

答案1

得分: 1

是的,多个字段可以具有相同的结构标签,但是如何解释这些标签取决于使用这些标签的库。

在这种情况下,似乎您使用的库会忽略重复的映射。我认为这是有道理的,否则在保存过程中,如果CenterCodeActualCenterCode包含不同的值,就会产生歧义,不知道保存哪个数据。

如果我正确理解您的意图,我更倾向于添加包含您描述逻辑的访问方法。

类似于

type LearnerModel struct {
    CenterCode dbr.NullString  `json:"centerCode" db:"LEARNINGCENTERCODE"`
    SomeOtherFeild dbr.NullString  `json:"someOtherFeild" db:"SOMEOTHERFEILD"`
}

func (m *LearnerModel) ActualCenterCode() dbr.NullString {
  if m.SomeOtherFeild != nil {
    return m.SomeOtherFeild
  }
  return m.CenterCode
}

这样就没有歧义了。

英文:

Yes, multiple fields can have same struct tags, how they will be interpreted though is up to the libraries using those tags.

In this case it seems that library you use, ignores duplicate mappings. In my opinion this makes sense as otherwise there would ambiguity which data to save if it would happen that CenterCode and ActualCenterCode contain different values during save.

If I understand correctly your intention, I would rather add accessor method containing logic you described.

Something like

type LearnerModel struct {
    CenterCode dbr.NullString  `json:"centerCode" db:"LEARNINGCENTERCODE"`
    SomeOtherFeild dbr.NullString  `json:"someOtherFeild" db:"SOMEOTHERFEILD"`
}

func (m *LearnerModel) ActualCenterCode() dbr.NullString {
  if m.SomeOtherFeild != nil {
    return m.SomeOtherFeild
  }
  return m.CenterCode
}

This way there is no ambiguity.

huangapple
  • 本文由 发表于 2023年4月25日 13:20:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76097828.html
匿名

发表评论

匿名网友

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

确定