Fix dsd http interface

This commit is contained in:
Daniel 2021-11-29 11:58:00 +01:00
parent 1695420b0e
commit cbd9d9a8ae
2 changed files with 18 additions and 6 deletions

View file

@ -87,12 +87,21 @@ func Dump(t interface{}, format uint8) ([]byte, error) {
// DumpIndent stores the interface as a dsd formatted data structure with indentation, if available. // DumpIndent stores the interface as a dsd formatted data structure with indentation, if available.
func DumpIndent(t interface{}, format uint8, indent string) ([]byte, error) { func DumpIndent(t interface{}, format uint8, indent string) ([]byte, error) {
data, err := dumpWithoutIdentifier(t, format, indent)
if err != nil {
return nil, err
}
// TODO: Find a better way to do this.
return append(varint.Pack8(format), data...), nil
}
func dumpWithoutIdentifier(t interface{}, format uint8, indent string) ([]byte, error) {
format, ok := ValidateSerializationFormat(format) format, ok := ValidateSerializationFormat(format)
if !ok { if !ok {
return nil, ErrIncompatibleFormat return nil, ErrIncompatibleFormat
} }
f := varint.Pack8(format)
var data []byte var data []byte
var err error var err error
switch format { switch format {
@ -135,7 +144,5 @@ func DumpIndent(t interface{}, format uint8, indent string) ([]byte, error) {
return nil, ErrIncompatibleFormat return nil, ErrIncompatibleFormat
} }
// TODO: Find a better way to do this. return data, nil
f = append(f, data...)
return f, nil
} }

View file

@ -61,6 +61,11 @@ func RequestHTTPResponseFormat(r *http.Request, format uint8) (mimeType string,
if !ok { if !ok {
return "", ErrIncompatibleFormat return "", ErrIncompatibleFormat
} }
// Omit charset.
mimeType, _, err = mime.ParseMediaType(mimeType)
if err != nil {
return "", fmt.Errorf("dsd: failed to parse content type: %w", err)
}
// Request response format. // Request response format.
r.Header.Set("Accept", mimeType) r.Header.Set("Accept", mimeType)
@ -77,7 +82,7 @@ func DumpToHTTPRequest(r *http.Request, t interface{}, format uint8) error {
} }
// Serialize data. // Serialize data.
data, err := Dump(t, format) data, err := dumpWithoutIdentifier(t, format, "")
if err != nil { if err != nil {
return fmt.Errorf("dsd: failed to serialize: %w", err) return fmt.Errorf("dsd: failed to serialize: %w", err)
} }
@ -101,7 +106,7 @@ func DumpToHTTPResponse(w http.ResponseWriter, r *http.Request, t interface{}) e
} }
// Serialize data. // Serialize data.
data, err := Dump(t, format) data, err := dumpWithoutIdentifier(t, format, "")
if err != nil { if err != nil {
return fmt.Errorf("dsd: failed to serialize: %w", err) return fmt.Errorf("dsd: failed to serialize: %w", err)
} }