MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / getFunctionArgs

Method getFunctionArgs

rest/functions_api.go:60–109  ·  view source on GitHub ↗

Subroutine for reading function name and arguments from a request

(maxSize *int)

Source from the content-addressed store, hash-verified

58
59// Subroutine for reading function name and arguments from a request
60func (h *handler) getFunctionArgs(maxSize *int) (string, map[string]interface{}, error) {
61 name := h.PathVar(kFnNameParam)
62 args := map[string]interface{}{}
63 var err error
64 if h.rq.Method == "POST" {
65 // POST: Args come from the request body in JSON format:
66 input, err := processContentEncoding(h.rq.Header, h.requestBody, "application/json")
67 if err != nil {
68 return "", nil, err
69 }
70 if h.rq.ContentLength >= 0 {
71 if err := db.CheckRequestSize(h.rq.ContentLength, maxSize); err != nil {
72 return "", nil, err
73 }
74 }
75 // Decode the body bytes into target structure.
76 decoder := json.NewDecoder(input)
77 err = decoder.Decode(&args)
78 _ = input.Close()
79 if err == nil {
80 err := db.CheckRequestSize(decoder.InputOffset(), maxSize)
81 if err != nil {
82 return "", nil, err
83 }
84 }
85 } else {
86 // GET: Params come from the URL queries (`?key=value`):
87 if err := db.CheckRequestSize(int64(len(h.rq.URL.RawQuery)), maxSize); err != nil {
88 return "", nil, err
89 }
90 for key, values := range h.getQueryValues() {
91 // `values` is an array of strings, one per instance of the key in the URL
92 if len(values) > 1 {
93 return "", nil, base.HTTPErrorf(http.StatusBadRequest, "Duplicate parameter '%s'", key)
94 }
95 value := values[0]
96 // Parse value as JSON if it looks like JSON, else just as a raw string:
97 if len(value) > 0 && strings.IndexByte(`0123456789-"[{`, value[0]) >= 0 {
98 var jsonValue interface{}
99 if base.JSONUnmarshal([]byte(value), &jsonValue) != nil {
100 return "", nil, base.HTTPErrorf(http.StatusBadRequest, "Value of ?%s is not valid JSON", key)
101 }
102 args[key] = jsonValue
103 } else {
104 args[key] = value
105 }
106 }
107 }
108 return name, args, err
109}
110
111// Subroutine to write N1QL query results to a response
112func (h *handler) writeQueryRows(rows sgbucket.QueryResultIterator) error {

Callers 1

handleFunctionCallMethod · 0.95

Calls 8

PathVarMethod · 0.95
getQueryValuesMethod · 0.95
CheckRequestSizeFunction · 0.92
HTTPErrorfFunction · 0.92
JSONUnmarshalFunction · 0.92
processContentEncodingFunction · 0.85
DecodeMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected