mirror of
https://github.com/hanzoai/o11y-foundry.git
synced 2026-10-30 18:26:45 +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.
82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
package domain
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNewJSONMaterial(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
contents []byte
|
|
path string
|
|
pass bool
|
|
expectedJSON []byte
|
|
expectedFmtContents []byte
|
|
expectedHasMultipleDocuments bool
|
|
}{
|
|
{
|
|
name: "Valid",
|
|
contents: []byte(`{"service":{"names":["query-service","frontend"]}}`),
|
|
path: "service.json",
|
|
pass: true,
|
|
expectedJSON: []byte(`{"service":{"names":["query-service","frontend"]}}`),
|
|
expectedFmtContents: []byte(`{"service":{"names":["query-service","frontend"]}}`),
|
|
expectedHasMultipleDocuments: false,
|
|
},
|
|
{
|
|
name: "Invalid",
|
|
contents: []byte(`{"service":`),
|
|
path: "service.json",
|
|
pass: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
material, err := NewJSONMaterial(tt.contents, tt.path)
|
|
if !tt.pass {
|
|
assert.Error(t, err)
|
|
return
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.path, material.Path())
|
|
assert.Equal(t, tt.expectedHasMultipleDocuments, material.HasMultipleDocuments())
|
|
assert.JSONEq(t, string(tt.expectedJSON), string(material.JSONContents()))
|
|
assert.Equal(t, tt.expectedFmtContents, material.FmtContents())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestJSONMaterialGetBytes(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
material JSONMaterial
|
|
path string
|
|
pass bool
|
|
expected []byte
|
|
}{
|
|
{
|
|
name: "Exists",
|
|
material: MustNewJSONMaterial([]byte(`{"service":{"name":"query-service"}}`), "service.json"),
|
|
path: "service.name",
|
|
pass: true,
|
|
expected: []byte("query-service"),
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
output, err := tt.material.GetBytes(tt.path)
|
|
if !tt.pass {
|
|
assert.Error(t, err)
|
|
return
|
|
}
|
|
|
|
assert.Equal(t, tt.expected, output)
|
|
})
|
|
}
|
|
}
|