basic conf for loader
This commit is contained in:
70
pkg/loader/conf.go
Normal file
70
pkg/loader/conf.go
Normal 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
20
pkg/set/set.go
Normal 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
30
pkg/set/set_test.go
Normal 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")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user