1// Copyright 2015 Google Inc. All Rights Reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package drive 16 17import ( 18 "fmt" 19 "strconv" 20 "strings" 21) 22 23const ( 24 TBool = iota 25 TRune 26 TInt 27 TInt64 28 TUInt 29 TString 30 TStringArray 31 TUInt64 32) 33 34type resolverEmitter func(varname, v string) (interface{}, error) 35 36type typeEmitterPair struct { 37 value string 38 valueEmitter func(v string) interface{} 39} 40 41type typeResolver struct { 42 key string 43 value string 44 resolver resolverEmitter 45} 46 47type tParsed int 48 49func (t tParsed) String() string { 50 switch t { 51 case TBool: 52 return "bool" 53 case TInt: 54 return "int" 55 case TInt64: 56 return "int64" 57 case TRune: 58 return "rune" 59 case TString: 60 return "string" 61 case TStringArray: 62 return "[]string" 63 case TUInt64: 64 return "uint64" 65 default: 66 return "unknown" 67 } 68} 69 70func parseErrorer(varname string, t tParsed, value interface{}, args ...interface{}) error { 71 return fmt.Errorf("%s: got \"%v\", expected type %s %v", varname, value, t, args) 72} 73 74func _boolfer(varname, strValue string) (interface{}, error) { 75 retr, rErr := strconv.ParseBool(strValue) 76 var value bool 77 var err error 78 79 if rErr == nil { 80 value = retr 81 } else { 82 err = parseErrorer(varname, TBool, value, rErr) 83 } 84 85 return value, err 86} 87 88func _stringfer(varname, value string) (interface{}, error) { 89 // TODO: perform strips and trims 90 return value, nil 91} 92 93func _stringArrayfer(varname, value string) (interface{}, error) { 94 splits := NonEmptyTrimmedStrings(strings.Split(value, ",")...) 95 return splits, nil 96} 97 98func stringArrayfer(varname, value string, optSave *Options) error { 99 var splits []string 100 splitsInterface, err := _stringArrayfer(varname, value) 101 if err != nil { 102 return err 103 } 104 105 splits, ok := splitsInterface.([]string) 106 if !ok { 107 return fmt.Errorf("varname: %s value: %s. Failed to create []string", varname, value) 108 } 109 110 var ptr *[]string 111 112 varnameLower := strings.ToLower(varname) 113 114 switch varnameLower { 115 case ExportsKey: 116 ptr = &optSave.Exports 117 118 case ExcludeOpsKey: 119 optSave.ExcludeCrudMask = CrudAtoi(splits...) 120 return nil 121 } 122 123 if ptr != nil { 124 *ptr = splits 125 } 126 return nil 127} 128 129func _intfer(varname, strValue string) (interface{}, error) { 130 var value int 131 var err error 132 133 v64, vErr := strconv.ParseInt(strValue, 10, 32) 134 if vErr == nil { 135 value = int(v64) 136 } else { 137 err = parseErrorer(varname, TInt, value, vErr) 138 } 139 140 return value, err 141} 142