Moved to CUE validation
This commit is contained in:
277
internal/schema/marte.cue
Normal file
277
internal/schema/marte.cue
Normal file
@@ -0,0 +1,277 @@
|
||||
package schema
|
||||
|
||||
#Classes: {
|
||||
RealTimeApplication: {
|
||||
Functions: {...} // type: node
|
||||
Data!: {...} // type: node
|
||||
States!: {...} // type: node
|
||||
...
|
||||
}
|
||||
StateMachine: {
|
||||
...
|
||||
}
|
||||
RealTimeState: {
|
||||
Threads: {...} // type: node
|
||||
...
|
||||
}
|
||||
RealTimeThread: {
|
||||
Functions: [...] // type: array
|
||||
...
|
||||
}
|
||||
GAMScheduler: {
|
||||
TimingDataSource: string // type: reference
|
||||
...
|
||||
}
|
||||
TimingDataSource: {
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
IOGAM: {
|
||||
InputSignals?: {...} // type: node
|
||||
OutputSignals?: {...} // type: node
|
||||
...
|
||||
}
|
||||
ReferenceContainer: {
|
||||
...
|
||||
}
|
||||
ConstantGAM: {
|
||||
...
|
||||
}
|
||||
PIDGAM: {
|
||||
Kp: float | int // type: float (allow int as it promotes)
|
||||
Ki: float | int
|
||||
Kd: float | int
|
||||
...
|
||||
}
|
||||
FileDataSource: {
|
||||
Filename: string
|
||||
Format?: string
|
||||
direction: "INOUT"
|
||||
...
|
||||
}
|
||||
LoggerDataSource: {
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
DANStream: {
|
||||
Timeout?: int
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
EPICSCAInput: {
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
EPICSCAOutput: {
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
EPICSPVAInput: {
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
EPICSPVAOutput: {
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
SDNSubscriber: {
|
||||
Address: string
|
||||
Port: int
|
||||
Interface?: string
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
SDNPublisher: {
|
||||
Address: string
|
||||
Port: int
|
||||
Interface?: string
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
UDPReceiver: {
|
||||
Port: int
|
||||
Address?: string
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
UDPSender: {
|
||||
Destination: string
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
FileReader: {
|
||||
Filename: string
|
||||
Format?: string
|
||||
Interpolate?: string
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
FileWriter: {
|
||||
Filename: string
|
||||
Format?: string
|
||||
StoreOnTrigger?: int
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
OrderedClass: {
|
||||
First: int
|
||||
Second: string
|
||||
...
|
||||
}
|
||||
BaseLib2GAM: {...}
|
||||
ConversionGAM: {...}
|
||||
DoubleHandshakeGAM: {...}
|
||||
FilterGAM: {
|
||||
Num: [...]
|
||||
Den: [...]
|
||||
ResetInEachState?: _
|
||||
InputSignals?: {...}
|
||||
OutputSignals?: {...}
|
||||
...
|
||||
}
|
||||
HistogramGAM: {
|
||||
BeginCycleNumber?: int
|
||||
StateChangeResetName?: string
|
||||
InputSignals?: {...}
|
||||
OutputSignals?: {...}
|
||||
...
|
||||
}
|
||||
Interleaved2FlatGAM: {...}
|
||||
FlattenedStructIOGAM: {...}
|
||||
MathExpressionGAM: {
|
||||
Expression: string
|
||||
InputSignals?: {...}
|
||||
OutputSignals?: {...}
|
||||
...
|
||||
}
|
||||
MessageGAM: {...}
|
||||
MuxGAM: {...}
|
||||
SimulinkWrapperGAM: {...}
|
||||
SSMGAM: {...}
|
||||
StatisticsGAM: {...}
|
||||
TimeCorrectionGAM: {...}
|
||||
TriggeredIOGAM: {...}
|
||||
WaveformGAM: {...}
|
||||
DAN: {
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
LinuxTimer: {
|
||||
ExecutionMode?: string
|
||||
SleepNature?: string
|
||||
SleepPercentage?: _
|
||||
Phase?: int
|
||||
CPUMask?: int
|
||||
TimeProvider?: {...}
|
||||
Signals: {...}
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
LinkDataSource: {
|
||||
direction: "INOUT"
|
||||
...
|
||||
}
|
||||
MDSReader: {
|
||||
TreeName: string
|
||||
ShotNumber: int
|
||||
Frequency: float | int
|
||||
Signals: {...}
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
MDSWriter: {
|
||||
NumberOfBuffers: int
|
||||
CPUMask: int
|
||||
StackSize: int
|
||||
TreeName: string
|
||||
PulseNumber?: int
|
||||
StoreOnTrigger: int
|
||||
EventName: string
|
||||
TimeRefresh: float | int
|
||||
NumberOfPreTriggers?: int
|
||||
NumberOfPostTriggers?: int
|
||||
Signals: {...}
|
||||
Messages?: {...}
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
NI1588TimeStamp: {
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
NI6259ADC: {
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
NI6259DAC: {
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
NI6259DIO: {
|
||||
direction: "INOUT"
|
||||
...
|
||||
}
|
||||
NI6368ADC: {
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
NI6368DAC: {
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
NI6368DIO: {
|
||||
direction: "INOUT"
|
||||
...
|
||||
}
|
||||
NI9157CircularFifoReader: {
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
NI9157MxiDataSource: {
|
||||
direction: "INOUT"
|
||||
...
|
||||
}
|
||||
OPCUADSInput: {
|
||||
direction: "IN"
|
||||
...
|
||||
}
|
||||
OPCUADSOutput: {
|
||||
direction: "OUT"
|
||||
...
|
||||
}
|
||||
RealTimeThreadAsyncBridge: {...}
|
||||
RealTimeThreadSynchronisation: {...}
|
||||
UARTDataSource: {
|
||||
direction: "INOUT"
|
||||
...
|
||||
}
|
||||
BaseLib2Wrapper: {...}
|
||||
EPICSCAClient: {...}
|
||||
EPICSPVA: {...}
|
||||
MemoryGate: {...}
|
||||
OPCUA: {...}
|
||||
SysLogger: {...}
|
||||
GAMDataSource: {
|
||||
direction: "INOUT"
|
||||
...
|
||||
}
|
||||
}
|
||||
|
||||
// Definition for any Object.
|
||||
// It must have a Class field.
|
||||
// Based on Class, it validates against #Classes.
|
||||
#Object: {
|
||||
Class: string
|
||||
// Allow any other field by default (extensibility),
|
||||
// unless #Classes definition is closed.
|
||||
// We allow open structs now.
|
||||
...
|
||||
|
||||
// Unify if Class is known.
|
||||
// If Class is NOT in #Classes, this might fail or do nothing depending on CUE logic.
|
||||
// Actually, `#Classes[Class]` fails if key is missing.
|
||||
// This ensures we validate against known classes.
|
||||
// If we want to allow unknown classes, we need a check.
|
||||
// But spec implies validation should check known classes.
|
||||
#Classes[Class]
|
||||
}
|
||||
@@ -1,237 +0,0 @@
|
||||
{
|
||||
"classes": {
|
||||
"RealTimeApplication": {
|
||||
"fields": [
|
||||
{"name": "Functions", "type": "node", "mandatory": true},
|
||||
{"name": "Data", "type": "node", "mandatory": true},
|
||||
{"name": "States", "type": "node", "mandatory": true}
|
||||
]
|
||||
},
|
||||
"StateMachine": {
|
||||
"fields": [
|
||||
{"name": "States", "type": "node", "mandatory": false}
|
||||
]
|
||||
},
|
||||
"RealTimeState": {
|
||||
"fields": [
|
||||
{"name": "Threads", "type": "node", "mandatory": true}
|
||||
]
|
||||
},
|
||||
"RealTimeThread": {
|
||||
"fields": [
|
||||
{"name": "Functions", "type": "array", "mandatory": true}
|
||||
]
|
||||
},
|
||||
"GAMScheduler": {
|
||||
"fields": [
|
||||
{"name": "TimingDataSource", "type": "reference", "mandatory": true}
|
||||
]
|
||||
},
|
||||
"TimingDataSource": {
|
||||
"fields": [],
|
||||
"direction": "IN"
|
||||
},
|
||||
"IOGAM": {
|
||||
"fields": [
|
||||
{"name": "InputSignals", "type": "node", "mandatory": false},
|
||||
{"name": "OutputSignals", "type": "node", "mandatory": false}
|
||||
]
|
||||
},
|
||||
"ReferenceContainer": {
|
||||
"fields": []
|
||||
},
|
||||
"ConstantGAM": {
|
||||
"fields": []
|
||||
},
|
||||
"PIDGAM": {
|
||||
"fields": [
|
||||
{"name": "Kp", "type": "float", "mandatory": true},
|
||||
{"name": "Ki", "type": "float", "mandatory": true},
|
||||
{"name": "Kd", "type": "float", "mandatory": true}
|
||||
]
|
||||
},
|
||||
"FileDataSource": {
|
||||
"fields": [
|
||||
{"name": "Filename", "type": "string", "mandatory": true},
|
||||
{"name": "Format", "type": "string", "mandatory": false}
|
||||
],
|
||||
"direction": "INOUT"
|
||||
},
|
||||
"LoggerDataSource": {
|
||||
"fields": [],
|
||||
"direction": "OUT"
|
||||
},
|
||||
"DANStream": {
|
||||
"fields": [
|
||||
{"name": "Timeout", "type": "int", "mandatory": false}
|
||||
],
|
||||
"direction": "OUT"
|
||||
},
|
||||
"EPICSCAInput": {
|
||||
"fields": [],
|
||||
"direction": "IN"
|
||||
},
|
||||
"EPICSCAOutput": {
|
||||
"fields": [],
|
||||
"direction": "OUT"
|
||||
},
|
||||
"EPICSPVAInput": {
|
||||
"fields": [],
|
||||
"direction": "IN"
|
||||
},
|
||||
"EPICSPVAOutput": {
|
||||
"fields": [],
|
||||
"direction": "OUT"
|
||||
},
|
||||
"SDNSubscriber": {
|
||||
"fields": [
|
||||
{"name": "Address", "type": "string", "mandatory": true},
|
||||
{"name": "Port", "type": "int", "mandatory": true},
|
||||
{"name": "Interface", "type": "string", "mandatory": false}
|
||||
],
|
||||
"direction": "IN"
|
||||
},
|
||||
"SDNPublisher": {
|
||||
"fields": [
|
||||
{"name": "Address", "type": "string", "mandatory": true},
|
||||
{"name": "Port", "type": "int", "mandatory": true},
|
||||
{"name": "Interface", "type": "string", "mandatory": false}
|
||||
],
|
||||
"direction": "OUT"
|
||||
},
|
||||
"UDPReceiver": {
|
||||
"fields": [
|
||||
{"name": "Port", "type": "int", "mandatory": true},
|
||||
{"name": "Address", "type": "string", "mandatory": false}
|
||||
],
|
||||
"direction": "IN"
|
||||
},
|
||||
"UDPSender": {
|
||||
"fields": [
|
||||
{"name": "Destination", "type": "string", "mandatory": true}
|
||||
],
|
||||
"direction": "OUT"
|
||||
},
|
||||
"FileReader": {
|
||||
"fields": [
|
||||
{"name": "Filename", "type": "string", "mandatory": true},
|
||||
{"name": "Format", "type": "string", "mandatory": false},
|
||||
{"name": "Interpolate", "type": "string", "mandatory": false}
|
||||
],
|
||||
"direction": "IN"
|
||||
},
|
||||
"FileWriter": {
|
||||
"fields": [
|
||||
{"name": "Filename", "type": "string", "mandatory": true},
|
||||
{"name": "Format", "type": "string", "mandatory": false},
|
||||
{"name": "StoreOnTrigger", "type": "int", "mandatory": false}
|
||||
],
|
||||
"direction": "OUT"
|
||||
},
|
||||
"OrderedClass": {
|
||||
"ordered": true,
|
||||
"fields": [
|
||||
{"name": "First", "type": "int", "mandatory": true},
|
||||
{"name": "Second", "type": "string", "mandatory": true}
|
||||
]
|
||||
},
|
||||
"BaseLib2GAM": { "fields": [] },
|
||||
"ConversionGAM": { "fields": [] },
|
||||
"DoubleHandshakeGAM": { "fields": [] },
|
||||
"FilterGAM": {
|
||||
"fields": [
|
||||
{"name": "Num", "type": "array", "mandatory": true},
|
||||
{"name": "Den", "type": "array", "mandatory": true},
|
||||
{"name": "ResetInEachState", "type": "any", "mandatory": false},
|
||||
{"name": "InputSignals", "type": "node", "mandatory": false},
|
||||
{"name": "OutputSignals", "type": "node", "mandatory": false}
|
||||
]
|
||||
},
|
||||
"HistogramGAM": {
|
||||
"fields": [
|
||||
{"name": "BeginCycleNumber", "type": "int", "mandatory": false},
|
||||
{"name": "StateChangeResetName", "type": "string", "mandatory": false},
|
||||
{"name": "InputSignals", "type": "node", "mandatory": false},
|
||||
{"name": "OutputSignals", "type": "node", "mandatory": false}
|
||||
]
|
||||
},
|
||||
"Interleaved2FlatGAM": { "fields": [] },
|
||||
"FlattenedStructIOGAM": { "fields": [] },
|
||||
"MathExpressionGAM": {
|
||||
"fields": [
|
||||
{"name": "Expression", "type": "string", "mandatory": true},
|
||||
{"name": "InputSignals", "type": "node", "mandatory": false},
|
||||
{"name": "OutputSignals", "type": "node", "mandatory": false}
|
||||
]
|
||||
},
|
||||
"MessageGAM": { "fields": [] },
|
||||
"MuxGAM": { "fields": [] },
|
||||
"SimulinkWrapperGAM": { "fields": [] },
|
||||
"SSMGAM": { "fields": [] },
|
||||
"StatisticsGAM": { "fields": [] },
|
||||
"TimeCorrectionGAM": { "fields": [] },
|
||||
"TriggeredIOGAM": { "fields": [] },
|
||||
"WaveformGAM": { "fields": [] },
|
||||
"DAN": { "fields": [], "direction": "OUT" },
|
||||
"LinuxTimer": {
|
||||
"fields": [
|
||||
{"name": "ExecutionMode", "type": "string", "mandatory": false},
|
||||
{"name": "SleepNature", "type": "string", "mandatory": false},
|
||||
{"name": "SleepPercentage", "type": "any", "mandatory": false},
|
||||
{"name": "Phase", "type": "int", "mandatory": false},
|
||||
{"name": "CPUMask", "type": "int", "mandatory": false},
|
||||
{"name": "TimeProvider", "type": "node", "mandatory": false},
|
||||
{"name": "Signals", "type": "node", "mandatory": true}
|
||||
],
|
||||
"direction": "IN"
|
||||
},
|
||||
"LinkDataSource": { "fields": [], "direction": "INOUT" },
|
||||
"MDSReader": {
|
||||
"fields": [
|
||||
{"name": "TreeName", "type": "string", "mandatory": true},
|
||||
{"name": "ShotNumber", "type": "int", "mandatory": true},
|
||||
{"name": "Frequency", "type": "float", "mandatory": true},
|
||||
{"name": "Signals", "type": "node", "mandatory": true}
|
||||
],
|
||||
"direction": "IN"
|
||||
},
|
||||
"MDSWriter": {
|
||||
"fields": [
|
||||
{"name": "NumberOfBuffers", "type": "int", "mandatory": true},
|
||||
{"name": "CPUMask", "type": "int", "mandatory": true},
|
||||
{"name": "StackSize", "type": "int", "mandatory": true},
|
||||
{"name": "TreeName", "type": "string", "mandatory": true},
|
||||
{"name": "PulseNumber", "type": "int", "mandatory": false},
|
||||
{"name": "StoreOnTrigger", "type": "int", "mandatory": true},
|
||||
{"name": "EventName", "type": "string", "mandatory": true},
|
||||
{"name": "TimeRefresh", "type": "float", "mandatory": true},
|
||||
{"name": "NumberOfPreTriggers", "type": "int", "mandatory": false},
|
||||
{"name": "NumberOfPostTriggers", "type": "int", "mandatory": false},
|
||||
{"name": "Signals", "type": "node", "mandatory": true},
|
||||
{"name": "Messages", "type": "node", "mandatory": false}
|
||||
],
|
||||
"direction": "OUT"
|
||||
},
|
||||
"NI1588TimeStamp": { "fields": [], "direction": "IN" },
|
||||
"NI6259ADC": { "fields": [], "direction": "IN" },
|
||||
"NI6259DAC": { "fields": [], "direction": "OUT" },
|
||||
"NI6259DIO": { "fields": [], "direction": "INOUT" },
|
||||
"NI6368ADC": { "fields": [], "direction": "IN" },
|
||||
"NI6368DAC": { "fields": [], "direction": "OUT" },
|
||||
"NI6368DIO": { "fields": [], "direction": "INOUT" },
|
||||
"NI9157CircularFifoReader": { "fields": [], "direction": "IN" },
|
||||
"NI9157MxiDataSource": { "fields": [], "direction": "INOUT" },
|
||||
"OPCUADSInput": { "fields": [], "direction": "IN" },
|
||||
"OPCUADSOutput": { "fields": [], "direction": "OUT" },
|
||||
"RealTimeThreadAsyncBridge": { "fields": [] },
|
||||
"RealTimeThreadSynchronisation": { "fields": [] },
|
||||
"UARTDataSource": { "fields": [], "direction": "INOUT" },
|
||||
"BaseLib2Wrapper": { "fields": [] },
|
||||
"EPICSCAClient": { "fields": [] },
|
||||
"EPICSPVA": { "fields": [] },
|
||||
"MemoryGate": { "fields": [] },
|
||||
"OPCUA": { "fields": [] },
|
||||
"SysLogger": { "fields": [] },
|
||||
"GAMDataSource": { "fields": [], "direction": "INOUT" }
|
||||
}
|
||||
}
|
||||
@@ -2,137 +2,73 @@ package schema
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
"cuelang.org/go/cue/cuecontext"
|
||||
)
|
||||
|
||||
//go:embed marte.json
|
||||
var defaultSchemaJSON []byte
|
||||
//go:embed marte.cue
|
||||
var defaultSchemaCUE []byte
|
||||
|
||||
type Schema struct {
|
||||
Classes map[string]ClassDefinition `json:"classes"`
|
||||
}
|
||||
|
||||
type ClassDefinition struct {
|
||||
Fields []FieldDefinition `json:"fields"`
|
||||
Ordered bool `json:"ordered"`
|
||||
Direction string `json:"direction"`
|
||||
}
|
||||
|
||||
type FieldDefinition struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"` // "int", "float", "string", "bool", "reference", "array", "node", "any"
|
||||
Mandatory bool `json:"mandatory"`
|
||||
Context *cue.Context
|
||||
Value cue.Value
|
||||
}
|
||||
|
||||
func NewSchema() *Schema {
|
||||
ctx := cuecontext.New()
|
||||
return &Schema{
|
||||
Classes: make(map[string]ClassDefinition),
|
||||
Context: ctx,
|
||||
Value: ctx.CompileBytes(defaultSchemaCUE),
|
||||
}
|
||||
}
|
||||
|
||||
func LoadSchema(path string) (*Schema, error) {
|
||||
// LoadSchema loads a CUE schema from a file and returns the cue.Value
|
||||
func LoadSchema(ctx *cue.Context, path string) (cue.Value, error) {
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var s Schema
|
||||
if err := json.Unmarshal(content, &s); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse schema: %v", err)
|
||||
}
|
||||
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// DefaultSchema returns the built-in embedded schema
|
||||
func DefaultSchema() *Schema {
|
||||
var s Schema
|
||||
if err := json.Unmarshal(defaultSchemaJSON, &s); err != nil {
|
||||
panic(fmt.Sprintf("failed to parse default embedded schema: %v", err))
|
||||
}
|
||||
if s.Classes == nil {
|
||||
s.Classes = make(map[string]ClassDefinition)
|
||||
}
|
||||
return &s
|
||||
}
|
||||
|
||||
// Merge adds rules from 'other' to 's'.
|
||||
// Rules for the same class are merged (new fields added, existing fields updated).
|
||||
func (s *Schema) Merge(other *Schema) {
|
||||
if other == nil {
|
||||
return
|
||||
}
|
||||
for className, classDef := range other.Classes {
|
||||
if existingClass, ok := s.Classes[className]; ok {
|
||||
// Merge fields
|
||||
fieldMap := make(map[string]FieldDefinition)
|
||||
for _, f := range classDef.Fields {
|
||||
fieldMap[f.Name] = f
|
||||
}
|
||||
|
||||
var mergedFields []FieldDefinition
|
||||
seen := make(map[string]bool)
|
||||
|
||||
// Keep existing fields, update if present in other
|
||||
for _, f := range existingClass.Fields {
|
||||
if newF, ok := fieldMap[f.Name]; ok {
|
||||
mergedFields = append(mergedFields, newF)
|
||||
} else {
|
||||
mergedFields = append(mergedFields, f)
|
||||
}
|
||||
seen[f.Name] = true
|
||||
}
|
||||
|
||||
// Append new fields
|
||||
for _, f := range classDef.Fields {
|
||||
if !seen[f.Name] {
|
||||
mergedFields = append(mergedFields, f)
|
||||
}
|
||||
}
|
||||
|
||||
existingClass.Fields = mergedFields
|
||||
if classDef.Ordered {
|
||||
existingClass.Ordered = true
|
||||
}
|
||||
if classDef.Direction != "" {
|
||||
existingClass.Direction = classDef.Direction
|
||||
}
|
||||
s.Classes[className] = existingClass
|
||||
} else {
|
||||
s.Classes[className] = classDef
|
||||
}
|
||||
return cue.Value{}, err
|
||||
}
|
||||
return ctx.CompileBytes(content), nil
|
||||
}
|
||||
|
||||
func LoadFullSchema(projectRoot string) *Schema {
|
||||
s := DefaultSchema()
|
||||
ctx := cuecontext.New()
|
||||
baseVal := ctx.CompileBytes(defaultSchemaCUE)
|
||||
if baseVal.Err() != nil {
|
||||
// Fallback or panic? Panic is appropriate for embedded schema failure
|
||||
panic(fmt.Sprintf("Embedded schema invalid: %v", baseVal.Err()))
|
||||
}
|
||||
|
||||
// 1. System Paths
|
||||
sysPaths := []string{
|
||||
"/usr/share/mdt/marte_schema.json",
|
||||
"/usr/share/mdt/marte_schema.cue",
|
||||
}
|
||||
|
||||
home, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
sysPaths = append(sysPaths, filepath.Join(home, ".local/share/mdt/marte_schema.json"))
|
||||
sysPaths = append(sysPaths, filepath.Join(home, ".local/share/mdt/marte_schema.cue"))
|
||||
}
|
||||
|
||||
for _, path := range sysPaths {
|
||||
if sysSchema, err := LoadSchema(path); err == nil {
|
||||
s.Merge(sysSchema)
|
||||
if val, err := LoadSchema(ctx, path); err == nil && val.Err() == nil {
|
||||
baseVal = baseVal.Unify(val)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Project Path
|
||||
if projectRoot != "" {
|
||||
projectSchemaPath := filepath.Join(projectRoot, ".marte_schema.json")
|
||||
if projSchema, err := LoadSchema(projectSchemaPath); err == nil {
|
||||
s.Merge(projSchema)
|
||||
projectSchemaPath := filepath.Join(projectRoot, ".marte_schema.cue")
|
||||
if val, err := LoadSchema(ctx, projectSchemaPath); err == nil && val.Err() == nil {
|
||||
baseVal = baseVal.Unify(val)
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
return &Schema{
|
||||
Context: ctx,
|
||||
Value: baseVal,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,12 @@ package validator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
"cuelang.org/go/cue/errors"
|
||||
|
||||
"github.com/marte-dev/marte-dev-tools/internal/index"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/schema"
|
||||
@@ -68,37 +72,9 @@ func (v *Validator) validateNode(node *index.ProjectNode) {
|
||||
}
|
||||
}
|
||||
|
||||
// Collect fields and their definitions
|
||||
fields := v.getFields(node)
|
||||
fieldOrder := []string{}
|
||||
for _, frag := range node.Fragments {
|
||||
for _, def := range frag.Definitions {
|
||||
if f, ok := def.(*parser.Field); ok {
|
||||
if _, exists := fields[f.Name]; exists { // already collected
|
||||
// Maintain order logic if needed, but getFields collects all.
|
||||
// For strict order check we might need this loop.
|
||||
// Let's assume getFields is enough for validation logic,
|
||||
// but for "duplicate check" and "class validation" we iterate fields map.
|
||||
// We need to construct fieldOrder.
|
||||
// Just reuse loop for fieldOrder
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Re-construct fieldOrder for order validation
|
||||
seen := make(map[string]bool)
|
||||
for _, frag := range node.Fragments {
|
||||
for _, def := range frag.Definitions {
|
||||
if f, ok := def.(*parser.Field); ok {
|
||||
if !seen[f.Name] {
|
||||
fieldOrder = append(fieldOrder, f.Name)
|
||||
seen[f.Name] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Check for duplicate fields
|
||||
// 1. Check for duplicate fields (Go logic)
|
||||
for name, defs := range fields {
|
||||
if len(defs) > 1 {
|
||||
firstFile := v.getFileForField(defs[0], node)
|
||||
@@ -139,11 +115,9 @@ func (v *Validator) validateNode(node *index.ProjectNode) {
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Schema Validation
|
||||
// 3. CUE Validation
|
||||
if className != "" && v.Schema != nil {
|
||||
if classDef, ok := v.Schema.Classes[className]; ok {
|
||||
v.validateClass(node, classDef, fields, fieldOrder)
|
||||
}
|
||||
v.validateWithCUE(node, className)
|
||||
}
|
||||
|
||||
// 4. Signal Validation (for DataSource signals)
|
||||
@@ -162,68 +136,95 @@ func (v *Validator) validateNode(node *index.ProjectNode) {
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Validator) validateClass(node *index.ProjectNode, classDef schema.ClassDefinition, fields map[string][]*parser.Field, fieldOrder []string) {
|
||||
// ... (same as before)
|
||||
for _, fieldDef := range classDef.Fields {
|
||||
if fieldDef.Mandatory {
|
||||
found := false
|
||||
if _, ok := fields[fieldDef.Name]; ok {
|
||||
found = true
|
||||
} else if fieldDef.Type == "node" {
|
||||
if _, ok := node.Children[fieldDef.Name]; ok {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
func (v *Validator) validateWithCUE(node *index.ProjectNode, className string) {
|
||||
// Check if class exists in schema
|
||||
classPath := cue.ParsePath(fmt.Sprintf("#Classes.%s", className))
|
||||
if v.Schema.Value.LookupPath(classPath).Err() != nil {
|
||||
return // Unknown class, skip validation
|
||||
}
|
||||
|
||||
if !found {
|
||||
v.Diagnostics = append(v.Diagnostics, Diagnostic{
|
||||
Level: LevelError,
|
||||
Message: fmt.Sprintf("Missing mandatory field '%s' for class '%s'", fieldDef.Name, node.Metadata["Class"]),
|
||||
Position: v.getNodePosition(node),
|
||||
File: v.getNodeFile(node),
|
||||
})
|
||||
}
|
||||
// Convert node to map
|
||||
data := v.nodeToMap(node)
|
||||
|
||||
// Encode data to CUE
|
||||
dataVal := v.Schema.Context.Encode(data)
|
||||
|
||||
// Unify with #Object
|
||||
// #Object requires "Class" field, which is present in data.
|
||||
objDef := v.Schema.Value.LookupPath(cue.ParsePath("#Object"))
|
||||
|
||||
// Unify
|
||||
res := objDef.Unify(dataVal)
|
||||
|
||||
if err := res.Validate(cue.Concrete(true)); err != nil {
|
||||
// Report errors
|
||||
|
||||
// Parse CUE error to diagnostic
|
||||
v.reportCUEError(err, node)
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Validator) reportCUEError(err error, node *index.ProjectNode) {
|
||||
list := errors.Errors(err)
|
||||
for _, e := range list {
|
||||
msg := e.Error()
|
||||
v.Diagnostics = append(v.Diagnostics, Diagnostic{
|
||||
Level: LevelError,
|
||||
Message: fmt.Sprintf("Schema Validation Error: %v", msg),
|
||||
Position: v.getNodePosition(node),
|
||||
File: v.getNodeFile(node),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Validator) nodeToMap(node *index.ProjectNode) map[string]interface{} {
|
||||
m := make(map[string]interface{})
|
||||
fields := v.getFields(node)
|
||||
|
||||
for name, defs := range fields {
|
||||
if len(defs) > 0 {
|
||||
// Use the last definition (duplicates checked elsewhere)
|
||||
m[name] = v.valueToInterface(defs[len(defs)-1].Value)
|
||||
}
|
||||
}
|
||||
|
||||
for _, fieldDef := range classDef.Fields {
|
||||
if fList, ok := fields[fieldDef.Name]; ok {
|
||||
f := fList[0]
|
||||
if !v.checkType(f.Value, fieldDef.Type) {
|
||||
v.Diagnostics = append(v.Diagnostics, Diagnostic{
|
||||
Level: LevelError,
|
||||
Message: fmt.Sprintf("Field '%s' expects type '%s'", fieldDef.Name, fieldDef.Type),
|
||||
Position: f.Position,
|
||||
File: v.getFileForField(f, node),
|
||||
})
|
||||
}
|
||||
}
|
||||
// Children as nested maps?
|
||||
// CUE schema expects nested structs for "node" type fields.
|
||||
// But `node.Children` contains ALL children (even those defined as +Child).
|
||||
// If schema expects `States: { ... }`, we map children.
|
||||
|
||||
for name, child := range node.Children {
|
||||
// normalize name? CUE keys are strings.
|
||||
// If child real name is "+States", key in Children is "States".
|
||||
// We use "States" as key in map.
|
||||
m[name] = v.nodeToMap(child)
|
||||
}
|
||||
|
||||
if classDef.Ordered {
|
||||
schemaIdx := 0
|
||||
for _, nodeFieldName := range fieldOrder {
|
||||
foundInSchema := false
|
||||
for i, fd := range classDef.Fields {
|
||||
if fd.Name == nodeFieldName {
|
||||
foundInSchema = true
|
||||
if i < schemaIdx {
|
||||
v.Diagnostics = append(v.Diagnostics, Diagnostic{
|
||||
Level: LevelError,
|
||||
Message: fmt.Sprintf("Field '%s' is out of order", nodeFieldName),
|
||||
Position: fields[nodeFieldName][0].Position,
|
||||
File: v.getFileForField(fields[nodeFieldName][0], node),
|
||||
})
|
||||
} else {
|
||||
schemaIdx = i
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if !foundInSchema {
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (v *Validator) valueToInterface(val parser.Value) interface{} {
|
||||
switch t := val.(type) {
|
||||
case *parser.StringValue:
|
||||
return t.Value
|
||||
case *parser.IntValue:
|
||||
i, _ := strconv.ParseInt(t.Raw, 0, 64)
|
||||
return i // CUE handles int64
|
||||
case *parser.FloatValue:
|
||||
f, _ := strconv.ParseFloat(t.Raw, 64)
|
||||
return f
|
||||
case *parser.BoolValue:
|
||||
return t.Value
|
||||
case *parser.ReferenceValue:
|
||||
return t.Value
|
||||
case *parser.ArrayValue:
|
||||
var arr []interface{}
|
||||
for _, e := range t.Elements {
|
||||
arr = append(arr, v.valueToInterface(e))
|
||||
}
|
||||
return arr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *Validator) validateSignal(node *index.ProjectNode, fields map[string][]*parser.Field) {
|
||||
@@ -308,12 +309,17 @@ func (v *Validator) validateGAMSignal(gamNode, signalNode *index.ProjectNode, di
|
||||
}
|
||||
}
|
||||
|
||||
// Check Direction
|
||||
// Check Direction using CUE Schema
|
||||
dsClass := v.getNodeClass(dsNode)
|
||||
if dsClass != "" {
|
||||
if classDef, ok := v.Schema.Classes[dsClass]; ok {
|
||||
dsDir := classDef.Direction
|
||||
if dsDir != "" {
|
||||
// Lookup class definition in Schema
|
||||
// path: #Classes.ClassName.direction
|
||||
path := cue.ParsePath(fmt.Sprintf("#Classes.%s.direction", dsClass))
|
||||
val := v.Schema.Value.LookupPath(path)
|
||||
|
||||
if val.Err() == nil {
|
||||
dsDir, err := val.String()
|
||||
if err == nil && dsDir != "" {
|
||||
if direction == "Input" && dsDir == "OUT" {
|
||||
v.Diagnostics = append(v.Diagnostics, Diagnostic{
|
||||
Level: LevelError,
|
||||
@@ -537,32 +543,7 @@ func isValidType(t string) bool {
|
||||
}
|
||||
|
||||
func (v *Validator) checkType(val parser.Value, expectedType string) bool {
|
||||
// ... (same as before)
|
||||
switch expectedType {
|
||||
case "int":
|
||||
_, ok := val.(*parser.IntValue)
|
||||
return ok
|
||||
case "float":
|
||||
_, ok := val.(*parser.FloatValue)
|
||||
return ok
|
||||
case "string":
|
||||
_, okStr := val.(*parser.StringValue)
|
||||
_, okRef := val.(*parser.ReferenceValue)
|
||||
return okStr || okRef
|
||||
case "bool":
|
||||
_, ok := val.(*parser.BoolValue)
|
||||
return ok
|
||||
case "array":
|
||||
_, ok := val.(*parser.ArrayValue)
|
||||
return ok
|
||||
case "reference":
|
||||
_, ok := val.(*parser.ReferenceValue)
|
||||
return ok
|
||||
case "node":
|
||||
return true
|
||||
case "any":
|
||||
return true
|
||||
}
|
||||
// Legacy function, replaced by CUE.
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -679,7 +660,8 @@ func isDataSource(node *index.ProjectNode) bool {
|
||||
if node.Parent != nil && node.Parent.Name == "Data" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
_, hasSignals := node.Children["Signals"]
|
||||
return hasSignals
|
||||
}
|
||||
|
||||
func isSignal(node *index.ProjectNode) bool {
|
||||
|
||||
Reference in New Issue
Block a user