github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/columns_dynamic.go (about) 1 // Copyright (C) 2019-2021 Zilliz. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance 4 // with the License. You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software distributed under the License 9 // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 10 // or implied. See the License for the specific language governing permissions and limitations under the License. 11 12 package entity 13 14 import ( 15 "github.com/cockroachdb/errors" 16 "github.com/tidwall/gjson" 17 ) 18 19 // ColumnDynamic is a logically wrapper for dynamic json field with provided output field. 20 type ColumnDynamic struct { 21 *ColumnJSONBytes 22 outputField string 23 } 24 25 func NewColumnDynamic(column *ColumnJSONBytes, outputField string) *ColumnDynamic { 26 return &ColumnDynamic{ 27 ColumnJSONBytes: column, 28 outputField: outputField, 29 } 30 } 31 32 func (c *ColumnDynamic) Name() string { 33 return c.outputField 34 } 35 36 // Get returns element at idx as interface{}. 37 // Overrides internal json column behavior, returns raw json data. 38 func (c *ColumnDynamic) Get(idx int) (interface{}, error) { 39 bs, err := c.ColumnJSONBytes.ValueByIdx(idx) 40 if err != nil { 41 return 0, err 42 } 43 r := gjson.GetBytes(bs, c.outputField) 44 if !r.Exists() { 45 return 0, errors.New("column not has value") 46 } 47 return r.Raw, nil 48 } 49 50 func (c *ColumnDynamic) GetAsInt64(idx int) (int64, error) { 51 bs, err := c.ColumnJSONBytes.ValueByIdx(idx) 52 if err != nil { 53 return 0, err 54 } 55 r := gjson.GetBytes(bs, c.outputField) 56 if !r.Exists() { 57 return 0, errors.New("column not has value") 58 } 59 if r.Type != gjson.Number { 60 return 0, errors.New("column not int") 61 } 62 return r.Int(), nil 63 } 64 65 func (c *ColumnDynamic) GetAsString(idx int) (string, error) { 66 bs, err := c.ColumnJSONBytes.ValueByIdx(idx) 67 if err != nil { 68 return "", err 69 } 70 r := gjson.GetBytes(bs, c.outputField) 71 if !r.Exists() { 72 return "", errors.New("column not has value") 73 } 74 if r.Type != gjson.String { 75 return "", errors.New("column not string") 76 } 77 return r.String(), nil 78 } 79 80 func (c *ColumnDynamic) GetAsBool(idx int) (bool, error) { 81 bs, err := c.ColumnJSONBytes.ValueByIdx(idx) 82 if err != nil { 83 return false, err 84 } 85 r := gjson.GetBytes(bs, c.outputField) 86 if !r.Exists() { 87 return false, errors.New("column not has value") 88 } 89 if !r.IsBool() { 90 return false, errors.New("column not string") 91 } 92 return r.Bool(), nil 93 } 94 95 func (c *ColumnDynamic) GetAsDouble(idx int) (float64, error) { 96 bs, err := c.ColumnJSONBytes.ValueByIdx(idx) 97 if err != nil { 98 return 0, err 99 } 100 r := gjson.GetBytes(bs, c.outputField) 101 if !r.Exists() { 102 return 0, errors.New("column not has value") 103 } 104 if r.Type != gjson.Number { 105 return 0, errors.New("column not string") 106 } 107 return r.Float(), nil 108 }