nurse/protocols/http/json_validation.go
Peter Kurfer c52912c5aa
Some checks failed
agola/nurse/Test and lint The run failed
refactor: migrate to new Gitea URL
2022-09-22 11:46:36 +02:00

61 lines
1.3 KiB
Go

package http
import (
"errors"
"io"
"net/http"
"github.com/valyala/bytebufferpool"
"code.icb4dc0.de/prskr/nurse/grammar"
"code.icb4dc0.de/prskr/nurse/validation"
)
var _ validation.FromCall[*http.Response] = (*JSONPathValidator)(nil)
type JSONPathValidator struct {
validator *validation.JSONPathValidator
}
func (j *JSONPathValidator) UnmarshalCall(c grammar.Call) error {
const pathAndWantArgsCount = 2
if err := grammar.ValidateParameterCount(c.Params, pathAndWantArgsCount); err != nil {
return err
}
var (
jsonPath string
err error
)
if jsonPath, err = c.Params[0].AsString(); err != nil {
return err
}
switch c.Params[1].Type() {
case grammar.ParamTypeInt:
j.validator, err = validation.JSONPathValidatorFor(jsonPath, *c.Params[1].Int)
case grammar.ParamTypeFloat:
j.validator, err = validation.JSONPathValidatorFor(jsonPath, *c.Params[1].Float)
case grammar.ParamTypeString:
j.validator, err = validation.JSONPathValidatorFor(jsonPath, *c.Params[1].String)
case grammar.ParamTypeUnknown:
fallthrough
default:
return errors.New("param type unknown")
}
return err
}
func (j *JSONPathValidator) Validate(resp *http.Response) error {
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
readBytes, err := io.Copy(buf, resp.Body)
if err != nil {
return err
}
return j.validator.Equals(buf.B[:readBytes])
}