gastownhall/beads · error
encoding TOML: %w
Error message
encoding TOML: %w
What it means
formulaToTOML encodes the parsed raw structure to TOML using a toml.Encoder with empty indentation. If the value cannot be represented in TOML (e.g. unsupported types after JSON decoding, keys that are invalid, or nil-typed values the encoder rejects), the error is wrapped as 'encoding TOML: %w'. This happens after JSON parsing succeeds, during the actual TOML serialization step.
Source
Thrown at cmd/bd/formula.go:692
if err != nil {
return nil, fmt.Errorf("reading source: %w", err)
}
// Parse into a map to preserve structure
var raw map[string]interface{}
if err := json.Unmarshal(jsonData, &raw); err != nil {
return nil, fmt.Errorf("parsing JSON: %w", err)
}
// Fix float64 to int for known integer fields
fixIntegerFields(raw)
// Encode to TOML
var buf bytes.Buffer
encoder := toml.NewEncoder(&buf)
encoder.Indent = ""
if err := encoder.Encode(raw); err != nil {
return nil, fmt.Errorf("encoding TOML: %w", err)
}
// Post-process to convert escaped \n in strings to multi-line strings
result := convertToMultiLineStrings(buf.String())
return []byte(result), nil
}
// convertToMultiLineStrings post-processes TOML to use multi-line strings
// where strings contain newlines. This improves readability for descriptions.
func convertToMultiLineStrings(input string) string {
// Regular expression to match key = "value with \n"
// We look for description fields specifically as those benefit most
lines := strings.Split(input, "\n")
var result []string
for _, line := range lines {
// Check if this line has a string with escaped newlinesView on GitHub (pinned to 71377f2769)
Solutions
- Normalize the formula JSON: make arrays homogeneous (all strings or all objects) and remove nulls from arrays.
- Fix integer/float fields so fixIntegerFields and the encoder see consistent types.
- Update or regenerate the formula to conform to the current schema, then retry conversion.
- If the TOML library reports a specific key/value, edit just that field in the source JSON.
Example fix
// before
{ "tags": ["a", 1, null] } // mixed-type array, not TOML-encodable
// after
{ "tags": ["a", "1"] } Defensive patterns
Strategy: validation
Validate before calling
// ensure arrays are homogeneous before conversion
for _, v := range raw["tags"].([]interface{}) {
if _, ok := v.(string); !ok { return errors.New("tags must be all strings") }
} Try / catch
if _, err := formulaToTOML(f); err != nil {
if strings.Contains(err.Error(), "encoding TOML") {
// inspect wrapped toml error; normalize mixed-type arrays/nulls in source JSON
}
} Prevention
- Keep JSON arrays homogeneous (TOML requires uniform array types).
- Avoid null inside arrays; use empty strings or omit the field.
- Regenerate formulas with the current bd writer instead of hand-authoring exotic structures.
- Test conversion on new formula schemas before committing them.
When it happens
Trigger: Converting a formula whose raw JSON contains values that cannot map to TOML, such as mixed-type arrays ([1, "a"]), nulls in arrays, deeply unusual structures, or map keys the TOML encoder cannot serialize.
Common situations: Formulas with heterogeneous arrays (JSON allows, TOML does not); null values inside arrays; hand-authored JSON exploiting JSON features with no TOML equivalent; older formula schemas with structures the converter's fixIntegerFields doesn't normalize.
Related errors
AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30).
Data as JSON: /api/errors/cbcae76a51eeaea1.
Report an issue: GitHub.