mirror of
https://github.com/hanzoai/o11y-foundry.git
synced 2026-11-02 14:03:05 +00:00
## Summary
- Split material domain into writer-facing `Material` and
traversal-capable `StructuredMaterial` interfaces with concrete
`YAMLMaterial`, `JSONMaterial`, `INIMaterial`, and `BlobMaterial` types.
- Inline INI and YAML format helpers into their respective material
types and remove `format.go` and `ini.go`.
- Rename interface methods for clarity: `Contents` → `JSONContents`,
`WithContents` → `CloneWithJSONContents`, `IsMultiDoc` →
`HasMultipleDocuments`.
- Switch material constructors to `internal/errors` with
`TypeInvalidInput` for parse failures, `TypeInternal` for canonical-JSON
marshaling failures, and `TypeNotFound` for missing traversal paths.
- Add `MustNew{JSON,YAML,INI}Material` constructors and split tests into
per-type `*_material_test.go` files.
- Tighten interface doc comments to follow godoc convention (lead with
identifier, drop pure restatement, document the gjson dotted-key path
syntax).
- Move `ini.PrettyFormat = false` into a package `init()` so the global
is touched once instead of on every `FmtContents` call.
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package domain
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/signoz/foundry/internal/errors"
|
|
)
|
|
|
|
var _ StructuredMaterial = JSONMaterial{}
|
|
|
|
type JSONMaterial struct {
|
|
path string
|
|
contents []byte
|
|
}
|
|
|
|
func NewJSONMaterial(contents []byte, path string) (JSONMaterial, error) {
|
|
if !json.Valid(contents) {
|
|
return JSONMaterial{}, errors.Newf(errors.TypeInvalidInput, "failed to create JSON material for path %q: contents are not valid JSON", path)
|
|
}
|
|
|
|
return JSONMaterial{
|
|
contents: contents,
|
|
path: path,
|
|
}, nil
|
|
}
|
|
|
|
func MustNewJSONMaterial(contents []byte, path string) JSONMaterial {
|
|
material, err := NewJSONMaterial(contents, path)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return material
|
|
}
|
|
|
|
func (m JSONMaterial) Path() string {
|
|
return m.path
|
|
}
|
|
|
|
func (m JSONMaterial) JSONContents() []byte {
|
|
return m.contents
|
|
}
|
|
|
|
func (m JSONMaterial) HasMultipleDocuments() bool {
|
|
return false
|
|
}
|
|
|
|
func (m JSONMaterial) FmtContents() []byte {
|
|
return m.contents
|
|
}
|
|
|
|
func (m JSONMaterial) CloneWithJSONContents(contents []byte) StructuredMaterial {
|
|
return JSONMaterial{
|
|
contents: contents,
|
|
path: m.path,
|
|
}
|
|
}
|
|
|
|
func (m JSONMaterial) GetBytes(path string) ([]byte, error) {
|
|
return getBytes(m.contents, path)
|
|
}
|
|
|
|
func (m JSONMaterial) GetStringSlice(path string) ([]string, error) {
|
|
return getStringSlice(m.contents, path)
|
|
}
|