Add Unimplemented types
This commit is contained in:
parent
91be16c3a4
commit
f5ba461012
@ -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 {
|
||||
|
55
generic/error.go
Normal file
55
generic/error.go
Normal file
@ -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
|
||||
}
|
58
generic/request.go
Normal file
58
generic/request.go
Normal file
@ -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
|
||||
}
|
30
generic/response.go
Normal file
30
generic/response.go
Normal file
@ -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
|
||||
}
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user