diff --git a/types/pseudo/int-bool.go b/types/pseudo/int-bool-json.go similarity index 67% rename from types/pseudo/int-bool.go rename to types/pseudo/int-bool-json.go index c0170cd..f1b475f 100644 --- a/types/pseudo/int-bool.go +++ b/types/pseudo/int-bool-json.go @@ -2,14 +2,14 @@ package pseudo import "encoding/json" -// IntBool is a custom type that represents a boolean value as an integer. +// IntBoolJSON is a custom type that represents a boolean value as an integer. // It is used for JSON marshaling and unmarshaling purposes. -type IntBool bool +type IntBoolJSON bool -// MarshalJSON is a method that serializes the IntBool value to JSON format. +// MarshalJSON is a method that serializes the IntBoolJSON value to JSON format. // It converts the boolean value to an integer (1 or 0), then marshals the integer to JSON. // The method returns the marshaled JSON bytes and an error, if any. -func (b IntBool) MarshalJSON() ([]byte, error) { +func (b IntBoolJSON) MarshalJSON() ([]byte, error) { var v int if b { v = 1 @@ -20,12 +20,12 @@ func (b IntBool) MarshalJSON() ([]byte, error) { return json.Marshal(v) } -// UnmarshalJSON is a method that deserializes the JSON data into the IntBool value. -// It unmarshals the received JSON data into an integer value, then assigns the IntBool -// value based on whether the integer is non-zero or zero. The IntBool value is set to +// UnmarshalJSON is a method that deserializes the JSON data into the IntBoolJSON value. +// It unmarshals the received JSON data into an integer value, then assigns the IntBoolJSON +// value based on whether the integer is non-zero or zero. The IntBoolJSON value is set to // true if the integer is non-zero, and false if the integer is zero. // The method returns an error if the unmarshaling process fails. -func (b *IntBool) UnmarshalJSON(data []byte) error { +func (b *IntBoolJSON) UnmarshalJSON(data []byte) error { var v int err := json.Unmarshal(data, &v) if err != nil { diff --git a/types/pseudo/int-bool-json_test.go b/types/pseudo/int-bool-json_test.go new file mode 100644 index 0000000..9e82c30 --- /dev/null +++ b/types/pseudo/int-bool-json_test.go @@ -0,0 +1,60 @@ +package pseudo + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestIntBoolJSON_MarshalJSON(t *testing.T) { + type TestCase struct { + name string + i IntBoolJSON + want string + wantErr bool + } + + cases := []TestCase{ + {name: "true", i: IntBoolJSON(true), want: "1", wantErr: false}, + {name: "false", i: IntBoolJSON(false), want: "0", wantErr: false}, + {name: "native true", i: true, want: "1", wantErr: false}, + {name: "native false", i: false, want: "0", wantErr: false}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + got, err := c.i.MarshalJSON() + assert.NoError(t, err, "it should not error") + assert.Equal(t, got, []byte(c.want)) + }) + } +} + +func TestIntBoolJSON_UnmarshalJSON(t *testing.T) { + type TestCase struct { + name string + json string + want IntBoolJSON + wantErr bool + } + + cases := []TestCase{ + {name: "number 1", json: "1", want: true, wantErr: false}, + {name: "number 0", json: "0", want: false, wantErr: false}, + {name: "non-number", json: `"foo"`, want: false, wantErr: true}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + var i IntBoolJSON + err := i.UnmarshalJSON([]byte(c.json)) + + if c.wantErr { + assert.Error(t, err, "it should error", c.json) + } else { + assert.NoError(t, err, "it should not error", c.json) + assert.Equal(t, c.want, i) + } + + }) + } +} diff --git a/types/pseudo/int-bool_test.go b/types/pseudo/int-bool_test.go deleted file mode 100644 index 7077303..0000000 --- a/types/pseudo/int-bool_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package pseudo - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestIntBool_MarshalJSON(t *testing.T) { - type TestCase struct { - name string - ib IntBool - want string - wantErr bool - } - - tests := []TestCase{ - {name: "true", ib: true, want: "1", wantErr: false}, - {name: "false", ib: false, want: "0", wantErr: false}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := tt.ib.MarshalJSON() - assert.NoError(t, err, "it should not error") - assert.Equal(t, got, []byte(tt.want)) - }) - } -} - -func TestIntBool_UnmarshalJSON(t *testing.T) { - type TestCase struct { - name string - json string - want IntBool - wantErr bool - } - - tests := []TestCase{ - {name: "number 1", json: "1", want: true, wantErr: false}, - {name: "number 0", json: "0", want: false, wantErr: false}, - {name: "non-number", json: `"foo"`, want: false, wantErr: true}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - var i IntBool - err := i.UnmarshalJSON([]byte(tt.json)) - - if tt.wantErr { - assert.Error(t, err, "it should error", tt.json) - } else { - assert.NoError(t, err, "it should not error", tt.json) - assert.Equal(t, tt.want, i) - } - - }) - } -} diff --git a/types/pseudo/message-struct.go b/types/pseudo/message-override-json.go similarity index 60% rename from types/pseudo/message-struct.go rename to types/pseudo/message-override-json.go index b838533..94f1064 100644 --- a/types/pseudo/message-struct.go +++ b/types/pseudo/message-override-json.go @@ -6,30 +6,30 @@ import ( "strings" ) -// MessageStruct represents a generic message structure that may be overridden by a string in the JSON representation. -type MessageStruct[T any] struct { +// MessageOverrideJSON represents a generic message structure that may be overridden by a string in the JSON representation. +type MessageOverrideJSON[T any] struct { Value *T Message string } -// MarshalJSON serializes the MessageStruct type to JSON representation. -// If MessageStruct has a non-empty Message or HasMessage is true, +// MarshalJSON serializes the MessageOverrideJSON type to JSON representation. +// If MessageOverrideJSON has a non-empty Message or HasMessage is true, // it marshals only the Message field as a string. Otherwise, it marshals the Value field. // It returns a byte slice and an error. -func (ms MessageStruct[T]) MarshalJSON() ([]byte, error) { - if strings.TrimSpace(ms.Message) != "" { - return json.Marshal(ms.Message) +func (m MessageOverrideJSON[T]) MarshalJSON() ([]byte, error) { + if strings.TrimSpace(m.Message) != "" { + return json.Marshal(m.Message) } - return json.Marshal(ms.Value) + return json.Marshal(m.Value) } -// UnmarshalJSON deserializes JSON data into a MessageStruct pointer. +// UnmarshalJSON deserializes JSON data into a MessageOverrideJSON pointer. // If the JSON is a string, it assigns the string to the Message field. // If the JSON is T, it assigns the value to the Value field. // In case the JSON data cannot be unmarshaled into a string or T, it returns an error. -func (ms *MessageStruct[T]) UnmarshalJSON(data []byte) error { - err := UnmarshalMessageOrStruct(data, &ms.Message, &ms.Value) +func (m *MessageOverrideJSON[T]) UnmarshalJSON(data []byte) error { + err := UnmarshalMessageOrStruct(data, &m.Message, &m.Value) if err != nil { return err } diff --git a/types/pseudo/message-override-json_test.go b/types/pseudo/message-override-json_test.go new file mode 100644 index 0000000..4c70fe9 --- /dev/null +++ b/types/pseudo/message-override-json_test.go @@ -0,0 +1,62 @@ +package pseudo + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestMessageOverrideJSON_MarshalJSON(t *testing.T) { + type TestCase struct { + name string + m MessageOverrideJSON[int] + want string + } + + value := 5 + + cases := []TestCase{ + {name: "has Value has Message", m: MessageOverrideJSON[int]{Value: &value, Message: "Test"}, want: `"Test"`}, + {name: "has Value no Message", m: MessageOverrideJSON[int]{Value: &value, Message: ""}, want: `5`}, + {name: "no Value and Message", m: MessageOverrideJSON[int]{Value: nil, Message: ""}, want: `null`}, + {name: "no Value has Message", m: MessageOverrideJSON[int]{Value: nil, Message: "Test"}, want: `"Test"`}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + got, err := c.m.MarshalJSON() + assert.NoError(t, err, "it should not error") + assert.Equal(t, []byte(c.want), got) + }) + } +} + +func TestMessageOverrideJSON_UnmarshalJSON(t *testing.T) { + type TestCase struct { + name string + json string + want MessageOverrideJSON[int] + wantErr bool + } + + value := 5 + + cases := []TestCase{ + {name: "string message", json: `"here is a message"`, want: MessageOverrideJSON[int]{Message: "here is a message"}, wantErr: false}, + {name: "expected value", json: `5`, want: MessageOverrideJSON[int]{Value: &value, Message: ""}, wantErr: false}, + {name: "unexpected value", json: `{"not an int": true}`, wantErr: true}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + var got MessageOverrideJSON[int] + err := got.UnmarshalJSON([]byte(c.json)) + + if c.wantErr { + assert.Error(t, err, "it should return an error") + } else { + assert.NoError(t, err, "it should not return an error") + assert.Equal(t, got, c.want) + } + }) + } +} diff --git a/types/pseudo/message-struct_test.go b/types/pseudo/message-struct_test.go deleted file mode 100644 index b2222f0..0000000 --- a/types/pseudo/message-struct_test.go +++ /dev/null @@ -1,62 +0,0 @@ -package pseudo - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestMessageStruct_MarshalJSON(t *testing.T) { - type TestCase struct { - name string - ms MessageStruct[int] - want string - } - - value := 5 - - tests := []TestCase{ - {name: "has Value has Message", ms: MessageStruct[int]{Value: &value, Message: "Test"}, want: `"Test"`}, - {name: "has Value no Message", ms: MessageStruct[int]{Value: &value, Message: ""}, want: `5`}, - {name: "no Value and Message", ms: MessageStruct[int]{Value: nil, Message: ""}, want: `null`}, - {name: "no Value has Message", ms: MessageStruct[int]{Value: nil, Message: "Test"}, want: `"Test"`}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := tt.ms.MarshalJSON() - assert.NoError(t, err, "it should not error") - assert.Equal(t, []byte(tt.want), got) - }) - } -} - -func TestMessageStruct_UnmarshalJSON(t *testing.T) { - type TestCase struct { - name string - json string - want MessageStruct[int] - wantErr bool - } - - value := 5 - - tests := []TestCase{ - {name: "string message", json: `"here is a message"`, want: MessageStruct[int]{Message: "here is a message"}, wantErr: false}, - {name: "expected value", json: `5`, want: MessageStruct[int]{Value: &value, Message: ""}, wantErr: false}, - {name: "unexpected value", json: `{"not an int": true}`, wantErr: true}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - var got MessageStruct[int] - err := got.UnmarshalJSON([]byte(tt.json)) - - if tt.wantErr { - assert.Error(t, err, "it should return an error") - } else { - assert.NoError(t, err, "it should not return an error") - assert.Equal(t, got, tt.want) - } - }) - } -}