From a242a08aaa542ea181e38de20658a067b3e60e1a Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Mon, 8 Jan 2024 19:39:38 -0330 Subject: refactor compressor parsing --- Makefile | 12 +++--------- compressor/compressor.go | 49 +++++++++++++++++++++--------------------------- 2 files changed, 24 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index d92c5fa..a3bf008 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,4 @@ -build: tidy format - go build -run: tidy format - go run . - -format: - gofmt -s -w . - -tidy: +all: go mod tidy + go build + gofmt -l -s -w . diff --git a/compressor/compressor.go b/compressor/compressor.go index efe792e..c03a8d5 100644 --- a/compressor/compressor.go +++ b/compressor/compressor.go @@ -4,6 +4,7 @@ import ( "github.com/BurntSushi/toml" "io/fs" fp "path/filepath" + "strings" "github.com/sam-anthony/volute/mass" "github.com/sam-anthony/volute/util" @@ -56,47 +57,24 @@ func init() { man, ser := fp.Split(manSer) man = fp.Clean(man) // Clean trailing slash - var exists bool - _, exists = compressors[man] - if !exists { + if _, ok := compressors[man]; !ok { // Manufacturer does NOT exist compressors[man] = make(map[string]map[string]Compressor) } - _, exists = compressors[man][ser] - if !exists { + if _, ok := compressors[man][ser]; !ok { // Series does NOT exist compressors[man][ser] = make(map[string]Compressor) } tomlFile := fp.Join(root, path) - var c Compressor - _, err = toml.DecodeFile(tomlFile, &c) - if err != nil { + if _, err = toml.DecodeFile(tomlFile, &c); err != nil { return err } - - // Replace .toml with .jpg - imageFile := tomlFile[:len(tomlFile)-len(".toml")] + ".jpg" - c.FileName = imageFile - - // Must parse MaxFlow seperately because the MassFlowRateUnit - // is stored as a string and must be converted with - // FlowRateUnitFromString(). - flow := struct { - FlowVal float32 - FlowUnit string - }{} - _, err = toml.DecodeFile(tomlFile, &flow) + c.FileName = strings.TrimSuffix(tomlFile, ".toml") + ".jpg" + c.MaxFlow, err = readMaxFlow(tomlFile) if err != nil { return err } - u, err := mass.FlowRateUnitFromString(flow.FlowUnit) - if err != nil { - return err - } - c.MaxFlow = mass.FlowRate(flow.FlowVal) * u - compressors[man][ser][mod] = c - return nil }) util.Check(err) @@ -105,3 +83,18 @@ func init() { func Compressors() map[string]map[string]map[string]Compressor { return compressors } + +func readMaxFlow(tomlFile string) (mass.FlowRate, error) { + flow := struct { + FlowVal float32 + FlowUnit string + }{} + if _, err := toml.DecodeFile(tomlFile, &flow); err != nil { + return -1, err + } + unit, err := mass.FlowRateUnitFromString(flow.FlowUnit) + if err != nil { + return -1, err + } + return mass.FlowRate(flow.FlowVal) * unit, nil +} -- cgit v1.2.3