github.com/weaviate/weaviate@v1.24.6/adapters/handlers/graphql/common/json_number.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package common 13 14 import ( 15 "encoding/json" 16 "fmt" 17 18 "github.com/tailor-inc/graphql" 19 "github.com/weaviate/weaviate/entities/aggregation" 20 ) 21 22 // JSONNumberResolver turns json.Number types into number types usable by graphQL 23 func JSONNumberResolver(p graphql.ResolveParams) (interface{}, error) { 24 switch v := p.Source.(type) { 25 case map[string]interface{}: 26 field, ok := v[p.Info.FieldName] 27 if !ok { 28 return nil, nil 29 } 30 31 switch n := field.(type) { 32 case json.Number: 33 return n.Float64() 34 case int64: 35 return float64(n), nil 36 case int: 37 return float64(n), nil 38 case float64: 39 return n, nil 40 } 41 42 return nil, fmt.Errorf("unknown number type for %t", field) 43 44 case map[string]float64: 45 return v[p.Info.FieldName], nil 46 47 case aggregation.Text: 48 switch p.Info.FieldName { 49 // case "count": 50 // // TODO gh-974: Support Count in text aggregations 51 // return nil, nil 52 53 default: 54 return nil, fmt.Errorf("fieldName '%s' does not match text aggregation", p.Info.FieldName) 55 } 56 57 default: 58 return nil, fmt.Errorf("json number resolver: unusable type %T", p.Source) 59 } 60 }