ca2533d4df
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
41 lines
711 B
Go
41 lines
711 B
Go
package value_objects
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
type Frequency string
|
|
|
|
const (
|
|
FrequencyDaily Frequency = "DAILY"
|
|
FrequencyWeekly Frequency = "WEEKLY"
|
|
FrequencyMonthly Frequency = "MONTHLY"
|
|
)
|
|
|
|
func (f Frequency) IsValid() bool {
|
|
switch f {
|
|
case FrequencyDaily, FrequencyWeekly, FrequencyMonthly:
|
|
return true
|
|
}
|
|
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
|
|
}
|