2024-03-14 03:08:53 +05:30
|
|
|
package dataformat
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// dataformats is a list of dataformats
|
|
|
|
|
var dataformats map[string]DataFormat
|
|
|
|
|
|
2024-03-25 10:08:26 +05:30
|
|
|
const (
|
|
|
|
|
// DefaultKey is the key i.e used when given
|
|
|
|
|
// data is not of k-v type
|
|
|
|
|
DefaultKey = "value"
|
|
|
|
|
)
|
|
|
|
|
|
2024-03-14 03:08:53 +05:30
|
|
|
func init() {
|
|
|
|
|
dataformats = make(map[string]DataFormat)
|
|
|
|
|
|
|
|
|
|
// register the default data formats
|
|
|
|
|
RegisterDataFormat(NewJSON())
|
|
|
|
|
RegisterDataFormat(NewXML())
|
|
|
|
|
RegisterDataFormat(NewRaw())
|
|
|
|
|
RegisterDataFormat(NewForm())
|
|
|
|
|
RegisterDataFormat(NewMultiPartForm())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// JSONDataFormat is the name of the JSON data format
|
|
|
|
|
JSONDataFormat = "json"
|
|
|
|
|
// XMLDataFormat is the name of the XML data format
|
|
|
|
|
XMLDataFormat = "xml"
|
|
|
|
|
// RawDataFormat is the name of the Raw data format
|
|
|
|
|
RawDataFormat = "raw"
|
|
|
|
|
// FormDataFormat is the name of the Form data format
|
|
|
|
|
FormDataFormat = "form"
|
|
|
|
|
// MultiPartFormDataFormat is the name of the MultiPartForm data format
|
|
|
|
|
MultiPartFormDataFormat = "multipart/form-data"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Get returns the dataformat by name
|
|
|
|
|
func Get(name string) DataFormat {
|
|
|
|
|
return dataformats[name]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RegisterEncoder registers an encoder
|
|
|
|
|
func RegisterDataFormat(dataformat DataFormat) {
|
|
|
|
|
dataformats[dataformat.Name()] = dataformat
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DataFormat is an interface for encoding and decoding
|
|
|
|
|
type DataFormat interface {
|
|
|
|
|
// IsType returns true if the data is of the type
|
|
|
|
|
IsType(data string) bool
|
|
|
|
|
// Name returns the name of the encoder
|
|
|
|
|
Name() string
|
|
|
|
|
// Encode encodes the data into a format
|
2024-03-25 10:08:26 +05:30
|
|
|
Encode(data KV) (string, error)
|
2024-03-14 03:08:53 +05:30
|
|
|
// Decode decodes the data from a format
|
2024-03-25 10:08:26 +05:30
|
|
|
Decode(input string) (KV, error)
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decoded is a decoded data format
|
|
|
|
|
type Decoded struct {
|
|
|
|
|
// DataFormat is the data format
|
|
|
|
|
DataFormat string
|
|
|
|
|
// Data is the decoded data
|
2024-03-25 10:08:26 +05:30
|
|
|
Data KV
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decode decodes the data from a format
|
|
|
|
|
func Decode(data string) (*Decoded, error) {
|
|
|
|
|
for _, dataformat := range dataformats {
|
|
|
|
|
if dataformat.IsType(data) {
|
|
|
|
|
decoded, err := dataformat.Decode(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
value := &Decoded{
|
|
|
|
|
DataFormat: dataformat.Name(),
|
|
|
|
|
Data: decoded,
|
|
|
|
|
}
|
|
|
|
|
return value, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Encode encodes the data into a format
|
2024-03-25 10:08:26 +05:30
|
|
|
func Encode(data KV, dataformat string) (string, error) {
|
2024-03-14 03:08:53 +05:30
|
|
|
if dataformat == "" {
|
|
|
|
|
return "", errors.New("dataformat is required")
|
|
|
|
|
}
|
|
|
|
|
if encoder, ok := dataformats[dataformat]; ok {
|
|
|
|
|
return encoder.Encode(data)
|
|
|
|
|
}
|
|
|
|
|
return "", fmt.Errorf("dataformat %s is not supported", dataformat)
|
|
|
|
|
}
|