Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion event_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@ package cotlib
import (
"encoding/xml"
"fmt"
"log/slog"

"github.com/NERVsystems/cotlib/validator"
)

// eventPointSchema holds the compiled schema for CoT event points.
var eventPointSchema *validator.Schema

// initErr stores any error encountered during schema compilation.
var initErr error

func init() {
var err error
eventPointSchema, err = validator.Compile(validator.EventPointXSD())
if err != nil {
panic(fmt.Errorf("compile event point schema: %w", err))
initErr = fmt.Errorf("compile event point schema: %w", err)
slog.Error("failed to compile event point schema", "error", err)
}
}

// ValidateAgainstSchema validates the given CoT event XML against the point schema.
func ValidateAgainstSchema(data []byte) error {
if initErr != nil {
return initErr
}
var p struct {
Point Point `xml:"point"`
}
Expand Down
31 changes: 31 additions & 0 deletions event_schema_error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cotlib

import (
"errors"
"testing"
)

// TestValidateAgainstSchemaInitError ensures ValidateAgainstSchema returns the
// initialization error instead of panicking when the schema failed to compile.
func TestValidateAgainstSchemaInitError(t *testing.T) {
// Save original values
origSchema := eventPointSchema
origErr := initErr
defer func() {
eventPointSchema = origSchema
initErr = origErr
}()

// Simulate compilation failure
initErr = errors.New("compile fail")
eventPointSchema = nil

if err := ValidateAgainstSchema(nil); err == nil || err.Error() != initErr.Error() {
t.Fatalf("expected %v, got %v", initErr, err)
}

// Call again to ensure same error returned and no panic
if err := ValidateAgainstSchema(nil); err == nil || err.Error() != initErr.Error() {
t.Fatalf("expected %v on second call, got %v", initErr, err)
}
}
Loading