From 560becfd32dd7355547938f3c6229060dd395aab Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 7 Mar 2024 00:54:35 +0100 Subject: chore(repo): renamed gonf subfolder to something more traditional in go land --- pkg/values.go | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 pkg/values.go (limited to 'pkg/values.go') 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? -- cgit v1.2.3