This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Janos Pasztor
committed
Jun 9, 2021
1 parent
3af5bdb
commit c524bba
Showing
8 changed files
with
239 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// responseMarshaller is an interface to cover all encoders for HTTP response bodies. | ||
type responseMarshaller interface { | ||
SupportsMIME(mime string) bool | ||
Marshal(body interface{}) ([]byte, error) | ||
} | ||
|
||
//region JSON | ||
|
||
type jsonMarshaller struct { | ||
} | ||
|
||
func (j *jsonMarshaller) SupportsMIME(mime string) bool { | ||
return mime == "application/json" || mime == "application/*" || mime == "*/*" | ||
} | ||
|
||
func (j *jsonMarshaller) Marshal(body interface{}) ([]byte, error) { | ||
return json.Marshal(body) | ||
} | ||
|
||
func (j *jsonMarshaller) Unmarshal(body []byte, target interface{}) error { | ||
return json.Unmarshal(body, target) | ||
} | ||
|
||
// endregion | ||
|
||
// region Text | ||
type TextMarshallable interface { | ||
MarshalText() string | ||
} | ||
|
||
type textMarshaller struct { | ||
} | ||
|
||
func (t *textMarshaller) SupportsMIME(mime string) bool { | ||
// HTML output might be better suited to piping through a templating engine. | ||
return mime == "text/html" || mime == "text/plain" || mime == "text/*" || mime == "*/*" | ||
} | ||
|
||
func (t *textMarshaller) Marshal(body interface{}) ([]byte, error) { | ||
switch assertedBody := body.(type) { | ||
case TextMarshallable: | ||
return []byte(assertedBody.MarshalText()), nil | ||
case string: | ||
return []byte(assertedBody), nil | ||
case int: | ||
return t.marshalNumber(body) | ||
case int8: | ||
return t.marshalNumber(body) | ||
case int16: | ||
return t.marshalNumber(body) | ||
case int32: | ||
return t.marshalNumber(body) | ||
case int64: | ||
return t.marshalNumber(body) | ||
case uint: | ||
return t.marshalNumber(body) | ||
case uint8: | ||
return t.marshalNumber(body) | ||
case uint16: | ||
return t.marshalNumber(body) | ||
case uint32: | ||
return t.marshalNumber(body) | ||
case uint64: | ||
return t.marshalNumber(body) | ||
case bool: | ||
if body.(bool) { | ||
return []byte("true"), nil | ||
} else { | ||
return []byte("false"), nil | ||
} | ||
case uintptr: | ||
return t.marshalPointer(body) | ||
default: | ||
return nil, fmt.Errorf("cannot marshal unknown type: %v", body) | ||
} | ||
} | ||
|
||
func (t *textMarshaller) marshalNumber(body interface{}) ([]byte, error) { | ||
return []byte(fmt.Sprintf("%d", body)), nil | ||
} | ||
|
||
func (t *textMarshaller) marshalPointer(body interface{}) ([]byte, error) { | ||
ptr := body.(uintptr) | ||
return t.Marshal(reflect.ValueOf(ptr).Elem()) | ||
} | ||
|
||
// endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package http | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestTextMarshal(t *testing.T) { | ||
dataSet := []interface{}{ | ||
42, | ||
int8(42), | ||
int16(42), | ||
int32(42), | ||
int64(42), | ||
uint(42), | ||
uint8(42), | ||
uint16(42), | ||
uint32(42), | ||
uint64(42), | ||
"42", | ||
testData{}, | ||
&testData{}, | ||
} | ||
|
||
marshaller := &textMarshaller{} | ||
for _, v := range dataSet { | ||
t.Run(reflect.TypeOf(v).Name(), func(t *testing.T) { | ||
result, err := marshaller.Marshal(v) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(result) != "42" { | ||
t.Fatalf("unexpected marshal result: %s", result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type testData struct{} | ||
|
||
func (t testData) MarshalText() string { | ||
return "42" | ||
} |