FIx shared structs + add better debugging/linting (#379)
* fix: Fixes issues w/ shared pointers to structs (#378) * feat: Adds even better debugging and linting support (#376) This is a #PATCH release.
This commit is contained in:
committed by
GitHub
parent
c3d7f26e3c
commit
5c9b88db4f
@@ -28,7 +28,13 @@ func middleware(h http.Handler, middlewares ...mw) http.HandlerFunc {
|
||||
|
||||
var validate = validator.New()
|
||||
|
||||
type methodToPayload map[string]any
|
||||
type methodToPayload map[string]func() any
|
||||
|
||||
// Generic factory function to create new payload instances per request
|
||||
func newPayload[T any]() any {
|
||||
var p T
|
||||
return &p
|
||||
}
|
||||
|
||||
type validatorMiddleware struct {
|
||||
validate *validator.Validate
|
||||
@@ -40,7 +46,8 @@ type validatorMiddleware struct {
|
||||
func (p validatorMiddleware) handle(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if p.methodToPayload[r.Method] == nil { // If no payload to validate for this method type...
|
||||
constructor, exists := p.methodToPayload[r.Method]
|
||||
if !exists { // If no payload to validate for this method type...
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
@@ -51,7 +58,9 @@ func (p validatorMiddleware) handle(next http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
pl := p.methodToPayload[r.Method]
|
||||
// Create a new instance for this request
|
||||
pl := constructor()
|
||||
|
||||
err = json.Unmarshal(body, &pl)
|
||||
|
||||
if err != nil {
|
||||
@@ -72,7 +81,7 @@ func (p validatorMiddleware) handle(next http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
// Pass the parsed data so we don't have to re-parse it in the handler
|
||||
ctx := context.WithValue(r.Context(), payload(payload("payload")), pl)
|
||||
ctx := context.WithValue(r.Context(), payload("payload"), pl)
|
||||
r = r.WithContext(ctx)
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
@@ -101,18 +110,18 @@ func (m withMrMiddleware) handle(next http.Handler) http.Handler {
|
||||
|
||||
mergeRequests, _, err := m.client.ListProjectMergeRequests(m.data.projectInfo.ProjectId, &options)
|
||||
if err != nil {
|
||||
handleError(w, fmt.Errorf("Failed to list merge requests: %w", err), "Failed to list merge requests", http.StatusInternalServerError)
|
||||
handleError(w, fmt.Errorf("failed to list merge requests: %w", err), "Failed to list merge requests", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if len(mergeRequests) == 0 {
|
||||
err := fmt.Errorf("Branch '%s' does not have any merge requests", m.data.gitInfo.BranchName)
|
||||
err := fmt.Errorf("branch '%s' does not have any merge requests", m.data.gitInfo.BranchName)
|
||||
handleError(w, err, "No MRs Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if len(mergeRequests) > 1 {
|
||||
err := errors.New("Please call gitlab.choose_merge_request()")
|
||||
err := errors.New("please call gitlab.choose_merge_request()")
|
||||
handleError(w, err, "Multiple MRs found", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@@ -155,9 +164,9 @@ func withMethodCheck(methods ...string) mw {
|
||||
}
|
||||
|
||||
// Helper function to format validation errors into more readable strings
|
||||
func formatValidationErrors(errors validator.ValidationErrors) error {
|
||||
func formatValidationErrors(errs validator.ValidationErrors) error {
|
||||
var s strings.Builder
|
||||
for i, e := range errors {
|
||||
for i, e := range errs {
|
||||
if i > 0 {
|
||||
s.WriteString("; ")
|
||||
}
|
||||
@@ -169,5 +178,5 @@ func formatValidationErrors(errors validator.ValidationErrors) error {
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf(s.String())
|
||||
return errors.New(s.String())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user