-
Notifications
You must be signed in to change notification settings - Fork 12
/
serving.go
59 lines (53 loc) · 1.47 KB
/
serving.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package aqua
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
)
func writeOutput(w http.ResponseWriter, outType []string, outVals []reflect.Value, pretty string) {
if len(outType) == 1 {
if outType[0] == "int" {
w.WriteHeader(int(outVals[0].Int()))
} else {
writeItem(w, outType[0], outVals[0], pretty)
}
} else if len(outType) == 2 {
w.WriteHeader(int(outVals[0].Int()))
writeItem(w, outType[1], outVals[1], pretty)
}
}
func writeItem(w http.ResponseWriter, oType string, oVal reflect.Value, pretty string) {
if strings.HasPrefix(oType, "*st:") {
o := oVal.Elem()
writeItem(w, getSymbolFromType(o.Type()), o, pretty)
return
}
switch {
case oType == "string":
v := oVal.String()
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Content-Length", strconv.Itoa(len(v)))
fmt.Fprintf(w, "%s", v)
case oType == "st:github.com/tolexo/aqua.Sac":
s := oVal.Interface().(Sac)
writeItem(w, getSymbolFromType(reflect.TypeOf(s.Data)), reflect.ValueOf(s.Data), pretty)
case oType == "map", strings.HasPrefix(oType, "st:"):
var j []byte
var e error
if pretty == "true" || pretty == "1" {
j, e = json.MarshalIndent(oVal.Interface(), "", "\t")
panicIf(e)
} else {
j, e = json.Marshal(oVal.Interface())
panicIf(e)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", strconv.Itoa(len(j)))
w.Write(j)
default:
fmt.Printf("Don't know how to return a: %s?\n", oType)
}
}