basic conf for loader

This commit is contained in:
Andreas Schröpfer
2021-02-27 20:09:29 +01:00
parent ac9b441283
commit cc6e58acd9
7 changed files with 375 additions and 0 deletions

1
.gitignore vendored
View File

@@ -15,3 +15,4 @@
# Dependency directories (remove the comment below to include it)
# vendor/
test/.~lock*

43
cmd/test2/conf.json Normal file
View File

@@ -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": ""
}
}

View File

@@ -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)
}

198
conf/default.json Normal file
View File

@@ -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"
}
}

70
pkg/loader/conf.go Normal file
View File

@@ -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)
}

20
pkg/set/set.go Normal file
View File

@@ -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
}

30
pkg/set/set_test.go Normal file
View File

@@ -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")
}
}