From a8a23a6cb3f20a3e1813f191727bcdb9c6884548 Mon Sep 17 00:00:00 2001 From: horus_arch Date: Sun, 4 Feb 2018 20:01:00 +0100 Subject: Better Makefile. Source: https://www.complicissimus.de/golang-makefile/ --- Makefile | 30 ++++++++++++-------- sanitize_price.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utilities.go | 28 ------------------- 3 files changed, 100 insertions(+), 40 deletions(-) create mode 100644 sanitize_price.go delete mode 100644 utilities.go diff --git a/Makefile b/Makefile index db2f498..7126e13 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,20 @@ -SOURCE=alkobote.de - -all: run - -$(SOURCE): - go build - -build: $(SOURCE) - +BINARY := $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))) +MAINFILE := +SOURCEDIR := . +SOURCES := $(shell find $(SOURCEDIR) -name '*.go') + +all: build run + +build: $(BINARY) + +$(BINARY): $(SOURCES) + go build -o $(BINARY) + run: build - ./$(SOURCE) - + ./$(BINARY) + clean: - $(RM) $(SOURCE) + $(RM) $(RMFLAGS) $(BINARY) + +edit: + $(EDITOR) $(MAINFILE) *.go diff --git a/sanitize_price.go b/sanitize_price.go new file mode 100644 index 0000000..5be9e67 --- /dev/null +++ b/sanitize_price.go @@ -0,0 +1,82 @@ +package main + +import ( + "errors" + "strconv" + "strings" +) + +func sanitize_price(price string) (int, error) { + multiply_by_100 := false + + price = strings.TrimSpace(price) + + price = strings.TrimPrefix(price, "€") + price = strings.TrimSpace(price) + + price = strings.TrimSuffix(price, "€") + price = strings.TrimSpace(price) + + c := string(price[len(price)-2:]) + c = string(c[0:1]) + + /* + Extracts the second last char and checks if it's a "." or a ",". + */ + if "," == c { + if strings.Count(price, ",") > 1 { + return 0, errors.New("Invalid format") + } + + multiply_by_10 = true + + } else if "." == c { + if strings.Count(price, ".") > 1 { + return 0, errors.New("Invalid format") + } + + multiply_by_10 = true + + } + + c := string(price[len(price)-3:]) + c = string(c[0:1]) + + /* + Extracts the third last char and checks if it's a "." or a ",". + */ + if "," == c { + if strings.Count(price, ",") > 1 { + return 0, errors.New("Invalid format") + } + + multiply_by_100 = true + + } else if "." == c { + if strings.Count(price, ".") > 1 { + return 0, errors.New("Invalid format") + } + + multiply_by_100 = true + + } + + price = strings.Replace(price, ",", "", -1) + price = strings.Replace(price, ".", "", -1) + + /* + Casts the price to integer in cents (not euro!). + */ + price_int, err := strconv.Atoi(price) + if err != nil { + return 0, err + } + + if multiply_by_10 { + price_int = price_int * 10 + } else if multiply_by_100 { + price_int = price_int * 100 + } + + return price_int, nil +} diff --git a/utilities.go b/utilities.go deleted file mode 100644 index 02538fa..0000000 --- a/utilities.go +++ /dev/null @@ -1,28 +0,0 @@ -package main - -import ( - "strings" -) - -func sanitize_price(price string) int { - multiply_by_100 := false - - price = strings.TrimSpace(price) - - price = strings.TrimPrefix(price, "€") - price = strings.TrimSpace(price) - - price = strings.TrimSuffix(price, "€") - price = strings.TrimSpace(price) - - /* - Extracts the third last char and checks if it's a ",". - */ - //if ( rune(",") == []rune(price)[-3]) - c := string(price[len(price)-3:]) - c = string(c[0:1]) - - if "," == c { - multiply_by_100 = true - } -} -- cgit v1.2.3