mirror of
https://forge.fsky.io/oneflux/omegafox.git
synced 2026-02-10 21:52:04 -08:00
67 lines
1.4 KiB
Go
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
|
|
}
|
|
}
|