summaryrefslogtreecommitdiff
path: root/pkg/values.go
diff options
context:
space:
mode:
authorJulien Dessaux2024-03-07 00:54:35 +0100
committerJulien Dessaux2024-03-07 01:02:23 +0100
commit560becfd32dd7355547938f3c6229060dd395aab (patch)
tree42329f4e47c64349ae8a0205f867632ac52bcdc6 /pkg/values.go
parentfeat(stdlib): began adding systemd services support (diff)
downloadgonf-560becfd32dd7355547938f3c6229060dd395aab.tar.gz
gonf-560becfd32dd7355547938f3c6229060dd395aab.tar.bz2
gonf-560becfd32dd7355547938f3c6229060dd395aab.zip
chore(repo): renamed gonf subfolder to something more traditional in go land
Diffstat (limited to 'pkg/values.go')
-rw-r--r--pkg/values.go104
1 files changed, 104 insertions, 0 deletions
diff --git a/pkg/values.go b/pkg/values.go
new file mode 100644
index 0000000..509b6bf
--- /dev/null
+++ b/pkg/values.go
@@ -0,0 +1,104 @@
+package gonf
+
+import (
+ "fmt"
+ "log/slog"
+ "strings"
+)
+
+type Value interface {
+ Bytes() []byte
+ String() string
+}
+
+func interfaceToValue(v any) Value {
+ if vv, ok := v.([]byte); ok {
+ return &BytesValue{vv}
+ }
+ if vv, ok := v.(int); ok {
+ return &IntValue{vv}
+ }
+ if vv, ok := v.(string); ok {
+ return &StringValue{vv}
+ }
+ if vv, ok := v.(*VariablePromise); ok {
+ return vv
+ }
+ slog.Error("interfaceToValue", "value", v, "error", "Not Implemented")
+ panic(fmt.Sprintf("interfaceToValue cannot take type %T as argument. Value was %#v.", v, v))
+}
+
+func interfaceToTemplateValue(v any) Value {
+ if vv, ok := v.([]byte); ok {
+ return &TemplateValue{data: string(vv)}
+ }
+ if vv, ok := v.(int); ok {
+ return &IntValue{vv}
+ }
+ if vv, ok := v.(string); ok {
+ return &TemplateValue{data: vv}
+ }
+ if vv, ok := v.(*VariablePromise); ok {
+ return vv
+ }
+ slog.Error("interfaceToTemplateValue", "value", v, "error", "Not Implemented")
+ panic(fmt.Sprintf("interfaceToTemplateValue cannot take type %T as argument. Value was %#v.", v, v))
+}
+
+// ----- BytesValue ------------------------------------------------------------
+type BytesValue struct {
+ value []byte
+}
+
+func (b BytesValue) Bytes() []byte {
+ return b.value
+}
+func (b BytesValue) String() string {
+ return string(b.value[:])
+}
+
+// ----- IntValue --------------------------------------------------------------
+type IntValue struct {
+ value int
+}
+
+func (i IntValue) Bytes() []byte {
+ return []byte(string(i.value))
+}
+func (i IntValue) Int() int {
+ return i.value
+}
+func (i IntValue) String() string {
+ return string(i.value)
+}
+
+// ----- StringsListValue ------------------------------------------------------
+type StringsListValue struct {
+ value []string
+}
+
+func (s *StringsListValue) Append(v ...string) {
+ s.value = append(s.value, v...)
+}
+func (s StringsListValue) Bytes() []byte {
+ return []byte(s.String())
+}
+func (s StringsListValue) String() string {
+ return strings.Join(s.value, "\n")
+}
+
+// ----- StringValue -----------------------------------------------------------
+type StringValue struct {
+ value string
+}
+
+func (s StringValue) Bytes() []byte {
+ return []byte(s.value)
+}
+func (s StringValue) String() string {
+ return s.value
+}
+
+// TODO maps
+
+// TODO what else?