This repository has been archived on 2019-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
scoreboard/ioutil/reader.go
2019-05-27 23:46:09 +02:00

45 lines
753 B
Go

package ioutil
import (
"encoding/json"
"io"
"io/ioutil"
validator "gopkg.in/go-playground/validator.v9"
)
var Validate = validator.New()
func ExtractJSON(reader io.Reader, vv ...interface{}) error {
data, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
for _, v := range vv {
if err := json.Unmarshal(data, v); err != nil {
return err
}
}
return nil
}
func ExtractAndValidateJSON(reader io.Reader, vv ...interface{}) error {
data, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
for _, v := range vv {
if err := json.Unmarshal(data, v); err != nil {
return err
}
if err := Validate.Struct(v); err != nil {
return err
}
}
return nil
}