Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Forceu committed Feb 3, 2025
1 parent b98ceb3 commit c2a325a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 104 deletions.
8 changes: 2 additions & 6 deletions build/go-generate/updateApiRouting.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,9 @@ func generateParseRequestMethod(typeName string, fields []*ast.Field) string {
case "string":
method += fmt.Sprintf(`
if (exists) {
p.%s, err = parseHeaderString(r, %s)
if err != nil {
return fmt.Errorf("invalid value in header %s supplied")
}
p.%s = r.Header.Get(%s)
}
`, field.Names[0].Name, headerName, strings.Replace(headerName, "\"", "", -1))
`, field.Names[0].Name, headerName)

case "bool":
method += fmt.Sprintf(`
Expand Down Expand Up @@ -177,7 +174,6 @@ func generateParseRequestMethod(typeName string, fields []*ast.Field) string {
}
}
}

method += "\nreturn p.ProcessParameter(r)\n}\n"
method += writeNewInstanceCode(typeName)

Expand Down
10 changes: 4 additions & 6 deletions internal/webserver/api/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ func getRouting(requestUrl string) (apiRoute, bool) {
}

type requestParser interface {
ProcessParameter(r *http.Request) error
// ParseRequest reads the supplied headers
// ParseRequest reads the supplied headers, stores them and afterwards calls ProcessParameter()
ParseRequest(r *http.Request) error
// ProcessParameter goes through the submitted parameters, checks them and converts them to expected values
ProcessParameter(r *http.Request) error
// New returns an empty struct of the type
New() requestParser
}

Expand Down Expand Up @@ -448,10 +450,6 @@ func checkHeaderExists(r *http.Request, key string, isRequired bool) (bool, erro
return exists, nil
}

func parseHeaderString(r *http.Request, key string) (string, error) {
return r.Header.Get(key), nil
}

func parseHeaderBool(r *http.Request, key string) (bool, error) {
value, err := strconv.ParseBool(r.Header.Get(key))
if err != nil {
Expand Down
115 changes: 23 additions & 92 deletions internal/webserver/api/routingParsing.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c2a325a

Please sign in to comment.