如何在Golang中创建一个具有相同成员但不同JSON标签的开发结构和生产结构?

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

How to have a Development struct and Production struct in Golang with identical members but different JSON tags?

问题

第一次提问!
我正在尝试将使用相同结构的开发和生产分开。


我正在使用Airtable,它将记录作为带有fld标签的JSON发送,我们在解组时使用该标签。

type AirtableRecord struct {
    Name   *string  `json:"fldAAAA,omitempty"`
}

我有两个独立的Airtable:

  1. 用于开发
  2. 用于生产

它们是相同的,只是由于Airtable的工作方式,字段被赋予不同的fld标签。

我的Airtable字段的图片


现在,为了将开发环境与生产环境分开,我必须根据指向的Airtable取消注释正确的成员。

type AirtableRecord struct {
    // 开发环境
    Name   *string  `json:"fldAAAA,omitempty"`

    // 生产环境
    //Name   *string  `json:"fldBBBB,omitempty"`
}

我将这个类型保存在自己的model.go文件中,其他包会使用它。

我已经研究过:

  • 在一行中有多个JSON标签,Golang不支持这样做
type AirtableRecord struct {
    // 开发环境或生产环境
    Name   *string  `json:"fldAAAA,fldBBBB,omitempty"`
}
  • 使用构建标签分离我的文件,也许这样可以,但我做错了
    文件1:
// +build dev
type AirtableRecord struct {
    // 开发环境
    Name   *string  `json:"fldAAAA,omitempty"`
}

文件2:

type AirtableRecord struct {
    // 生产环境
    Name   *string  `json:"fldBBBB,omitempty"`
}
  • 研究了使用retag,但他们给出的示例不符合我的需求
    • retag链接:https://pkg.go.dev/github.com/sevlyar/retag@v0.0.0-20190429052747-c3f10e304082

我想根据我是在开发模式还是生产模式下运行来动态更改这个成员的标签。

非常感谢任何帮助!

英文:

First time asking a question!
I am trying to separate my development and production that use the same struct.


I am working with Airtable, which sends records as a JSON with an fld tag that we use when unmarshaling.

type AirtableRecord struct {
    Name   *string  `json:"fldAAAA,omitempty"`
}

I have 2 separate Airtables:

  1. For development
  2. For production

They are identical, except because of how Airtable works the fields are given different fld tags

Image of my Airtable Field


Right now to separate development env from production env, I have to uncomment the right members depending which Airtable I am pointing to.

type AirtableRecord struct {
    // Development
    Name   *string  `json:"fldAAAA,omitempty"`

    // Production
    //Name   *string  `json:"fldBBBB,omitempty"`
}

I keep this type in it's own model.go file, that other packages use.

I have looked into:

  • Having multiple JSON tags in a line, Golang doesn't do this
type AirtableRecord struct {
    // Development or Production
    Name   *string  `json:"fldAAAA,fldBBBB,omitempty"`
}
  • Separating my files using build tags, maybe this works but I'm doing it wrong

File 1:

// +build dev
type AirtableRecord struct {
    // Development
    Name   *string  `json:"fldAAAA,omitempty"`
}

File 2:

type AirtableRecord struct {
    // Production
    Name   *string  `json:"fldBBBB,omitempty"`
}

I want to change this Member's tag dynamically based on if I'm running in development mode or production mode.

Any and all help would be appreciated!

答案1

得分: 2

如果在使用构建标签时遇到“在此块中重新声明”编译错误,请在生产文件中指定一个非“dev”的标签,以避免此错误。

开发文件:

// +build dev
type AirtableRecord struct {
    // Development
    Name   *string  `json:"fldAAAA,omitempty"`
}

生产文件:

// +build !dev
type AirtableRecord struct {
    // Development
    Name   *string  `json:"fldAAAA,omitempty"`
}

构建命令:

# 对于开发环境
go build -tags=dev -o devrel
# 对于生产环境
go build -tags=prod -o prodrel  
或者对于生产环境不使用标签

另外,自1.17版本以来,构建标签的格式已更改,所以在您的情况下,应该是:

//go:build dev

但旧的格式也应该可以工作。

英文:

If you are getting redeclared in this block compile error with build tags, then specify a not'ed tag on prod file, to avoid it.

Dev File

// +build dev
type AirtableRecord struct {
    // Development
    Name   *string  `json:"fldAAAA,omitempty"`
}

Prod File

// +build !dev
type AirtableRecord struct {
    // Development
    Name   *string  `json:"fldAAAA,omitempty"`
}

build with

# for dev
go build -tags=dev -o devrel
# For prod
go build -tags=prod -o prodrel  
Or no tags for prod

Also build tag format has changed since 1.17 so in your case it will be,

//go:build dev

but should work with old one too.

huangapple
  • 本文由 发表于 2022年12月2日 07:59:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/74649449.html
匿名

发表评论

匿名网友

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

确定