79490120

Date: 2025-03-06 17:01:19
Score: 1.5
Natty:
Report link

Convert the Template field to json.RawMessage in a MarshalJSON method on BrandTemplate.

type BrandTemplate struct {
    Type     string `json:"type"`   // Template type (e.g., email_forgot_password)
    Locale   string `json:"locale"` // Locale (e.g., "es" for Spanish, "en" for English)
    Template string `json:"-"`      // Template content (Email/SMS content)
}

func (t *BrandTemplate) MarshalJSON() ([]byte, error) {
    // Define type to break recursive calls to MarshalJSON.
    // The type X has all of fields for BrandTemplate, but
    // non of the methods.
    type X BrandTemplate

    // Marshal a type that shadows BrandTemplate.Template
    // with a raw JSON field of the same name.
    return json.Marshal(
        struct {
            *X
            Template json.RawMessage `json:"template"`
        }{
            (*X)(t),
            json.RawMessage(t.Template),
        })
}

https://play.golang.com/p/2ZQlziRRDOY

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Al Green