jx/internal/folio/constraint.go

84 lines
1.8 KiB
Go
Raw Normal View History

2024-09-19 07:57:26 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package folio
import (
"decl/internal/data"
"errors"
"log/slog"
)
// Dependencies describe a requirement on a given system property
// system properties:
// loaded from config
//
// deps assigned to decl
// match values in document configurations
// match values in all configurations?
//
// documents/facter.jx.yaml -> facts -> Get(key)
//
// ConfigMapper? -> DocumentRegistry
// system:
// arch: amd64
// foo: bar
var (
ErrConstraintFailure = errors.New("Constraint failure")
)
type Constraint map[string]any
//func Compare[aV, bV map[K]V, K, V comparable](a aV, b bV) (matched bool) {
func CompareMap(a, b any) (matched bool) {
for k,v := range a.(Constraint) {
if bv, exists := b.(map[string]any)[k]; exists && bv == v {
matched = true
} else {
matched = false
}
}
return
}
func (c Constraint) CompareValues(a, b any) (matched bool) {
matched = true
slog.Info("Constraint.CompareValues()", "a", a, "b", b)
switch btype := b.(type) {
case map[string]string:
return CompareMap(a, b)
case map[string]any:
return CompareMap(a, b)
case []string:
acmp := a.([]string)
if len(acmp) > len(btype) {
return false
}
for i,v := range acmp {
if v != btype[i] {
return false
}
}
default:
return a == b
}
return
}
func (c Constraint) Check(configs data.ConfigurationValueGetter) (matched bool) {
matched = true
for k,v := range c {
slog.Info("Constraint.Check()", "constraint", c, "config", configs)
if configValue, err := configs.GetValue(k); err != nil || ! c.CompareValues(v, configValue) {
slog.Info("Constraint.Check() - FAILURE", "configvalue", configValue, "constraint", v, "error", err)
matched = false
}
}
return
}