Files
audita/internal/appconfig/sharedglossary/load.go

53 lines
1.4 KiB
Go

package sharedglossary
import (
"context"
"errors"
"strings"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
ck "go.maximumdirect.net/configkit"
)
// Load composes ordered shared documents and validates the completed envelope.
// A nil result means no documents were supplied.
func Load(ctx context.Context, inputs []ck.Input) (*ck.Result[map[string]any], error) {
if err := ctx.Err(); err != nil {
return nil, err
}
if len(inputs) == 0 {
return nil, nil
}
validator, err := NewValidator()
if err != nil {
return nil, err
}
underlays := make([]ck.Underlay, 0, len(inputs)-1)
for _, input := range inputs[:len(inputs)-1] {
input.Imports = true
underlays = append(underlays, ck.Underlay{Inputs: []ck.Input{input}})
}
main := inputs[len(inputs)-1]
main.Imports = true
result, err := ck.LoadLayered(main, ck.LayeredOptions[map[string]any]{
Definition: ck.Definition[map[string]any]{SequenceIdentities: []ck.SequenceIdentity{config.GlossaryEntryIdentity(ck.SequenceSelector{ck.SequenceLiteralKey("collections")})}},
Underlays: underlays,
})
if canceled := ctx.Err(); canceled != nil {
return nil, errors.Join(err, canceled)
}
if err != nil {
return nil, err
}
var sources []string
for _, source := range result.Sources() {
if source.Path != "" {
sources = append(sources, source.Path)
}
}
if err := validator.Validate(result.Config, strings.Join(sources, ", ")); err != nil {
return nil, err
}
return result, nil
}