zeroshade commented on code in PR #344: URL: https://github.com/apache/arrow-go/pull/344#discussion_r2064330741
########## parquet/variants/util.go: ########## @@ -0,0 +1,154 @@ +// 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 variants + +import ( + "fmt" + "io" + "reflect" + "time" +) + +// Reads a little-endian encoded uint (betwen 1 and 8 bytes wide) from a raw buffer at a specified +// offset and returns its value. If any part of the read would be out of bounds, this returns an error. +func readUint(raw []byte, offset, size int) (uint64, error) { + if size < 1 || size > 8 { + return 0, fmt.Errorf("invalid size, must be in range [1,8]: %d", size) + } + if maxPos := offset + size; maxPos > len(raw) { + return 0, fmt.Errorf("out of bounds: trying to access position %d, max position is %d", maxPos, len(raw)) + } + var ret uint64 + for i := range size { + ret |= uint64(raw[i+offset]) << (8 * i) + } + return ret, nil +} Review Comment: Then you could probably just do something like: ```go switch size { case 1: var val uint8 _, err := binary.Decode(raw[offset:size], binary.LittleEndian, &val) return val, err case 2: var val uint16 _, err := binary.Decode(raw[offset:size], binary.LittleEndian, &val) return val, err case 4: var val uint32 _, err := binary.Decode(raw[offset:size], binary.LittleEndian, &val) return val, err case 8: var val uint64 _, err := binary.Decode(raw[offset:size], binary.LittleEndian, &val) return val, err } ``` right? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org