github.com/go-kivik/kivik/v4@v4.3.2/couchdb/json.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package couchdb 14 15 import ( 16 "encoding/json" 17 "net/http" 18 19 internal "github.com/go-kivik/kivik/v4/int/errors" 20 ) 21 22 // encodeKey encodes a key to a view query, or similar, to be passed to CouchDB. 23 func encodeKey(i interface{}) (string, error) { 24 if raw, ok := i.(json.RawMessage); ok { 25 return string(raw), nil 26 } 27 raw, err := json.Marshal(i) 28 if err != nil { 29 err = &internal.Error{Status: http.StatusBadRequest, Err: err} 30 } 31 return string(raw), err 32 } 33 34 var jsonKeys = []string{"endkey", "end_key", "key", "startkey", "start_key", "keys", "doc_ids"} 35 36 func encodeKeys(opts map[string]interface{}) error { 37 for _, key := range jsonKeys { 38 if v, ok := opts[key]; ok { 39 value, err := encodeKey(v) 40 if err != nil { 41 return err 42 } 43 opts[key] = value 44 } 45 } 46 return nil 47 }