1

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!

LuckyScum
  • 21
  • 4
  • 1
    I would look into how you get your data from airtable and not attempt to parse this broken json. No experience with airtable, but it looks like other people do get meaningful field names in their json, instead of "fldXXX" https://stackoverflow.com/questions/67103153/parsing-all-prices-of-same-field-name-if-found-in-sheet-from-json-data-vba-in-a – Erwin Bolwidt Dec 02 '22 at 00:21
  • I want to use the field id rather than the field name since its probable someone will change the field name to suit their needs. Appreciate the link though @ErwinBolwidt – LuckyScum Dec 02 '22 at 01:53
  • As an alternative to build tags you can use a custom `json.Unmarshaler`: https://go.dev/play/p/TM0nj5no1UJ – mkopriva Dec 02 '22 at 03:59

1 Answers1

2

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.