summaryrefslogtreecommitdiff
path: root/gonf/variables.go
diff options
context:
space:
mode:
authorJulien Dessaux2024-02-05 00:59:31 +0100
committerJulien Dessaux2024-03-07 00:58:09 +0100
commitceac85dbc11cecb95c738049f2d4fd43c7d18828 (patch)
treec08a56673092402c8126ef6a4b8cf5a9c2987195 /gonf/variables.go
parentchore(repo): initial import (diff)
downloadgonf-ceac85dbc11cecb95c738049f2d4fd43c7d18828.tar.gz
gonf-ceac85dbc11cecb95c738049f2d4fd43c7d18828.tar.bz2
gonf-ceac85dbc11cecb95c738049f2d4fd43c7d18828.zip
chore(gonf): first draft of the gonf lib with commands, files, packages and variables
Diffstat (limited to 'gonf/variables.go')
-rw-r--r--gonf/variables.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/gonf/variables.go b/gonf/variables.go
new file mode 100644
index 0000000..3c365ae
--- /dev/null
+++ b/gonf/variables.go
@@ -0,0 +1,57 @@
+package gonf
+
+import "log/slog"
+
+// ----- Globals ---------------------------------------------------------------
+var variables map[string]*VariablePromise
+
+// ----- Init ------------------------------------------------------------------
+func init() {
+ variables = make(map[string]*VariablePromise)
+}
+
+// ----- Public ----------------------------------------------------------------
+func Default(name string, value Value) *VariablePromise {
+ if v, ok := variables[name]; ok {
+ if !v.isDefault {
+ slog.Debug("default would overwrite a variable, ignoring", "name", name, "old_value", v.value, "new_value", value)
+ return nil
+ }
+ slog.Error("default is being overwritten", "name", name, "old_value", v.value, "new_value", value)
+ }
+ v := &VariablePromise{
+ isDefault: true,
+ name: name,
+ value: value,
+ }
+ variables[name] = v
+ return v
+}
+func Variable(name string, value Value) *VariablePromise {
+ if v, ok := variables[name]; ok && !v.isDefault {
+ slog.Error("variable is being overwritten", "name", name, "old_value", v, "new_value", value)
+ }
+ v := &VariablePromise{
+ isDefault: false,
+ name: name,
+ value: value,
+ }
+ variables[name] = v
+ return v
+}
+
+type VariablePromise struct {
+ isDefault bool
+ name string
+ value Value
+}
+
+// ----- Internal --------------------------------------------------------------
+func getVariable(name string) string {
+ if v, ok := variables[name]; ok {
+ return v.value.String()
+ } else {
+ slog.Error("undefined variable or default", "name", name)
+ return name
+ }
+}