// Licensed to the Apache Software Foundation (ASF) under one// or more contributor license agreements. See the NOTICE file// distributed with this work for additional information// regarding copyright ownership. The ASF licenses this file// to you under the Apache License, Version 2.0 (the// "License"); you may not use this file except in compliance// with the License. You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing,// software distributed under the License is distributed on an// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY// KIND, either express or implied. See the License for the// specific language governing permissions and limitations// under the License.package variantimport ()// pathElem is one step of a VariantPath: an object field when isField is set, else an array index.type pathElem struct { name string index int isField bool}// VariantPath is an ordered list of steps to navigate into a variant value. The// zero value is the root path; extend it with Field and Index.typeVariantPathstruct { elems []pathElem}// Field returns a copy of the path with an object-field step appended.func ( VariantPath) ( string) VariantPath {returnVariantPath{elems: append(.grow(), pathElem{name: , isField: true})}}// Index returns a copy of the path with an array-index step appended.func ( VariantPath) ( int) VariantPath {returnVariantPath{elems: append(.grow(), pathElem{index: })}}// Join returns a copy of the path with other's steps appended.func ( VariantPath) ( VariantPath) VariantPath {returnVariantPath{elems: append(.grow(), .elems...)}}func ( VariantPath) () []pathElem {returnappend(make([]pathElem, 0, len(.elems)+1), .elems...)}// Len returns the number of steps in the path.func ( VariantPath) () int { returnlen(.elems) }// StepAt returns the i-th step's name and index, with isField true for an object-field step.func ( VariantPath) ( int) ( string, int, bool) { := .elems[]return .name, .index, .isField}// GetByPath navigates path into v and returns the leaf value. found is false when// the path is cleanly absent (a missing object field, or an out-of-range or// non-array index). It returns an error for a type error (a field step into a// non-object) or corrupt data (a field id not present in the metadata).func ( Value) ( VariantPath) ( Value, bool, error) { := for , := range .elems {if .isField { , := .Value().(ObjectValue)if ! {returnValue{}, false, fmt.Errorf("%w: variant path field %q applied to non-object", arrow.ErrInvalid, .name) } , := .ValueByKey(.name)if != nil {iferrors.Is(, arrow.ErrNotFound) {returnValue{}, false, nil }returnValue{}, false, } = .Valuecontinue } , := .Value().(ArrayValue)if ! {returnValue{}, false, nil }if .index < 0 || uint64(.index) >= uint64(.Len()) {returnValue{}, false, nil } , := .Value(uint32(.index))if != nil {returnValue{}, false, nil } = }return , true, nil}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.