From cc6e58acd90ebe1fde1e583aba7fe082e18ffdc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Schr=C3=B6pfer?= Date: Sat, 27 Feb 2021 20:09:29 +0100 Subject: [PATCH] basic conf for loader --- .gitignore | 1 + cmd/test2/conf.json | 43 ++++++++++ cmd/test2/main.go | 13 +++ conf/default.json | 198 ++++++++++++++++++++++++++++++++++++++++++++ pkg/loader/conf.go | 70 ++++++++++++++++ pkg/set/set.go | 20 +++++ pkg/set/set_test.go | 30 +++++++ 7 files changed, 375 insertions(+) create mode 100644 cmd/test2/conf.json create mode 100644 conf/default.json create mode 100644 pkg/loader/conf.go create mode 100644 pkg/set/set.go create mode 100644 pkg/set/set_test.go diff --git a/.gitignore b/.gitignore index f4d432a..8af6ec7 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ +test/.~lock* diff --git a/cmd/test2/conf.json b/cmd/test2/conf.json new file mode 100644 index 0000000..e1c5d58 --- /dev/null +++ b/cmd/test2/conf.json @@ -0,0 +1,43 @@ +{ + "title": "", + "version": "", + "values": [ + { + "sheet": 0, + "cell": "", + "type": "", + "struct": "", + "field": "", + "default": "" + } + ], + "areas": [ + { + "sheet": 0, + "start_row": 0, + "end_row": 0, + "struct": "", + "cols": [ + { + "col": "", + "field": "", + "type": "", + "default": "" + } + ] + } + ], + "rating": { + "sheet": 0, + "start_row": 0, + "end_row": 0, + "points_col": "", + "max_points_col": "", + "id_col": "", + "short_name_col": "", + "name_col": "", + "estimations_col": "", + "weight_col": "", + "selected_by_user_col": "" + } +} diff --git a/cmd/test2/main.go b/cmd/test2/main.go index 2d664bf..957052f 100644 --- a/cmd/test2/main.go +++ b/cmd/test2/main.go @@ -2,7 +2,9 @@ package main import ( "fmt" + "os" + "git.ecogood.org/andreas.schroepfer/excelConverter/pkg/loader" excelize "github.com/360EntSecGroup-Skylar/excelize/v2" ) @@ -12,4 +14,15 @@ func main() { sheets := xFile.GetSheetMap() rows, _ := xFile.GetRows(sheets[4]) fmt.Printf("%#v", rows) + c := loader.Conf{} + c.Values = append(c.Values, loader.Value{}) + c.Areas = append(c.Areas, loader.Area{ + Cols: []loader.AreaCol{ + loader.AreaCol{}, + }, + }) + fd, err := os.OpenFile("conf.json", os.O_CREATE, 0777) + fmt.Println(err) + defer fd.Close() + c.EncodeJSON(fd) } diff --git a/conf/default.json b/conf/default.json new file mode 100644 index 0000000..275b2dd --- /dev/null +++ b/conf/default.json @@ -0,0 +1,198 @@ +{ + "title": "Good Balance Calculator", + "version": "5.04", + "values": [ + { + "sheet": 3, + "cell": "C7", + "type": "int", + "struct": "CompanyFacts", + "field": "TotalPurchaseFromSuppliers", + "default": "" + }, + { + "sheet": 3, + "cell": "C27", + "type": "int", + "struct": "CompanyFacts", + "field": "TotalStaffCosts", + "default": "" + }, + { + "sheet": 3, + "cell": "C18", + "type": "int", + "struct": "CompanyFacts", + "field": "Profit", + "default": "" + }, + { + "sheet": 3, + "cell": "C19", + "type": "int", + "struct": "CompanyFacts", + "field": "FinancialCosts", + "default": "" + }, + { + "sheet": 3, + "cell": "C20", + "type": "int", + "struct": "CompanyFacts", + "field": "IncomeFromFinancialInvestments", + "default": "" + }, + { + "sheet": 3, + "cell": "C22", + "type": "int", + "struct": "CompanyFacts", + "field": "AdditionsToFixedAssets", + "default": "" + }, + { + "sheet": 3, + "cell": "C37", + "type": "int", + "struct": "CompanyFacts", + "field": "Turnover", + "default": "" + }, + { + "sheet": 3, + "cell": "C21", + "type": "int", + "struct": "CompanyFacts", + "field": "TotalAssets", + "default": "" + }, + { + "sheet": 3, + "cell": "C23", + "type": "int", + "struct": "CompanyFacts", + "field": "FinancialAssetsAndCashBalance", + "default": "" + }, + { + "sheet": 3, + "cell": "C26", + "type": "int", + "struct": "CompanyFacts", + "field": "NumberOfEmployees", + "default": "" + }, + { + "sheet": 3, + "cell": "C24", + "type": "bool", + "struct": "CompanyFacts", + "field": "HasCanteen", + "default": "" + }, + { + "sheet": 3, + "cell": "C38", + "type": "bool", + "struct": "CompanyFacts", + "field": "IsB2B", + "default": "" + }, + { + "sheet": 3, + "cell": "C33", + "type": "int", + "struct": "CompanyFacts", + "field": "AverageJourneyToWorkForStaffInKm", + "default": "" + } + ], + "areas": [ + { + "sheet": "3", + "start_row": 10, + "end_row": 14, + "struct": "SupplyFraction", + "cols": [ + { + "col": "B", + "field": "IndustryCode", + "type": "string", + "default": "" + }, + { + "col": "D", + "field": "CountryCode", + "type": "string", + "default": "" + }, + { + "col": "E", + "field": "Costs", + "type": "string", + "default": "" + } + ] + }, + { + "sheet": "3", + "start_row": 30, + "end_row": 32, + "struct": "EmployeesFraction", + "cols": [ + { + "col": "B", + "field": "CountryCode", + "type": "string", + "default": "" + }, + { + "col": "D", + "field": "Percentage", + "type": "float32", + "default": "" + } + ] + }, + { + "sheet": "3", + "start_row": 41, + "end_row": 43, + "struct": "IndustrySector", + "cols": [ + { + "col": "B", + "field": "IndustryCode", + "type": "string", + "default": "" + }, + { + "col": "C", + "field": "Description", + "type": "string", + "default": "" + }, + { + "col": "D", + "field": "AmountOfTotalTurnover", + "type": "int", + "default": "" + } + ] + } + ], + "rating": { + "sheet": 4, + "start_row": 9, + "end_row": 93, + "points_col": "I", + "max_points_col": "J", + "id_col": "", + "short_name_col": "B", + "name_col": "C", + "estimations_col": "H", + "weight_col": "D", + "selected_by_user_col": "N" + } + } + \ No newline at end of file diff --git a/pkg/loader/conf.go b/pkg/loader/conf.go new file mode 100644 index 0000000..e39cf1c --- /dev/null +++ b/pkg/loader/conf.go @@ -0,0 +1,70 @@ +package loader + +import ( + "encoding/json" + "io" +) + +// Conf configures the mapping from the excel version +// to the eCalc structure +type Conf struct { + Title string `json:"title"` + Version string `json:"version"` + Values []Value `json:"values"` + Areas []Area `json:"areas"` + Rating Rating `json:"rating"` +} + +// Value defines a single value inside the excel workbook +type Value struct { + Sheet int `json:"sheet"` + Cell string `json:"cell"` + Type string `json:"type"` + Struct string `json:"struct"` + Field string `json:"field"` + Default string `json:"default"` +} + +// Area defines rows and cols, which are maped to +// a slice of a type. For example IndustrySector +type Area struct { + Sheet int `json:"sheet"` + StartRow int `json:"start_row"` + EndRow int `json:"end_row"` + Struct string `json:"struct"` + Cols []AreaCol `json:"cols"` +} + +// AreaCol defines each col of an area. As Col-Definition +// the letter from the Excel should be used. +// For example "A" for the first col, "B" for the second ... +type AreaCol struct { + Col string `json:"col"` + Field string `json:"field"` + Type string `json:"type"` + Default string `json:"default"` +} + +// Rating defines the area of the ratingarea +// The cols must be defined by the letter of the col +// for example "A" for the first col +type Rating struct { + Sheet int `json:"sheet"` + StartRow int `json:"start_row"` + EndRow int `json:"end_row"` + PointsCol string `json:"points_col"` + MaxPointsCol string `json:"max_points_col"` + IDCol string `json:"id_col"` + ShortNameCol string `json:"short_name_col"` + NameCol string `json:"name_col"` + EstimationsCol string `json:"estimations_col"` + WeightCol string `json:"weight_col"` + SelectedByUserCol string `json:"selected_by_user_col"` +} + +// EncodeJSON writes the JSON of the conf into the Writer +func (c Conf) EncodeJSON(w io.Writer) error { + enc := json.NewEncoder(w) + enc.SetIndent("", " ") + return enc.Encode(c) +} diff --git a/pkg/set/set.go b/pkg/set/set.go new file mode 100644 index 0000000..36b42b0 --- /dev/null +++ b/pkg/set/set.go @@ -0,0 +1,20 @@ +package set + +import ( + "errors" + "reflect" +) + +func Field(dst interface{}, fieldName string, value interface{}) error { + + valDst := reflect.ValueOf(dst).Elem() + dstField := valDst.FieldByName(fieldName) + + val := reflect.ValueOf(value) + if dstField.Kind() != val.Kind() { + return errors.New("value-Type does not match to field") + } + + dstField.Set(val) + return nil +} diff --git a/pkg/set/set_test.go b/pkg/set/set_test.go new file mode 100644 index 0000000..bd492a6 --- /dev/null +++ b/pkg/set/set_test.go @@ -0,0 +1,30 @@ +package set + +import "testing" + +func TestField(t *testing.T) { + input := struct { + MyString string + MyInt int + MyBool bool + }{} + value := "abc" + Field(&input, "MyString", value) + if input.MyString != value { + t.Errorf("got: %s; want: %s", input.MyString, value) + } + vInt := 2 + Field(&input, "MyInt", vInt) + if input.MyInt != vInt { + t.Errorf("MyInt: %d, want: %d", input.MyInt, vInt) + } + err := Field(&input, "MyInt", "vInt") + if err == nil { + t.Error("expect error, when wrong value-Type") + } + err = Field(&input, "NoField", "jjj") + if err == nil { + t.Error("expect error, when field not exists") + } + +}