omegafox/launcher/validate.go
daijro a2cb02df8a Add config type validator
- Validates property types passed in --config.
- Types are stored in properties.json.
2024-08-18 05:14:20 -05:00

67 lines
1.4 KiB
Go

package main
import (
"fmt"
"math"
"os"
"reflect"
)
type Property struct {
Property string `json:"property"`
Type string `json:"type"`
}
func validateConfig(configMap map[string]interface{}) {
properties := loadProperties()
// Create a map for quick lookup of property types
propertyTypes := make(map[string]string)
for _, prop := range properties {
propertyTypes[prop.Property] = prop.Type
}
for key, value := range configMap {
expectedType, exists := propertyTypes[key]
if !exists {
fmt.Printf("Warning: Unknown property %s in config\n", key)
continue
}
if !validateType(value, expectedType) {
fmt.Printf("Invalid type for property %s. Expected %s, got %T\n", key, expectedType, value)
os.Exit(1)
}
}
}
func loadProperties() []Property {
propertiesPath := getPath("properties.json")
var properties []Property
parseJson(propertiesPath, &properties)
return properties
}
func validateType(value interface{}, expectedType string) bool {
switch expectedType {
case "str":
_, ok := value.(string)
return ok
case "int":
v, ok := value.(float64)
return ok && v == math.Trunc(v)
case "uint":
v, ok := value.(float64)
return ok && v == math.Trunc(v) && v >= 0
case "double":
_, ok := value.(float64)
return ok
case "bool":
_, ok := value.(bool)
return ok
case "array":
return reflect.TypeOf(value).Kind() == reflect.Slice
default:
return false
}
}