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

46 lines
753 B
Go
Raw Permalink Normal View History

2019-05-27 21:46:09 +00:00
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
}