Home Download Docs Code Community
     1	/*
     2	Copyright 2015 The Perkeep Authors
     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 conv contains utilities for parsing values delimited by spaces.
    18	package conv // import "perkeep.org/pkg/conv"
    19	
    20	import (
    21		"bytes"
    22		"errors"
    23		"fmt"
    24	
    25		"perkeep.org/pkg/blob"
    26	
    27		"go4.org/strutil"
    28	)
    29	
    30	func ParseFields(v []byte, dst ...interface{}) error {
    31		for i, dv := range dst {
    32			thisv := v
    33			if i < len(dst)-1 {
    34				sp := bytes.IndexByte(v, ' ')
    35				if sp == -1 {
    36					return fmt.Errorf("missing space following field index %d", i)
    37				}
    38				thisv = v[:sp]
    39				v = v[sp+1:]
    40			}
    41			switch dv := dv.(type) {
    42			case *blob.Ref:
    43				br, ok := blob.ParseBytes(thisv)
    44				if !ok {
    45	
    46				}
    47				*dv = br
    48			case *uint32:
    49				n, err := strutil.ParseUintBytes(thisv, 10, 32)
    50				if err != nil {
    51					return err
    52				}
    53				*dv = uint32(n)
    54			case *uint64:
    55				n, err := strutil.ParseUintBytes(thisv, 10, 64)
    56				if err != nil {
    57					return err
    58				}
    59				*dv = n
    60			case *int64:
    61				n, err := strutil.ParseUintBytes(thisv, 10, 64)
    62				if err != nil {
    63					return err
    64				}
    65				if int64(n) < 0 {
    66					return errors.New("conv: negative numbers not accepted with int64 dest type")
    67				}
    68				*dv = int64(n)
    69			default:
    70				return fmt.Errorf("conv: unsupported target pointer type %T", dv)
    71			}
    72		}
    73		return nil
    74	}
Website layout inspired by memcached.
Content by the authors.