Skip to content

Generate Elements

An element defines one value derived for every selected struct field.

elements:
- name: "json"
input:
mode: "tag"
tag_priority: ["json"]
output:
mode: "constant"

Uses the first non-empty tag in tag_priority. If no listed tag exists, the element is omitted for that field.

input:
mode: "tag"
tag_priority: ["db", "json"]

For json:"display_name,omitempty", Constago uses display_name; tag options after the first comma are removed.

Always derives the value from the Go field name. tag_priority receives its normal default when omitted, but it is not consulted in this mode.

input:
mode: "field"
tag_priority: ["field"]

Uses the first tag match, then falls back to the field name.

input:
mode: "tagThenField"
tag_priority: ["json"]

Given:

type User struct {
DisplayName string `json:"display_name"`
}
output:
mode: "constant"
format:
prefix: "Json"

Generates:

const JsonUserDisplayName = "display_name"

This mode generates an initialized package variable with a grouped anonymous struct:

output:
mode: "struct"
format:
prefix: "Json"
var JsonUser = struct {
DisplayName string
}{
DisplayName: "display_name",
}

Use it as JsonUser.DisplayName.

No standalone constant or grouped variable is emitted. The value remains available to getters that list the element in returns.

The element’s format controls generated names:

format:
holder: "pascal"
struct: "pascal"
prefix: "Json"
suffix: "Key"
  • holder formats fields inside grouped accessor variables.
  • struct formats top-level constant names and grouped accessor variable names.
  • prefix and suffix wrap the struct and field portions.
  • Formats are camel, pascal, snake, and snakeUpper.

With prefix: Json, suffix: Key, and struct: snakeUpper, the constant for User.DisplayName is JSON_USER_DISPLAY_NAME_KEY.

Transforms affect the generated string value, not its Go identifier:

transform:
tag_values: true
value_case: "upper"
value_separator: "_"
  • tag_values: false leaves tag-derived values unchanged.
  • Field-name fallbacks are always transformed.
  • value_case accepts asIs, camel, pascal, upper, or lower.
  • value_separator joins detected words with the configured string.

For a DisplayName field, lower plus _ produces display_name.