Skip to content

Commit

Permalink
Using float64 instead of Decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
h00s committed Jan 24, 2024
1 parent 85cae2e commit aafbaa8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 79 deletions.
27 changes: 11 additions & 16 deletions api/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package helpers

import (
"math"
"strconv"

"github.com/shopspring/decimal"
)

func StrToInt(s string) int {
Expand All @@ -22,26 +21,22 @@ func StrToFloat(s string) float64 {
return f
}

func StrToDecimal(s string) decimal.Decimal {
d, err := decimal.NewFromString(s)
if err != nil {
return decimal.Zero
}
return d.Round(2)
func roundFloat(number float64) float64 {
return math.Round(number*100) / 100
}

func ConvertFahrenheitToCelsius(f float64) decimal.Decimal {
return decimal.NewFromFloat((f - 32) * 5 / 9).Round(2)
func ConvertFahrenheitToCelsius(f float64) float64 {
return roundFloat((f - 32) * 5 / 9)
}

func ConvertMileToKilometer(mph float64) decimal.Decimal {
return decimal.NewFromFloat(mph * 1.609344).Round(2)
func ConvertMileToKilometer(mph float64) float64 {
return roundFloat(mph * 1.609344)
}

func ConvertHGToKPA(hg float64) decimal.Decimal {
return decimal.NewFromFloat(hg * 33.8638866667).Round(2)
func ConvertHGToKPA(hg float64) float64 {
return roundFloat(hg * 33.8638866667)
}

func ConvertInchToMillimeter(inch float64) decimal.Decimal {
return decimal.NewFromFloat(inch * 25.4).Round(2)
func ConvertInchToMillimeter(inch float64) float64 {
return roundFloat(inch * 25.4)
}
55 changes: 27 additions & 28 deletions api/models/weather.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,44 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/h00s-go/wunderground-bridge/api/helpers"
"github.com/shopspring/decimal"
)

type Weather struct {
StationID string `json:"station_id"`
Temperature decimal.Decimal `json:"temperature"`
DewPoint decimal.Decimal `json:"dew_point"`
Humidity int `json:"humidity"`
Pressure decimal.Decimal `json:"pressure"`
Wind Wind `json:"wind"`
Rain Rain `json:"rain"`
Solar Solar `json:"solar"`
Indoor Indoor `json:"indoor"`
UpdatedAt time.Time `json:"updated_at"`
StationID string `json:"station_id"`
Temperature float64 `json:"temperature"`
DewPoint float64 `json:"dew_point"`
Humidity int `json:"humidity"`
Pressure float64 `json:"pressure"`
Wind Wind `json:"wind"`
Rain Rain `json:"rain"`
Solar Solar `json:"solar"`
Indoor Indoor `json:"indoor"`
UpdatedAt time.Time `json:"updated_at"`
}

type Wind struct {
Chill decimal.Decimal `json:"chill"`
Direction int `json:"direction"`
Speed decimal.Decimal `json:"speed"`
Gust decimal.Decimal `json:"gust"`
Chill float64 `json:"chill"`
Direction int `json:"direction"`
Speed float64 `json:"speed"`
Gust float64 `json:"gust"`
}

type Rain struct {
In decimal.Decimal `json:"in"`
InDaily decimal.Decimal `json:"in_daily"`
InWeekly decimal.Decimal `json:"in_weekly"`
InMonthly decimal.Decimal `json:"in_monthly"`
InYearly decimal.Decimal `json:"in_yearly"`
In float64 `json:"in"`
InDaily float64 `json:"in_daily"`
InWeekly float64 `json:"in_weekly"`
InMonthly float64 `json:"in_monthly"`
InYearly float64 `json:"in_yearly"`
}

type Solar struct {
Radiation decimal.Decimal `json:"radiation"`
UV int `json:"uv"`
Radiation float64 `json:"radiation"`
UV int `json:"uv"`
}

type Indoor struct {
Temperature decimal.Decimal `json:"temperature"`
Humidity int `json:"humidity"`
Temperature float64 `json:"temperature"`
Humidity int `json:"humidity"`
}

func NewWeather(ctx *fiber.Ctx) (*Weather, error) {
Expand Down Expand Up @@ -73,7 +72,7 @@ func NewWeather(ctx *fiber.Ctx) (*Weather, error) {
InYearly: helpers.ConvertInchToMillimeter(helpers.StrToFloat(ctx.Query("yearlyrainin"))),
},
Solar: Solar{
Radiation: helpers.StrToDecimal(ctx.Query("solarradiation")),
Radiation: helpers.StrToFloat(ctx.Query("solarradiation")),
UV: helpers.StrToInt(ctx.Query("UV")),
},
Indoor: Indoor{
Expand All @@ -90,16 +89,16 @@ func NewWeather(ctx *fiber.Ctx) (*Weather, error) {
}

func (w *Weather) Validate() bool {
if w.Temperature.IntPart() < -50 || w.Temperature.IntPart() > 50 {
if w.Temperature < -50 || w.Temperature > 50 {
return false
}
if w.Humidity < 0 || w.Humidity > 100 {
return false
}
if w.DewPoint.IntPart() < -50 || w.DewPoint.IntPart() > 50 {
if w.DewPoint < -50 || w.DewPoint > 50 {
return false
}
if w.Pressure.IntPart() < 800 || w.Pressure.IntPart() > 1200 {
if w.Pressure < 800 || w.Pressure > 1200 {
return false
}
return true
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/gofiber/fiber/v2 v2.52.0
github.com/gorilla/websocket v1.5.1
github.com/shopspring/decimal v1.3.1
)

require (
Expand Down
34 changes: 0 additions & 34 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,72 +1,38 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
github.com/gofiber/fiber/v2 v2.49.2 h1:ONEN3/Vc+dUCxxDgZZwpqvhISgHqb+bu+isBiEyKEQs=
github.com/gofiber/fiber/v2 v2.49.2/go.mod h1:gNsKnyrmfEWFpJxQAV0qvW6l70K1dZGno12oLtukcts=
github.com/gofiber/fiber/v2 v2.50.0 h1:ia0JaB+uw3GpNSCR5nvC5dsaxXjRU5OEu36aytx+zGw=
github.com/gofiber/fiber/v2 v2.50.0/go.mod h1:21eytvay9Is7S6z+OgPi7c7n4++tnClWmhpimVHMimw=
github.com/gofiber/fiber/v2 v2.52.0 h1:S+qXi7y+/Pgvqq4DrSmREGiFwtB7Bu6+QFLuIHYw/UE=
github.com/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g=
github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M=
github.com/valyala/fasthttp v1.50.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=
github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

0 comments on commit aafbaa8

Please sign in to comment.