Skip to content

Commit

Permalink
fix: Change to using unit types as an enum from str
Browse files Browse the repository at this point in the history
  • Loading branch information
deregtd committed Jul 24, 2024
1 parent 1b15ce9 commit 159975a
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 24 deletions.
8 changes: 4 additions & 4 deletions pkg/units/distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ type Distance Unit[DistanceUnit]
// MarshalJSON is a custom marshaler for the unit type to add the UnitType string
func (u Distance) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType string `json:"unitType"`
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType UnitType `json:"unitType"`
}{
Value: u.Value,
Unit: int(u.Unit),
UnitType: "dist",
UnitType: UnitTypeDistance,
})
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/units/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type Flow Unit[FlowUnit]
// MarshalJSON is a custom marshaler for the unit type to add the UnitType string
func (u Flow) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType string `json:"unitType"`
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType UnitType `json:"unitType"`
}{
Value: u.Value,
Unit: int(u.Unit),
UnitType: "flow",
UnitType: UnitTypeFlow,
})
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/units/pressure.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type Pressure Unit[PressureUnit]
// MarshalJSON is a custom marshaler for the unit type to add the UnitType string
func (u Pressure) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType string `json:"unitType"`
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType UnitType `json:"unitType"`
}{
Value: u.Value,
Unit: int(u.Unit),
UnitType: "pres",
UnitType: UnitTypePressure,
})
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/units/temperature.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ type Temperature Unit[TemperatureUnit]
// MarshalJSON is a custom marshaler for the unit type to add the UnitType string
func (u Temperature) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType string `json:"unitType"`
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType UnitType `json:"unitType"`
}{
Value: u.Value,
Unit: int(u.Unit),
UnitType: "temp",
UnitType: UnitTypeTemperature,
})
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/units/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
// units transferred from boatweb.
package units

// UnitType is an enum for all unit types
// ENUM(Distance,Flow,Pressure,Temperature,Velocity,Volume)
//
//go:generate go run github.com/abice/go-enum@latest --values
type UnitType int

// Unit is a base type for all other unit structs
type Unit[T ~int] struct {
Value float32
Expand Down
84 changes: 84 additions & 0 deletions pkg/units/units_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/units/velocity.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ type Velocity Unit[VelocityUnit]
// MarshalJSON is a custom marshaler for the unit type to add the UnitType string
func (u Velocity) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType string `json:"unitType"`
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType UnitType `json:"unitType"`
}{
Value: u.Value,
Unit: int(u.Unit),
UnitType: "spd",
UnitType: UnitTypeVelocity,
})
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/units/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ type Volume Unit[VolumeUnit]
// MarshalJSON is a custom marshaler for the unit type to add the UnitType string
func (u Volume) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType string `json:"unitType"`
Value float32 `json:"value"`
Unit int `json:"unit"`
UnitType UnitType `json:"unitType"`
}{
Value: u.Value,
Unit: int(u.Unit),
UnitType: "vol",
UnitType: UnitTypeVolume,
})
}

Expand Down

0 comments on commit 159975a

Please sign in to comment.