Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

chore: add type for strings that should be unset or non empty (#12774)

Naman Vermae0da06f76d6d
diff --git a/pkg/valuer/unset_or_non_empty_string.go b/pkg/valuer/unset_or_non_empty_string.go
new file mode 100644
index 0000000000..ecf52f2c92
--- /dev/null
+++ b/pkg/valuer/unset_or_non_empty_string.go
@@ -0,0 +1,113 @@
+package valuer
+
+import (
+ "database/sql/driver"
+ "encoding/json"
+
+ "github.com/SigNoz/signoz/pkg/errors"
+)
+
+var _ Valuer = (*UnsetOrNonEmptyString)(nil)
+
+// UnsetOrNonEmptyString separates a field left out of the input from one
+// explicitly set to "". Decoding rejects "", and json calls UnmarshalJSON only
+// for a field that is present, so a field holding "" is a field nobody set.
+type UnsetOrNonEmptyString struct {
+ val string
+}
+
+func NewUnsetOrNonEmptyString(val string) (UnsetOrNonEmptyString, error) {
+ if val == "" {
+ return UnsetOrNonEmptyString{}, errors.Newf(errors.TypeInvalidInput, ErrCodeInvalidValuer, "string must not be empty")
+ }
+
+ return UnsetOrNonEmptyString{val: val}, nil
+}
+
+func MustNewUnsetOrNonEmptyString(val string) UnsetOrNonEmptyString {
+ nonEmptyString, err := NewUnsetOrNonEmptyString(val)
+ if err != nil {
+ panic(err)
+ }
+
+ return nonEmptyString
+}
+
+func (enum UnsetOrNonEmptyString) IsZero() bool {
+ return enum.val == ""
+}
+
+func (enum UnsetOrNonEmptyString) StringValue() string {
+ return enum.val
+}
+
+func (enum UnsetOrNonEmptyString) String() string {
+ return enum.val
+}
+
+func (enum UnsetOrNonEmptyString) MarshalJSON() ([]byte, error) {
+ return json.Marshal(enum.StringValue())
+}
+
+func (enum *UnsetOrNonEmptyString) UnmarshalJSON(data []byte) error {
+ var str string
+ if err := json.Unmarshal(data, &str); err != nil {
+ return err
+ }
+
+ var err error
+ *enum, err = NewUnsetOrNonEmptyString(str)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (enum UnsetOrNonEmptyString) Value() (driver.Value, error) {
+ return enum.StringValue(), nil
+}
+
+func (enum *UnsetOrNonEmptyString) Scan(val any) error {
+ if enum == nil {
+ return errors.Newf(errors.TypeInternal, ErrCodeUnknownValuerScan, "unset_or_non_empty_string: (nil \"%T\")", enum)
+ }
+
+ str, ok := val.(string)
+ if !ok {
+ return errors.Newf(errors.TypeInternal, ErrCodeUnknownValuerScan, "unset_or_non_empty_string: (non-string \"%T\")", val)
+ }
+
+ var err error
+ *enum, err = NewUnsetOrNonEmptyString(str)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (enum *UnsetOrNonEmptyString) UnmarshalText(text []byte) error {
+ var err error
+ *enum, err = NewUnsetOrNonEmptyString(string(text))
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (enum UnsetOrNonEmptyString) MarshalText() (text []byte, err error) {
+ return []byte(enum.StringValue()), nil
+}
+
+func (enum *UnsetOrNonEmptyString) UnmarshalParam(param string) error {
+ nonEmptyString, err := NewUnsetOrNonEmptyString(param)
+ if err != nil {
+ return err
+ }
+
+ *enum = nonEmptyString
+
+ return nil
+}
diff --git a/pkg/valuer/unset_or_non_empty_string_test.go b/pkg/valuer/unset_or_non_empty_string_test.go
new file mode 100644
index 0000000000..47f0179992
--- /dev/null
+++ b/pkg/valuer/unset_or_non_empty_string_test.go
@@ -0,0 +1,92 @@
+package valuer
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestNewUnsetOrNonEmptyString(t *testing.T) {
+ testCases := []struct {
+ description string
+ value string
+ expectedError bool
+ expectedWrapped UnsetOrNonEmptyString
+ }{
+ {description: "plain value", value: "oncall", expectedWrapped: UnsetOrNonEmptyString{val: "oncall"}},
+ {description: "case and surrounding space are kept", value: " On Call ", expectedWrapped: UnsetOrNonEmptyString{val: " On Call "}},
+ {description: "whitespace alone is not empty", value: " ", expectedWrapped: UnsetOrNonEmptyString{val: " "}},
+ {description: "empty", value: "", expectedError: true},
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.description, func(t *testing.T) {
+ nonEmptyString, err := NewUnsetOrNonEmptyString(testCase.value)
+ if testCase.expectedError {
+ assert.Error(t, err)
+ return
+ }
+
+ require.NoError(t, err)
+ assert.Equal(t, testCase.expectedWrapped, nonEmptyString)
+ })
+ }
+}
+
+func TestUnsetOrNonEmptyStringUnmarshalJSONRejectsAnEmptyString(t *testing.T) {
+ var target struct {
+ Title UnsetOrNonEmptyString `json:"title"`
+ }
+
+ require.NoError(t, json.Unmarshal([]byte(`{"title":"Alert"}`), &target))
+ assert.Equal(t, "Alert", target.Title.StringValue())
+
+ assert.Error(t, json.Unmarshal([]byte(`{"title":""}`), &target))
+}
+
+// The zero value is the only way an empty UnsetOrNonEmptyString comes about, and it is
+// what lets a caller omit the field and take a default filled in elsewhere.
+func TestUnsetOrNonEmptyStringUnmarshalJSONLeavesAnAbsentFieldZero(t *testing.T) {
+ var target struct {
+ Title UnsetOrNonEmptyString `json:"title"`
+ }
+
+ require.NoError(t, json.Unmarshal([]byte(`{}`), &target))
+ assert.True(t, target.Title.IsZero())
+}
+
+func TestUnsetOrNonEmptyStringMarshalJSON(t *testing.T) {
+ raw, err := json.Marshal(MustNewUnsetOrNonEmptyString("Alert"))
+ require.NoError(t, err)
+ assert.JSONEq(t, `"Alert"`, string(raw))
+}
+
+func TestUnsetOrNonEmptyStringScanRejectsAnEmptyString(t *testing.T) {
+ var nonEmptyString UnsetOrNonEmptyString
+
+ require.NoError(t, nonEmptyString.Scan("oncall"))
+ assert.Equal(t, "oncall", nonEmptyString.StringValue())
+
+ assert.Error(t, nonEmptyString.Scan(""))
+ assert.Error(t, nonEmptyString.Scan(nil))
+}
+
+func TestUnsetOrNonEmptyStringUnmarshalTextRejectsAnEmptyString(t *testing.T) {
+ var nonEmptyString UnsetOrNonEmptyString
+
+ require.NoError(t, nonEmptyString.UnmarshalText([]byte("oncall")))
+ assert.Equal(t, "oncall", nonEmptyString.StringValue())
+
+ assert.Error(t, nonEmptyString.UnmarshalText([]byte("")))
+}
+
+func TestUnsetOrNonEmptyStringUnmarshalParamRejectsAnEmptyString(t *testing.T) {
+ var nonEmptyString UnsetOrNonEmptyString
+
+ require.NoError(t, nonEmptyString.UnmarshalParam("oncall"))
+ assert.Equal(t, "oncall", nonEmptyString.StringValue())
+
+ assert.Error(t, nonEmptyString.UnmarshalParam(""))
+}