зеркало из https://github.com/mislav/hub.git
Add `yamlConfigEncoder` and `yamlConfigDecoder`
This commit is contained in:
Родитель
72d8220279
Коммит
ae77e64ff8
|
@ -2,8 +2,10 @@ package github
|
|||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"gopkg.in/yaml.v1"
|
||||
)
|
||||
|
||||
type configDecoder interface {
|
||||
|
@ -17,3 +19,15 @@ func (t *tomlConfigDecoder) Decode(r io.Reader, v interface{}) error {
|
|||
_, err := toml.DecodeReader(r, v)
|
||||
return err
|
||||
}
|
||||
|
||||
type yamlConfigDecoder struct {
|
||||
}
|
||||
|
||||
func (y *yamlConfigDecoder) Decode(r io.Reader, v interface{}) error {
|
||||
d, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return yaml.Unmarshal(d, v)
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"io"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"gopkg.in/yaml.v1"
|
||||
)
|
||||
|
||||
type configEncoder interface {
|
||||
|
@ -17,3 +18,20 @@ func (t *tomlConfigEncoder) Encode(w io.Writer, v interface{}) error {
|
|||
enc := toml.NewEncoder(w)
|
||||
return enc.Encode(v)
|
||||
}
|
||||
|
||||
type yamlConfigEncoder struct {
|
||||
}
|
||||
|
||||
func (y *yamlConfigEncoder) Encode(w io.Writer, v interface{}) error {
|
||||
d, err := yaml.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
n, err := w.Write(d)
|
||||
if err == nil && n < len(d) {
|
||||
err = io.ErrShortWrite
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ import (
|
|||
|
||||
func newConfigService() *configService {
|
||||
return &configService{
|
||||
Encoder: &tomlConfigEncoder{},
|
||||
Decoder: &tomlConfigDecoder{},
|
||||
Encoder: &yamlConfigEncoder{},
|
||||
Decoder: &yamlConfigDecoder{},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче