github.com/go-kivik/kivik/v4@v4.3.2/x/collate/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 collate
    14  
    15  import (
    16  	"fmt"
    17  	"reflect"
    18  )
    19  
    20  type jsonType int
    21  
    22  const (
    23  	jsonTypeNull jsonType = iota
    24  	jsonTypeBool
    25  	jsonTypeNumber
    26  	jsonTypeString
    27  	jsonTypeArray
    28  	jsonTypeObject
    29  )
    30  
    31  func jsonTypeOf(v interface{}) jsonType {
    32  	if isNil(v) {
    33  		return jsonTypeNull
    34  	}
    35  
    36  	switch v.(type) {
    37  	case bool:
    38  		return jsonTypeBool
    39  	case float64:
    40  		return jsonTypeNumber
    41  	case string:
    42  		return jsonTypeString
    43  	case []interface{}:
    44  		return jsonTypeArray
    45  	case map[string]interface{}:
    46  		return jsonTypeObject
    47  	}
    48  	panic(fmt.Sprintf("unexpected JSON type: %T", v))
    49  }
    50  
    51  func isNil(v interface{}) bool {
    52  	if v == nil {
    53  		return true
    54  	}
    55  	rv := reflect.ValueOf(v)
    56  	return rv.Kind() == reflect.Ptr && rv.IsNil()
    57  }