{"record":{"id":"cbcae76a51eeaea1","repo":"gastownhall/beads","slug":"encoding-toml-w","errorCode":null,"errorMessage":"encoding TOML: %w","messagePattern":"encoding TOML: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/formula.go","lineNumber":692,"sourceCode":"\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"reading source: %w\", err)\n\t}\n\n\t// Parse into a map to preserve structure\n\tvar raw map[string]interface{}\n\tif err := json.Unmarshal(jsonData, &raw); err != nil {\n\t\treturn nil, fmt.Errorf(\"parsing JSON: %w\", err)\n\t}\n\n\t// Fix float64 to int for known integer fields\n\tfixIntegerFields(raw)\n\n\t// Encode to TOML\n\tvar buf bytes.Buffer\n\tencoder := toml.NewEncoder(&buf)\n\tencoder.Indent = \"\"\n\tif err := encoder.Encode(raw); err != nil {\n\t\treturn nil, fmt.Errorf(\"encoding TOML: %w\", err)\n\t}\n\n\t// Post-process to convert escaped \\n in strings to multi-line strings\n\tresult := convertToMultiLineStrings(buf.String())\n\n\treturn []byte(result), nil\n}\n\n// convertToMultiLineStrings post-processes TOML to use multi-line strings\n// where strings contain newlines. This improves readability for descriptions.\nfunc convertToMultiLineStrings(input string) string {\n\t// Regular expression to match key = \"value with \\n\"\n\t// We look for description fields specifically as those benefit most\n\tlines := strings.Split(input, \"\\n\")\n\tvar result []string\n\n\tfor _, line := range lines {\n\t\t// Check if this line has a string with escaped newlines","sourceCodeStart":674,"sourceCodeEnd":710,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/formula.go#L674-L710","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n{ \"tags\": [\"a\", 1, null] }   // mixed-type array, not TOML-encodable\n// after\n{ \"tags\": [\"a\", \"1\"] }","handlingStrategy":"validation","validationCode":"// ensure arrays are homogeneous before conversion\nfor _, v := range raw[\"tags\"].([]interface{}) {\n    if _, ok := v.(string); !ok { return errors.New(\"tags must be all strings\") }\n}","typeGuard":null,"tryCatchPattern":"if _, err := formulaToTOML(f); err != nil {\n    if strings.Contains(err.Error(), \"encoding TOML\") {\n        // inspect wrapped toml error; normalize mixed-type arrays/nulls in source JSON\n    }\n}","preventionTips":["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."],"tags":["formula","toml","serialization"],"backgroundTag":"toml-encoding-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}