storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/s3select/internal/parquet-go/data/jsonvalue.go (about) 1 /* 2 * Minio Cloud Storage, (C) 2019 Minio, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package data 18 19 import ( 20 "fmt" 21 22 "github.com/tidwall/gjson" 23 24 "storj.io/minio/pkg/s3select/internal/parquet-go/gen-go/parquet" 25 ) 26 27 type jsonValue struct { 28 result *gjson.Result 29 path *string 30 } 31 32 func (v *jsonValue) String() string { 33 if v.result == nil { 34 return "<nil>" 35 } 36 37 return fmt.Sprintf("%v", *v.result) 38 } 39 40 func (v *jsonValue) IsNull() bool { 41 return v.result == nil || v.result.Type == gjson.Null 42 } 43 44 func (v *jsonValue) Get(path string) *jsonValue { 45 if v.path != nil { 46 var result *gjson.Result 47 if *v.path == path { 48 result = v.result 49 } 50 51 return resultToJSONValue(result) 52 } 53 54 if v.result == nil { 55 return resultToJSONValue(nil) 56 } 57 58 result := v.result.Get(path) 59 if !result.Exists() { 60 return resultToJSONValue(nil) 61 } 62 63 return resultToJSONValue(&result) 64 } 65 66 func (v *jsonValue) GetValue(parquetType parquet.Type, convertedType *parquet.ConvertedType) (interface{}, error) { 67 if v.result == nil { 68 return nil, nil 69 } 70 71 return resultToParquetValue(*v.result, parquetType, convertedType) 72 } 73 74 func (v *jsonValue) GetArray() ([]gjson.Result, error) { 75 if v.result == nil { 76 return nil, nil 77 } 78 79 return resultToArray(*v.result) 80 } 81 82 func (v *jsonValue) Range(iterator func(key, value gjson.Result) bool) error { 83 if v.result == nil || v.result.Type == gjson.Null { 84 return nil 85 } 86 87 if v.result.Type != gjson.JSON || !v.result.IsObject() { 88 return fmt.Errorf("result is not Map but %v", v.result.Type) 89 } 90 91 v.result.ForEach(iterator) 92 return nil 93 } 94 95 func resultToJSONValue(result *gjson.Result) *jsonValue { 96 return &jsonValue{ 97 result: result, 98 } 99 } 100 101 func bytesToJSONValue(data []byte) (*jsonValue, error) { 102 if !gjson.ValidBytes(data) { 103 return nil, fmt.Errorf("invalid JSON data") 104 } 105 106 result := gjson.ParseBytes(data) 107 return resultToJSONValue(&result), nil 108 }