From 1aa64072b1ed5af83c86ee18f9adfd2a58981dfb Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 19 May 2018 15:17:21 +0200 Subject: [PATCH] JSON config reader | error types --- config.go | 33 +++++++++++++++++++++++++++++++++ errors.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ types.go | 27 +++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 config.go create mode 100644 errors.go create mode 100644 types.go diff --git a/config.go b/config.go new file mode 100644 index 0000000..50414d4 --- /dev/null +++ b/config.go @@ -0,0 +1,33 @@ +package gfw + +import ( + "encoding/json" + "os" +) + +// Load builds a struct representation of the +// configuration file located at @path +// The structure checks for most format errors +func Load(path string) (*controller, error) { + + /* (1) Extract data + ---------------------------------------------------------*/ + /* (1) Open file */ + var configFile, err = os.Open(path) + if err != nil { + return nil, err + } + defer configFile.Close() + + /* (2) Init receiving dataset */ + var receiver *controller + + /* (3) Decode JSON */ + decoder := json.NewDecoder(configFile) + err = decoder.Decode(receiver) + if err != nil { + return nil, err + } + + return receiver, nil +} diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..cc7f903 --- /dev/null +++ b/errors.go @@ -0,0 +1,48 @@ +package gfw + +import ( + "fmt" +) + +type Err struct { + Code int + Reason string + Arguments []interface{} +} + +var ( + /* Base */ + Success = &Err{0, "all right", nil} + Failure = &Err{1, "it failed", nil} + Unknown = &Err{-1, "", nil} + + NoMatchFound = &Err{2, "no resource found", nil} + AlreadyExists = &Err{3, "resource already exists", nil} + + Config = &Err{4, "configuration error", nil} + + /* I/O */ + UploadError = &Err{100, "upload failed", nil} + DownloadError = &Err{101, "download failed", nil} + MissingDownloadHeaders = &Err{102, "download headers are missing", nil} + MissingDownloadBody = &Err{103, "download body is missing", nil} + + /* Controllers */ + UnknownController = &Err{200, "unknown controller", nil} + UnknownMethod = &Err{201, "unknown method", nil} + UncallableController = &Err{202, "uncallable controller", nil} + UncallableMethod = &Err{203, "uncallable method", nil} + + /* Permissions */ + Permission = &Err{300, "permission error", nil} + Token = &Err{301, "token error", nil} + + /* Check */ + MissingParam = &Err{400, "missing parameter", nil} + InvalidParam = &Err{401, "invalid parameter", nil} + InvalidDefaultParam = &Err{402, "invalid default param", nil} +) + +func (e Err) Error() string { + return fmt.Sprintf("[%d] %s", e.Code, e.Reason) +} diff --git a/types.go b/types.go new file mode 100644 index 0000000..db5ce8b --- /dev/null +++ b/types.go @@ -0,0 +1,27 @@ +package gfw + +/* (1) Configuration +---------------------------------------------------------*/ + +type methodParameter struct { + Description string `json:"des"` + Type string `json:"typ"` + Rename *string `json:"ren"` + Optional *bool `json:"opt"` + Default *interface{} `json:"def"` +} +type method struct { + Description string `json:"des"` + Permission [][]string `json:"per"` + Parameters *map[string]methodParameter `json:"par"` + Options *map[string]interface{} `json:"opt"` +} + +type controller struct { + GET *method `json:"GET"` + POST *method `json:"POST"` + PUT *method `json:"PUT"` + DELETE *method `json:"DELETE"` + + Children map[string]controller `json:"/"` +}