forked from odeke-em/drive
/
lex.go
141 lines (117 loc) · 2.82 KB
/
lex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed 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 drive
import (
"fmt"
"strconv"
"strings"
)
const (
TBool = iota
TRune
TInt
TInt64
TUInt
TString
TStringArray
TUInt64
)
type resolverEmitter func(varname, v string) (interface{}, error)
type typeEmitterPair struct {
value string
valueEmitter func(v string) interface{}
}
type typeResolver struct {
key string
value string
resolver resolverEmitter
}
type tParsed int
func (t tParsed) String() string {
switch t {
case TBool:
return "bool"
case TInt:
return "int"
case TInt64:
return "int64"
case TRune:
return "rune"
case TString:
return "string"
case TStringArray:
return "[]string"
case TUInt64:
return "uint64"
default:
return "unknown"
}
}
func parseErrorer(varname string, t tParsed, value interface{}, args ...interface{}) error {
return fmt.Errorf("%s: got \"%v\", expected type %s %v", varname, value, t, args)
}
func _boolfer(varname, strValue string) (interface{}, error) {
retr, rErr := strconv.ParseBool(strValue)
var value bool
var err error
if rErr == nil {
value = retr
} else {
err = parseErrorer(varname, TBool, value, rErr)
}
return value, err
}
func _stringfer(varname, value string) (interface{}, error) {
// TODO: perform strips and trims
return value, nil
}
func _stringArrayfer(varname, value string) (interface{}, error) {
splits := NonEmptyTrimmedStrings(strings.Split(value, ",")...)
return splits, nil
}
func stringArrayfer(varname, value string, optSave *Options) error {
var splits []string
splitsInterface, err := _stringArrayfer(varname, value)
if err != nil {
return err
}
splits, ok := splitsInterface.([]string)
if !ok {
return fmt.Errorf("varname: %s value: %s. Failed to create []string", varname, value)
}
var ptr *[]string
varnameLower := strings.ToLower(varname)
switch varnameLower {
case ExportsKey:
ptr = &optSave.Exports
case ExcludeOpsKey:
optSave.ExcludeCrudMask = CrudAtoi(splits...)
return nil
}
if ptr != nil {
*ptr = splits
}
return nil
}
func _intfer(varname, strValue string) (interface{}, error) {
var value int
var err error
v64, vErr := strconv.ParseInt(strValue, 10, 32)
if vErr == nil {
value = int(v64)
} else {
err = parseErrorer(varname, TInt, value, vErr)
}
return value, err
}