Use Unimplemented types as base

This commit is contained in:
some 2024-10-08 21:24:55 -04:00
parent df289d1e35
commit 604dcbf426
Signed by: some
GPG Key ID: 65D0589220B9BFC8
6 changed files with 22 additions and 134 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.23.1
require ( require (
somehole.com/common/log v0.1.3 somehole.com/common/log v0.1.3
somehole.com/common/security v0.2.1 somehole.com/common/security v0.2.1
somehole.com/service/router v0.8.0 somehole.com/service/router v0.12.0
) )
require ( require (

4
go.sum
View File

@ -8,5 +8,5 @@ somehole.com/common/log v0.1.3 h1:2PAui0+5EryTAHqVUZQeepLcJTGssHKz2OL+jBknHI0=
somehole.com/common/log v0.1.3/go.mod h1:NS2eHnN120GA6oFbBm3XhB5yHww0eXTbLuMQYZxYNyA= somehole.com/common/log v0.1.3/go.mod h1:NS2eHnN120GA6oFbBm3XhB5yHww0eXTbLuMQYZxYNyA=
somehole.com/common/security v0.2.1 h1:H7TpErYKsCOxKYYie75jU7azO5vEgeqrQ9uA+AjXafA= somehole.com/common/security v0.2.1 h1:H7TpErYKsCOxKYYie75jU7azO5vEgeqrQ9uA+AjXafA=
somehole.com/common/security v0.2.1/go.mod h1:6SMKdIrfxT460XXWafpD6A9yT/ykwipGsGztA5g5vVM= somehole.com/common/security v0.2.1/go.mod h1:6SMKdIrfxT460XXWafpD6A9yT/ykwipGsGztA5g5vVM=
somehole.com/service/router v0.8.0 h1:93+qKPpllgWSXGQTvBYLVK565rrFSWTxvMevepDCesY= somehole.com/service/router v0.12.0 h1:nrBn+OJF9cN22vgXcZYdcSbLTamPwMEvu4NG2lJLO88=
somehole.com/service/router v0.8.0/go.mod h1:F+/sNY4/ei7C9QD5+iA+gUHugcOAcrWspHKwX8d8oiM= somehole.com/service/router v0.12.0/go.mod h1:F+/sNY4/ei7C9QD5+iA+gUHugcOAcrWspHKwX8d8oiM=

View File

@ -1,17 +1,14 @@
package server package server
import ( import (
"io" "encoding/json"
"somehole.com/service/oauth2/session" "somehole.com/service/oauth2/session"
"somehole.com/service/router" "somehole.com/service/router"
) )
type CallbackRequestBuilder struct { type CallbackRequestBuilder struct {
allowedMethods []string router.UnimplementedRequestBuilder
header struct {
router.Header
}
values struct { values struct {
router.Values router.Values
State session.State `form:"state"` State session.State `form:"state"`
@ -19,38 +16,8 @@ type CallbackRequestBuilder struct {
} }
} }
func (crb CallbackRequestBuilder) RequestBuilder() router.RequestBuilder { func (rb *CallbackRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) {
return &crb err := rb.values.Values.Parse(values)
}
func (crb *CallbackRequestBuilder) Allowed(method string) (errRes router.ErrorResponse) {
var ok bool
for _, m := range crb.allowedMethods {
if m == method {
ok = true
}
}
if !ok {
return ErrorMethodNotAllowed
}
return Ok
}
func (crb *CallbackRequestBuilder) Header(header router.Header) (errRes router.ErrorResponse) {
err := crb.header.Header.Parse(header)
if err != nil {
return ErrorBadRequest
}
return Ok
}
func (crb *CallbackRequestBuilder) ReadBody(body io.ReadCloser) (errRes router.ErrorResponse) {
defer body.Close()
return Ok
}
func (crb *CallbackRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) {
err := crb.values.Values.Parse(values)
if err != nil { if err != nil {
return ErrorBadRequest return ErrorBadRequest
} }
@ -58,27 +25,15 @@ func (crb *CallbackRequestBuilder) Values(values router.Values) (errRes router.E
} }
type CallbackResponse struct { type CallbackResponse struct {
header struct { router.UnimplementedResponse
router.Header
}
Body struct { Body struct {
Message string `json:"message"` Message string `json:"message"`
} }
} }
func (cr CallbackResponse) Response() router.Response { func (r *CallbackResponse) BodyBytes() (body []byte) {
return &cr body, _ = json.Marshal(r.Body)
} return
func (cr *CallbackResponse) Header() (header router.Header) {
if cr.header.Header == nil {
cr.header.Header.Parse(cr.header)
}
return cr.header.Header
}
func (cr *CallbackResponse) BodyBytes() []byte {
return mustMarshalJson(cr.Body)
} }
type UnimplementedCallbackServer struct{} type UnimplementedCallbackServer struct{}

View File

@ -1,14 +0,0 @@
package server
import (
"encoding/json"
"fmt"
)
func mustMarshalJson(in any) []byte {
out, err := json.Marshal(in)
if err != nil {
panic(fmt.Errorf("could not marshal %#v: %v", in, err))
}
return out
}

View File

@ -1,9 +1,9 @@
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings"
) )
type Error uint32 type Error uint32
@ -50,14 +50,6 @@ func (e Error) Error() (out string) {
} }
func (e Error) BodyBytes() (body []byte) { func (e Error) BodyBytes() (body []byte) {
var msg string body, _ = json.Marshal(struct{ Error string }{Error: e.String()})
switch e { return
default:
msg = strings.Join(strings.Split(e.String(), " "), "_")
}
return mustMarshalJson(struct {
Error string `json:"error"`
}{
Error: msg,
})
} }

View File

@ -1,17 +1,14 @@
package server package server
import ( import (
"io" "encoding/json"
"somehole.com/service/oauth2/session" "somehole.com/service/oauth2/session"
"somehole.com/service/router" "somehole.com/service/router"
) )
type TokenRequestBuilder struct { type TokenRequestBuilder struct {
allowedMethods []string router.UnimplementedRequestBuilder
header struct {
router.Header
}
values struct { values struct {
router.Values router.Values
ResponseType string `form:"response_type"` ResponseType string `form:"response_type"`
@ -20,38 +17,8 @@ type TokenRequestBuilder struct {
} }
} }
func (trb TokenRequestBuilder) RequestBuilder() router.RequestBuilder { func (rb *TokenRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) {
return &trb err := rb.values.Values.Parse(values)
}
func (trb *TokenRequestBuilder) Allowed(method string) (errRes router.ErrorResponse) {
var ok bool
for _, m := range trb.allowedMethods {
if m == method {
ok = true
}
}
if !ok {
return ErrorMethodNotAllowed
}
return Ok
}
func (trb *TokenRequestBuilder) Header(header router.Header) (errRes router.ErrorResponse) {
err := trb.header.Header.Parse(header)
if err != nil {
return ErrorBadRequest
}
return Ok
}
func (trb *TokenRequestBuilder) ReadBody(body io.ReadCloser) (errRes router.ErrorResponse) {
defer body.Close()
return Ok
}
func (trb *TokenRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) {
err := trb.values.Values.Parse(values)
if err != nil { if err != nil {
return ErrorBadRequest return ErrorBadRequest
} }
@ -59,9 +26,7 @@ func (trb *TokenRequestBuilder) Values(values router.Values) (errRes router.Erro
} }
type TokenResponse struct { type TokenResponse struct {
header struct { router.UnimplementedResponse
router.Header
}
Body struct { Body struct {
VerificationUri string `json:"verification_uri"` VerificationUri string `json:"verification_uri"`
UserCode session.Code `json:"user_code"` UserCode session.Code `json:"user_code"`
@ -71,19 +36,9 @@ type TokenResponse struct {
} }
} }
func (tr TokenResponse) Response() router.Response { func (r *TokenResponse) BodyBytes() (body []byte) {
return &tr body, _ = json.Marshal(r.Body)
} return
func (tr *TokenResponse) Header() (header router.Header) {
if tr.header.Header == nil {
tr.header.Header.Parse(tr.header)
}
return tr.header.Header
}
func (tr *TokenResponse) BodyBytes() []byte {
return mustMarshalJson(tr.Body)
} }
type UnimplementedTokenServer struct{} type UnimplementedTokenServer struct{}