Skip to content

Commit

Permalink
feat: Use go-enum for generating unit enums to support reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
deregtd committed Jul 13, 2024
1 parent 203d0d4 commit 887b4be
Show file tree
Hide file tree
Showing 13 changed files with 558 additions and 66 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ test:
lint:
@golangci-lint run
@go run github.com/getoutreach/lintroller/cmd/[email protected] -config lintroller.yaml ./...

.PHONY: generate
generate:
@go generate ./...
22 changes: 8 additions & 14 deletions pkg/units/distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@ package units
import "encoding/json"

// DistanceUnit is an enum for all distance unit types
// ENUM(Meter, Foot, Mile, NauticalMile, Fathom)
// Meter is a meter
// Foot is a foot
// Mile is a mile
// NauticalMile is a nautical mile
// Fathom is a fathom
//
//go:generate go run github.com/abice/go-enum@latest --marshal --noprefix --values
type DistanceUnit int

// DistanceUnits
const (
// Meter is a meter
Meter DistanceUnit = 0
// Foot is a foot
Foot DistanceUnit = 1
// Mile is a mile
Mile DistanceUnit = 2
// NauticalMile is a nautical mile
NauticalMile DistanceUnit = 3
// Fathom is a fathom
Fathom DistanceUnit = 4
)

// distanceConversions is a helper for doing unit conversions on distance units
var distanceConversions = map[DistanceUnit]float32{
Meter: 1609.34,
Expand Down
95 changes: 95 additions & 0 deletions pkg/units/distance_enum.go

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

16 changes: 6 additions & 10 deletions pkg/units/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ package units
import "encoding/json"

// FlowUnit is an enum for all flow unit types
// ENUM(LitersPerHour, GallonsPerMinute, GallonsPerHour)
// LitersPerHour is L/hr
// GallonsPerMinute is Gal/min
// GallonsPerHour is Gal/hr
//
//go:generate go run github.com/abice/go-enum@latest --marshal --noprefix --values
type FlowUnit int

// The FlowUnits
const (
// LitersPerHour is L/hr
LitersPerHour FlowUnit = iota
// GallonsPerMinute is Gal/min
GallonsPerMinute
// GallonsPerHour is Gal/hr
GallonsPerHour
)

// flowConversions is a helper for doing unit conversions on FlowUnits
var flowConversions = map[FlowUnit]float32{
LitersPerHour: 1,
Expand Down
85 changes: 85 additions & 0 deletions pkg/units/flow_enum.go

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

16 changes: 6 additions & 10 deletions pkg/units/pressure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ package units
import "encoding/json"

// PressureUnit is an enum for all pressure unit types
// ENUM(Psi, Hpa, Pa)
// Psi is PSI (pounds per square inch)
// Hpa is HectoPascals (100 Pascals)
// Pa is Pascals
//
//go:generate go run github.com/abice/go-enum@latest --marshal --noprefix --values
type PressureUnit int

// The different PressureUnits
const (
// Psi is PSI (pounds per square inch)
Psi PressureUnit = 0
// Hpa is HectoPascals (100 Pascals)
Hpa PressureUnit = 1
// Pa is Pascals
Pa PressureUnit = 2
)

// pressureConversions is a helper for doing unit conversions on PressureUnits
var pressureConversions = map[PressureUnit]float32{
Psi: 0.0145038,
Expand Down
85 changes: 85 additions & 0 deletions pkg/units/pressure_enum.go

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

13 changes: 3 additions & 10 deletions pkg/units/temperature.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ import (
)

// TemperatureUnit is an enum for all temperature unit types
// ENUM(Kelvin, Farenheit, Celsius)
//
//go:generate go run github.com/abice/go-enum@latest --marshal --noprefix --values
type TemperatureUnit int

// The different TemperatureUnits
const (
// Kelvin is Kelvin
Kelvin TemperatureUnit = iota
// Farenheit is Farenheit
Farenheit TemperatureUnit = iota
// Celsius is Celsius
Celsius TemperatureUnit = iota
)

// Temperature is a generic Unit structure that represents temperatures
type Temperature Unit[TemperatureUnit]

Expand Down
Loading

0 comments on commit 887b4be

Please sign in to comment.