From 8d4a60b86f112606e9e0b063838ba43dcbf42ab7 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Thu, 5 Jul 2018 12:53:05 +0000 Subject: [PATCH] implement types api --- crm/types.go | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/crm/types.go b/crm/types.go index 9b8f72b91..5fd8c7658 100644 --- a/crm/types.go +++ b/crm/types.go @@ -1,37 +1,37 @@ package crm import ( + "encoding/json" "os" "path" "path/filepath" - "encoding/json" "github.com/pkg/errors" ) var _ = errors.Wrap +func typesDecodeJSON(filename string) (map[string]interface{}, error) { + file, err := os.Open(filename) + if err != nil { + return nil, err + } + defer file.Close() + result := make(map[string]interface{}) + return result, json.NewDecoder(file).Decode(&result) +} + func (*Types) List(r *typesListRequest) (interface{}, error) { matches, err := filepath.Glob("../crm/types/*.json") if err != nil { return nil, err } - decodeFile := func(filename string) (map[string]interface{}, error) { - file, err := os.Open(filename) - if err != nil { - return nil, err - } - defer file.Close() - result := make(map[string]interface{}) - return result, json.NewDecoder(file).Decode(&result) - } - res := make([]interface{}, 0) for _, match := range matches { t := path.Base(match) t = t[:len(t)-5] - params, err := decodeFile(match) + params, err := typesDecodeJSON(match) if err != nil { return nil, errors.Wrap(err, "Error when parsing "+match) } @@ -42,5 +42,13 @@ func (*Types) List(r *typesListRequest) (interface{}, error) { } func (*Types) Type(r *typesTypeRequest) (interface{}, error) { - return nil, errors.New("Not implemented: Types.type") + if r.id == "" { + return nil, errors.New("Missing id parameter") + } + params, err := typesDecodeJSON("../crm/types/" + r.id + ".json") + if err != nil { + return nil, errors.Wrap(err, "Error reading type: "+r.id) + } + params["type"] = r.id + return params, nil }