All checks were successful
ci/woodpecker/tag/release Pipeline was successful
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package outputschema
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/core/schema"
|
|
)
|
|
|
|
const (
|
|
SchemaBareSegments = "bare-segments"
|
|
SchemaAuditaV1 = "audita-v1"
|
|
)
|
|
|
|
type Encoder func(*schema.Transcript) ([]byte, error)
|
|
|
|
type Definition struct {
|
|
Key string
|
|
Encoder Encoder
|
|
}
|
|
|
|
var definitions = map[string]Definition{
|
|
SchemaBareSegments: {
|
|
Key: SchemaBareSegments,
|
|
Encoder: schema.TranscriptToJSON,
|
|
},
|
|
SchemaAuditaV1: {
|
|
Key: SchemaAuditaV1,
|
|
Encoder: encodeAuditaV1,
|
|
},
|
|
}
|
|
|
|
var supportedKeys = []string{
|
|
SchemaBareSegments,
|
|
SchemaAuditaV1,
|
|
}
|
|
|
|
func SupportedKeys() []string {
|
|
out := make([]string, len(supportedKeys))
|
|
copy(out, supportedKeys)
|
|
return out
|
|
}
|
|
|
|
func IsSupported(key string) bool {
|
|
_, ok := definitions[strings.TrimSpace(key)]
|
|
return ok
|
|
}
|
|
|
|
func Resolve(key string) (Definition, error) {
|
|
normalized := strings.TrimSpace(key)
|
|
if normalized == "" {
|
|
return Definition{}, fmt.Errorf("output schema must not be empty")
|
|
}
|
|
if !IsSupported(normalized) {
|
|
return Definition{}, fmt.Errorf("unsupported output schema %q", normalized)
|
|
}
|
|
def := definitions[normalized]
|
|
return def, nil
|
|
}
|
|
|
|
func encodeAuditaV1(transcript *schema.Transcript) ([]byte, error) {
|
|
payload := struct {
|
|
Schema string `json:"schema"`
|
|
Version string `json:"version"`
|
|
Segments []schema.Segment `json:"segments"`
|
|
}{
|
|
Schema: SchemaAuditaV1, Version: "v1", Segments: transcript.OutputSegments(),
|
|
}
|
|
return json.MarshalIndent(payload, "", " ")
|
|
}
|