Skip to content

Commit

Permalink
types/pseudo: normalize names
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlettman committed Jul 16, 2024
1 parent 6498e29 commit c0f6cc3
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 139 deletions.
16 changes: 8 additions & 8 deletions types/pseudo/int-bool.go → types/pseudo/int-bool-json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
60 changes: 60 additions & 0 deletions types/pseudo/int-bool-json_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

})
}
}
58 changes: 0 additions & 58 deletions types/pseudo/int-bool_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
62 changes: 62 additions & 0 deletions types/pseudo/message-override-json_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
62 changes: 0 additions & 62 deletions types/pseudo/message-struct_test.go

This file was deleted.

0 comments on commit c0f6cc3

Please sign in to comment.