Skip to content

Commit

Permalink
added Convert
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmerr committed Aug 31, 2023
1 parent ee120f9 commit 3723f6f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
__debug_bin
31 changes: 31 additions & 0 deletions measureable_units.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,34 @@ func (x ViscosityUnit) DisplayName() string {
tt := proto.GetExtension(test, E_DisplayName)
return tt.(string)
}

func (x *VolumeType) Convert(unit VolumeUnit) *VolumeType {
test := x.Unit.Descriptor().Values().ByNumber(x.Unit.Number()).Options()
tt := proto.GetExtension(test, E_ConversionVolumeUnit)
response := &VolumeType{
Unit: unit,
Value: x.Value,
}
c, ok := tt.(*ConversionVolumeUnit)
if ok {
for _, r := range c.Rates {
if r.Target == unit {
switch r.Operator {
case ArithmeticOperators_ARITHMETIC_OPERATORS_ADDITION:
response.Value = x.Value + r.Value
case ArithmeticOperators_ARITHMETIC_OPERATORS_SUBTRACTION:
response.Value = x.Value - r.Value
case ArithmeticOperators_ARITHMETIC_OPERATORS_MULTIPLICATION:
response.Value = x.Value * r.Value
case ArithmeticOperators_ARITHMETIC_OPERATORS_DIVISION:
response.Value = x.Value / r.Value
case ArithmeticOperators_ARITHMETIC_OPERATORS_UNSPECIFIED:
response.Value = x.Value
}
break
}
}
}

return response
}
22 changes: 22 additions & 0 deletions measureable_units_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package beerprotov1

import (
"fmt"
"testing"
)

func Test_main(t *testing.T) {

vol := &VolumeType{
Unit: VolumeUnit_VOLUME_UNIT_ML,
Value: 5,
}

got := vol.Convert(VolumeUnit_VOLUME_UNIT_L)

if got.Value != 0.005 {
t.Errorf("Convert(5) = %v; want 0.005", got.Value)
}
fmt.Print(vol)

}

0 comments on commit 3723f6f

Please sign in to comment.