|
| 1 | +// Copyright 2025 "Google LLC" |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package validators |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "hpc-toolkit/pkg/config" |
| 22 | + "hpc-toolkit/pkg/modulereader" |
| 23 | + |
| 24 | + "github.com/zclconf/go-cty/cty" |
| 25 | +) |
| 26 | + |
| 27 | +// getNestedValue retrieves a cty.Value from a Dict using a dot-separated path. |
| 28 | +func getNestedValue(d config.Dict, path string) (cty.Value, bool) { |
| 29 | + parts := strings.Split(path, ".") |
| 30 | + currentVal := d.AsObject() |
| 31 | + |
| 32 | + for i, part := range parts { |
| 33 | + if !currentVal.Type().IsObjectType() && !currentVal.Type().IsMapType() { |
| 34 | + return cty.NilVal, false |
| 35 | + } |
| 36 | + |
| 37 | + if !currentVal.Type().HasAttribute(part) { |
| 38 | + return cty.NilVal, false |
| 39 | + } |
| 40 | + |
| 41 | + val := currentVal.GetAttr(part) |
| 42 | + if i == len(parts)-1 { |
| 43 | + return val, true |
| 44 | + } |
| 45 | + currentVal = val |
| 46 | + } |
| 47 | + return cty.NilVal, false |
| 48 | +} |
| 49 | + |
| 50 | +// evaluateAndFlatten converts a cty.Value into a slice of cty.Value elements. |
| 51 | +// If it's a list/tuple, returns elements; otherwise returns single-item slice. |
| 52 | +func evaluateAndFlatten(val cty.Value) []cty.Value { |
| 53 | + var values []cty.Value |
| 54 | + if val.Type().IsListType() || val.Type().IsTupleType() { |
| 55 | + for it := val.ElementIterator(); it.Next(); { |
| 56 | + _, v := it.Element() |
| 57 | + values = append(values, v) |
| 58 | + } |
| 59 | + } else { |
| 60 | + values = append(values, val) |
| 61 | + } |
| 62 | + return values |
| 63 | +} |
| 64 | + |
| 65 | +// getModuleSettingValues retrieves a cty.Value from module settings using a dot-separated path, |
| 66 | +// evaluates expressions via bp.Eval and returns flattened slice + path for errors. |
| 67 | +func getModuleSettingValues(bp config.Blueprint, group config.Group, modIdx int, mod config.Module, settingName string) ([]cty.Value, config.Path, error) { |
| 68 | + var nilPath config.Path |
| 69 | + |
| 70 | + // Determine canonical path for this module.setting in the blueprint. |
| 71 | + groupIndex := bp.GroupIndex(group.Name) |
| 72 | + path := config.Root.Groups.At(groupIndex).Modules.At(modIdx).Settings.Dot(settingName) |
| 73 | + |
| 74 | + // If YAML context exists and the path is not present in the user's YAML, |
| 75 | + // treat the setting as absent so validators skip it (honoring optional:true). |
| 76 | + |
| 77 | + if bp.YamlCtx != nil { |
| 78 | + if _, ok := bp.YamlCtx.Pos(path); !ok { |
| 79 | + return nil, nilPath, fmt.Errorf("setting %q not present in blueprint YAML for module %q", settingName, mod.ID) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + val, _ := getNestedValue(mod.Settings, settingName) |
| 84 | + |
| 85 | + if evaledVal, err := bp.Eval(val); err == nil { |
| 86 | + val = evaledVal |
| 87 | + } |
| 88 | + values := evaluateAndFlatten(val) |
| 89 | + |
| 90 | + return values, path, nil |
| 91 | +} |
| 92 | + |
| 93 | +// parseStringList normalizes an input that may be a single string, []interface{} or nil into []string. |
| 94 | +func parseStringList(v interface{}) ([]string, bool) { |
| 95 | + if v == nil { |
| 96 | + return nil, false |
| 97 | + } |
| 98 | + switch vv := v.(type) { |
| 99 | + case string: |
| 100 | + return []string{vv}, true |
| 101 | + case []interface{}: |
| 102 | + out := make([]string, 0, len(vv)) |
| 103 | + for _, e := range vv { |
| 104 | + s, ok := e.(string) |
| 105 | + if !ok { |
| 106 | + return nil, false |
| 107 | + } |
| 108 | + out = append(out, s) |
| 109 | + } |
| 110 | + return out, true |
| 111 | + case []string: |
| 112 | + return vv, true |
| 113 | + default: |
| 114 | + return nil, false |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// Target represents a resolved target (module-setting or blueprint var) for validation. |
| 119 | +type Target struct { |
| 120 | + Name string |
| 121 | + Values []cty.Value |
| 122 | + Path config.Path |
| 123 | + IsBlueprint bool // true if came from blueprint vars, false if module.settings |
| 124 | +} |
| 125 | + |
| 126 | +// processModuleSettings processes a list of names interpreted as module settings. |
| 127 | +func processModuleSettings(bp config.Blueprint, mod config.Module, group config.Group, modIdx int, list []string, optional bool, handler func(Target) error) error { |
| 128 | + for _, s := range list { |
| 129 | + values, path, err := getModuleSettingValues(bp, group, modIdx, mod, s) |
| 130 | + if err != nil { |
| 131 | + if optional { |
| 132 | + continue |
| 133 | + } |
| 134 | + missingPath := config.Root.Groups.At(bp.GroupIndex(group.Name)).Modules.At(modIdx).Settings.Dot(s) |
| 135 | + return config.BpError{ |
| 136 | + Err: fmt.Errorf("setting %q not found in module %q settings", s, mod.ID), |
| 137 | + Path: missingPath, |
| 138 | + } |
| 139 | + } |
| 140 | + if err := handler(Target{Name: s, Values: values, Path: path, IsBlueprint: false}); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + } |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +// IterateRuleTargets resolves vars/settings from a validation rule according to scope and optional semantics, |
| 148 | +// and calls the provided handler for each resolved Target. The handler may return an error to stop iteration. |
| 149 | +func IterateRuleTargets( |
| 150 | + bp config.Blueprint, |
| 151 | + mod config.Module, |
| 152 | + rule modulereader.ValidationRule, |
| 153 | + group config.Group, |
| 154 | + modIdx int, |
| 155 | + handler func(Target) error, |
| 156 | +) error { |
| 157 | + |
| 158 | + // Only support `vars:` in module metadata validators (each treated as a module.setting name). |
| 159 | + // `settings:` support has been removed to enforce a single convention. |
| 160 | + varsList, _ := parseStringList(rule.Inputs["vars"]) |
| 161 | + optional := true |
| 162 | + if v, ok := rule.Inputs["optional"]; ok { |
| 163 | + if b, ok := v.(bool); ok { |
| 164 | + optional = b |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + // Interpret each var name as module.settings.<name> |
| 169 | + return processModuleSettings(bp, mod, group, modIdx, varsList, optional, handler) |
| 170 | +} |
0 commit comments