mirror of
https://github.com/Bornholm/formidable.git
synced 2025-07-23 14:01:33 +02:00
feat: values updaters
This commit is contained in:
40
internal/data/format/json/encoder_handler.go
Normal file
40
internal/data/format/json/encoder_handler.go
Normal file
@ -0,0 +1,40 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"forge.cadoles.com/wpetit/formidable/internal/data/format"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type EncoderHandler struct{}
|
||||
|
||||
func (d *EncoderHandler) Match(url *url.URL) bool {
|
||||
ext := filepath.Ext(path.Join(url.Host, url.Path))
|
||||
|
||||
return ext == ExtensionJSON ||
|
||||
format.MatchURLQueryFormat(url, FormatJSON)
|
||||
}
|
||||
|
||||
func (d *EncoderHandler) Encode(url *url.URL, data interface{}) (io.Reader, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
encoder := json.NewEncoder(&buf)
|
||||
|
||||
encoder.SetIndent("", " ")
|
||||
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return &buf, nil
|
||||
}
|
||||
|
||||
func NewEncoderHandler() *EncoderHandler {
|
||||
return &EncoderHandler{}
|
||||
}
|
38
internal/data/format/yaml/encoder_handler.go
Normal file
38
internal/data/format/yaml/encoder_handler.go
Normal file
@ -0,0 +1,38 @@
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"forge.cadoles.com/wpetit/formidable/internal/data/format"
|
||||
"github.com/pkg/errors"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type EncoderHandler struct{}
|
||||
|
||||
func (d *EncoderHandler) Match(url *url.URL) bool {
|
||||
ext := filepath.Ext(path.Join(url.Host, url.Path))
|
||||
|
||||
return ExtensionYAML.MatchString(ext) ||
|
||||
format.MatchURLQueryFormat(url, FormatYAML)
|
||||
}
|
||||
|
||||
func (d *EncoderHandler) Encode(url *url.URL, data interface{}) (io.Reader, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
encoder := yaml.NewEncoder(&buf)
|
||||
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return &buf, nil
|
||||
}
|
||||
|
||||
func NewEncoderHandler() *EncoderHandler {
|
||||
return &EncoderHandler{}
|
||||
}
|
Reference in New Issue
Block a user