-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We should have a type request body. This PR parses the body to specific type. Passing to handlers (ingest,query runner) methods will added in the next PRs.
- Loading branch information
Showing
5 changed files
with
85 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package mux | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type JSON map[string]interface{} | ||
type NDJSON []JSON | ||
type Unknown []error | ||
|
||
type RequestBody interface { | ||
isParsedRequestBody() // this is a marker method | ||
} | ||
|
||
func (j JSON) isParsedRequestBody() {} | ||
func (n NDJSON) isParsedRequestBody() {} | ||
func (e Unknown) isParsedRequestBody() {} | ||
|
||
func ParseRequestBody(ctx context.Context, req *Request) { | ||
|
||
var errors []error | ||
|
||
// maybe it's a JSON | ||
|
||
if len(req.Body) > 1 && req.Body[0] == '{' { | ||
parsedBody := make(JSON) | ||
if err := json.Unmarshal([]byte(req.Body), &parsedBody); err != nil { | ||
errors = append(errors, fmt.Errorf("error while parsing JSON %s", err)) | ||
} else { | ||
req.ParsedBody = JSON(parsedBody) | ||
return | ||
} | ||
} | ||
|
||
// maybe it's a NDJSON | ||
|
||
if len(req.Body) > 1 && req.Body[0] == '{' { | ||
|
||
var ndjson NDJSON | ||
|
||
var err error | ||
for _, line := range strings.Split(req.Body, "\n") { | ||
|
||
parsedLine := make(JSON) | ||
|
||
err = json.Unmarshal([]byte(line), &parsedLine) | ||
if err != nil { | ||
errors = append(errors, fmt.Errorf("error while parsing NDJSON %s", err)) | ||
break | ||
} | ||
|
||
ndjson = append(ndjson, parsedLine) | ||
} | ||
if err == nil { | ||
req.ParsedBody = ndjson | ||
return | ||
} | ||
} | ||
|
||
// if nothing else, it's unknown | ||
|
||
req.ParsedBody = Unknown(errors) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters