From f5ba4610123c2721a43509ebdf9ce0358b1c12d1 Mon Sep 17 00:00:00 2001 From: some Date: Tue, 8 Oct 2024 21:06:53 -0400 Subject: [PATCH] Add Unimplemented types --- common.go | 6 ++--- generic/error.go | 55 ++++++++++++++++++++++++++++++++++++++++++ generic/request.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ generic/response.go | 30 +++++++++++++++++++++++ header.go | 2 +- values.go | 2 +- 6 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 generic/error.go create mode 100644 generic/request.go create mode 100644 generic/response.go diff --git a/common.go b/common.go index edbd905..db19065 100644 --- a/common.go +++ b/common.go @@ -5,11 +5,11 @@ import ( "reflect" ) -type GenericValues map[string][]string +type genericValues map[string][]string -func (gv GenericValues) Parse(data any, tag string) (err error) { +func (gv genericValues) Parse(data any, tag string) (err error) { if gv == nil { - gv = make(GenericValues) + gv = make(genericValues) } d := reflect.ValueOf(data) if d.Kind() != reflect.Struct { diff --git a/generic/error.go b/generic/error.go new file mode 100644 index 0000000..3ba1139 --- /dev/null +++ b/generic/error.go @@ -0,0 +1,55 @@ +package unimplemented + +import ( + "encoding/json" + "fmt" + "net/http" +) + +type Error uint32 + +const ( + Ok Error = http.StatusOK + ErrorNotImplemented Error = http.StatusNotImplemented + ErrorMethodNotAllowed Error = http.StatusMethodNotAllowed + ErrorBadRequest Error = http.StatusBadRequest + ErrorUnauthorized Error = http.StatusUnauthorized + ErrorServerError Error = http.StatusInternalServerError +) + +func (e Error) Ok() (ok bool) { + return e == Ok +} + +func (e Error) HttpStatus() (code int) { + return int(e) +} + +func (e Error) String() (out string) { + switch e { + case Ok: + out = "ok" + case ErrorNotImplemented: + out = "server not implemented" + case ErrorMethodNotAllowed: + out = "method not allowed" + case ErrorBadRequest: + out = "bad request" + case ErrorUnauthorized: + out = "user unauthorized" + case ErrorServerError: + out = "internal server error" + default: + out = "unhandled error" + } + return +} + +func (e Error) Error() (out string) { + return fmt.Sprintf("%s (%d)", e.String(), e.HttpStatus()) +} + +func (e Error) BodyBytes() (body []byte) { + body, _ = json.Marshal(struct{ Error string }{Error: e.String()}) + return +} diff --git a/generic/request.go b/generic/request.go new file mode 100644 index 0000000..03ef57f --- /dev/null +++ b/generic/request.go @@ -0,0 +1,58 @@ +package unimplemented + +import ( + "encoding/json" + "io" + + "somehole.com/service/router" +) + +type RequestBuilder struct { + allowedMethods []string + header struct { + router.Header + } + values struct { + router.Values + } + Body struct{} +} + +func (rb RequestBuilder) RequestBuilder() router.RequestBuilder { + return &rb +} + +func (rb *RequestBuilder) Allowed(method string) (errRes router.ErrorResponse) { + var ok bool + for _, m := range rb.allowedMethods { + if m == method { + ok = true + } + } + if !ok { + return ErrorMethodNotAllowed + } + return Ok +} + +func (rb *RequestBuilder) Header(header router.Header) (errRes router.ErrorResponse) { + err := rb.header.Header.Parse(header) + if err != nil { + return ErrorBadRequest + } + return Ok +} + +func (rb *RequestBuilder) ReadBody(body io.ReadCloser) (errRes router.ErrorResponse) { + defer body.Close() + json.NewDecoder(body).Decode(&rb.Body) + return Ok +} + +func (rb *RequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) { + err := rb.values.Values.Parse(values) + if err != nil { + return ErrorBadRequest + } + return Ok +} diff --git a/generic/response.go b/generic/response.go new file mode 100644 index 0000000..2afe1b9 --- /dev/null +++ b/generic/response.go @@ -0,0 +1,30 @@ +package unimplemented + +import ( + "encoding/json" + + "somehole.com/service/router" +) + +type Response struct { + header struct { + router.Header + } + Body struct{} +} + +func (r Response) Response() router.Response { + return &r +} + +func (r *Response) Header() (header router.Header) { + if r.header.Header == nil { + r.header.Header.Parse(r.header) + } + return r.header.Header +} + +func (r *Response) BodyBytes() (body []byte) { + body, _ = json.Marshal(r.Body) + return +} diff --git a/header.go b/header.go index b1e6556..f60ed6c 100644 --- a/header.go +++ b/header.go @@ -7,7 +7,7 @@ import ( type Header map[string][]string func (h Header) Parse(data any) error { - return GenericValues(h).Parse(data, "header") + return genericValues(h).Parse(data, "header") } func (h Header) Get(key string) (value string) { diff --git a/values.go b/values.go index d0f9d8d..445637c 100644 --- a/values.go +++ b/values.go @@ -3,5 +3,5 @@ package router type Values map[string][]string func (v Values) Parse(data any) error { - return GenericValues(v).Parse(data, "form") + return genericValues(v).Parse(data, "form") }