Improve type safety with proper value objects for HabitType and Frequency
Replace generic string types with strongly-typed value objects throughout the application layer. This change ensures compile-time type checking and automatic validation during JSON deserialization. Changes: - Add JSON marshaling/unmarshaling to HabitType and Frequency value objects - Update all DTOs to use typed fields instead of strings - Update commands and queries to use proper types - Remove unnecessary string conversions - Add comprehensive JSON serialization tests - Fix existing tests to work with typed fields Benefits: - Type safety: compiler catches invalid usage - Automatic validation: invalid values rejected during JSON parsing - Better code documentation and self-explanatory APIs - Reduced runtime errors
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
package value_objects
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Frequency string
|
||||
|
||||
const (
|
||||
@@ -15,3 +20,21 @@ func (f Frequency) IsValid() bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (f Frequency) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(string(f))
|
||||
}
|
||||
|
||||
func (f *Frequency) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*f = Frequency(s)
|
||||
if !f.IsValid() {
|
||||
return fmt.Errorf("invalid frequency: %s (must be DAILY, WEEKLY, or MONTHLY)", s)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package value_objects
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFrequency_MarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
frequency Frequency
|
||||
expected string
|
||||
}{
|
||||
{"Daily", FrequencyDaily, `"DAILY"`},
|
||||
{"Weekly", FrequencyWeekly, `"WEEKLY"`},
|
||||
{"Monthly", FrequencyMonthly, `"MONTHLY"`},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
data, err := json.Marshal(tt.frequency)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal: %v", err)
|
||||
}
|
||||
if string(data) != tt.expected {
|
||||
t.Errorf("Expected %s, got %s", tt.expected, string(data))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrequency_UnmarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected Frequency
|
||||
shouldErr bool
|
||||
}{
|
||||
{"Valid Daily", `"DAILY"`, FrequencyDaily, false},
|
||||
{"Valid Weekly", `"WEEKLY"`, FrequencyWeekly, false},
|
||||
{"Valid Monthly", `"MONTHLY"`, FrequencyMonthly, false},
|
||||
{"Invalid frequency", `"YEARLY"`, "", true},
|
||||
{"Empty string", `""`, "", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var f Frequency
|
||||
err := json.Unmarshal([]byte(tt.input), &f)
|
||||
|
||||
if tt.shouldErr {
|
||||
if err == nil {
|
||||
t.Error("Expected error but got none")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if f != tt.expected {
|
||||
t.Errorf("Expected %s, got %s", tt.expected, f)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrequency_JSONRoundTrip(t *testing.T) {
|
||||
type testStruct struct {
|
||||
Frequency Frequency `json:"frequency"`
|
||||
}
|
||||
|
||||
original := testStruct{Frequency: FrequencyWeekly}
|
||||
|
||||
data, err := json.Marshal(original)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal failed: %v", err)
|
||||
}
|
||||
|
||||
var decoded testStruct
|
||||
err = json.Unmarshal(data, &decoded)
|
||||
if err != nil {
|
||||
t.Fatalf("Unmarshal failed: %v", err)
|
||||
}
|
||||
|
||||
if decoded.Frequency != original.Frequency {
|
||||
t.Errorf("Round trip failed: expected %s, got %s", original.Frequency, decoded.Frequency)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,10 @@
|
||||
package value_objects
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type HabitType string
|
||||
|
||||
const (
|
||||
@@ -15,3 +20,21 @@ func (ht HabitType) IsValid() bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (ht HabitType) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(string(ht))
|
||||
}
|
||||
|
||||
func (ht *HabitType) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*ht = HabitType(s)
|
||||
if !ht.IsValid() {
|
||||
return fmt.Errorf("invalid habit type: %s (must be BOOLEAN, COUNTER, or VALUE)", s)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package value_objects
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHabitType_MarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
habitType HabitType
|
||||
expected string
|
||||
}{
|
||||
{"Boolean", HabitTypeBoolean, `"BOOLEAN"`},
|
||||
{"Counter", HabitTypeCounter, `"COUNTER"`},
|
||||
{"Value", HabitTypeValue, `"VALUE"`},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
data, err := json.Marshal(tt.habitType)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal: %v", err)
|
||||
}
|
||||
if string(data) != tt.expected {
|
||||
t.Errorf("Expected %s, got %s", tt.expected, string(data))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHabitType_UnmarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected HabitType
|
||||
shouldErr bool
|
||||
}{
|
||||
{"Valid Boolean", `"BOOLEAN"`, HabitTypeBoolean, false},
|
||||
{"Valid Counter", `"COUNTER"`, HabitTypeCounter, false},
|
||||
{"Valid Value", `"VALUE"`, HabitTypeValue, false},
|
||||
{"Invalid type", `"INVALID"`, "", true},
|
||||
{"Empty string", `""`, "", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var ht HabitType
|
||||
err := json.Unmarshal([]byte(tt.input), &ht)
|
||||
|
||||
if tt.shouldErr {
|
||||
if err == nil {
|
||||
t.Error("Expected error but got none")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if ht != tt.expected {
|
||||
t.Errorf("Expected %s, got %s", tt.expected, ht)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHabitType_JSONRoundTrip(t *testing.T) {
|
||||
type testStruct struct {
|
||||
Type HabitType `json:"type"`
|
||||
}
|
||||
|
||||
original := testStruct{Type: HabitTypeCounter}
|
||||
|
||||
data, err := json.Marshal(original)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal failed: %v", err)
|
||||
}
|
||||
|
||||
var decoded testStruct
|
||||
err = json.Unmarshal(data, &decoded)
|
||||
if err != nil {
|
||||
t.Fatalf("Unmarshal failed: %v", err)
|
||||
}
|
||||
|
||||
if decoded.Type != original.Type {
|
||||
t.Errorf("Round trip failed: expected %s, got %s", original.Type, decoded.Type)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user