package resolution import ( "encoding/json" "fmt" "sort" stdio "git.novox.be/novox/mesh-sdk/go" mcat "git.novox.be/novox/mesh-sdk/go/catalogue" ) // settingWords is every setting m declares that a layer over it sets, its default among them, as the word the one // settings reader takes it from (design 47 H2 §781, H-A-370): stdio.SettingWord(), MESH_CONFIG_, holding the // value as text (a duration as "10m", a number as a program reads it, a list or an object as JSON). Composed alike into // each of m's processes' environments and into m's slice of the tool runner's MESH_TOOL_ENV. Nil when it sets none. func settingWords(m mcat.Manifest, layers []mcat.Layer) (map[string]string, error) { keys := declaredConfig(m) sort.Strings(keys) var out map[string]string for _, key := range keys { value, set := settingValue(WithDefaults(m, layers), key) if !set { continue } text := plainly(value) switch value.(type) { case map[string]any, []any: raw, err := json.Marshal(value) if err != nil { return nil, fmt.Errorf("%s's setting %s cannot be written as text: %w", m.Module, key, err) } text = string(raw) } if out == nil { out = map[string]string{} } out[stdio.SettingWord(key)] = text } return out, nil } // withSettingWords is words with each of the setting words laid in, refusing a word the module's own words give // another value: a module's settings reach its code under one word each (H-A-370). func withSettingWords(words, settings map[string]string, module, where string) (map[string]string, error) { for word, value := range settings { if was, had := words[word]; had && was != value { return nil, fmt.Errorf("%s's %s gives %s the value %q, and the word is its setting's, composed as %q (design 47 H2 §781)", module, where, word, was, value) } if words == nil { words = map[string]string{} } words[word] = value } return words, nil } // settingWordsInto lays m's setting words into a process resource's environment (H-A-370); any other resource is left. func settingWordsInto(resource map[string]any, m mcat.Manifest, layers []mcat.Layer) error { if fmt.Sprint(resource["type"]) != "process" { return nil } settings, err := settingWords(m, layers) if err != nil || len(settings) == 0 { return err } env, _ := resource["env"].(map[string]any) words := map[string]string{} for k, v := range env { words[k] = fmt.Sprint(v) } if _, err := withSettingWords(words, settings, m.Module, fmt.Sprintf("process %v", resource["id"])); err != nil { return err } filled := map[string]any{} for k, v := range env { filled[k] = v } for word, value := range settings { filled[word] = value } resource["env"] = filled return nil }