-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathconfig.ts
200 lines (168 loc) · 5.19 KB
/
config.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2018 The Casbin Authors. 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.
// ConfigInterface defines the behavior of a Config implementation
export interface ConfigInterface {
getString(key: string): string;
getStrings(key: string): string[];
getBool(key: string): boolean;
getInt(key: string): number;
getFloat(key: string): number;
set(key: string, value: string): void;
}
export class Config implements ConfigInterface {
private static DEFAULT_SECTION = 'default';
private static DEFAULT_COMMENT = '#';
private static DEFAULT_COMMENT_SEM = ';';
private static DEFAULT_MULTI_LINE_SEPARATOR = '\\';
private data: Map<string, Map<string, string>>;
private constructor() {
this.data = new Map<string, Map<string, string>>();
}
/**
* newConfig create an empty configuration representation from file.
*
* @param text the content of the model file.
* @return the constructor of Config.
*/
public static newConfig(text: string): Config {
const config = new Config();
config.parseText(text);
return config;
}
/**
* newConfigFromText create an empty configuration representation from text.
*
* @param text the model text.
* @return the constructor of Config.
*/
public static newConfigFromText(text: string): Config {
const config = new Config();
config.parseText(text);
return config;
}
/**
* addConfig adds a new section->key:value to the configuration.
*/
private addConfig(section: string, option: string, value: string): boolean {
if (section === '') {
section = Config.DEFAULT_SECTION;
}
const hasKey = this.data.has(section);
if (!hasKey) {
this.data.set(section, new Map<string, string>());
}
const item = this.data.get(section);
if (item) {
item.set(option, value);
return item.has(option);
} else {
return false;
}
}
private parseText(text: string): void {
const lines = text.split('\n').filter((v) => v);
const linesCount = lines.length;
let section = '';
let currentLine = '';
lines.forEach((n, index) => {
let commentPos = n.indexOf(Config.DEFAULT_COMMENT);
if (commentPos > -1) {
n = n.slice(0, commentPos);
}
commentPos = n.indexOf(Config.DEFAULT_COMMENT_SEM);
if (commentPos > -1) {
n = n.slice(0, commentPos);
}
const line = n.trim();
if (!line) {
return;
}
const lineNumber = index + 1;
if (line.startsWith('[') && line.endsWith(']')) {
if (currentLine.length !== 0) {
this.write(section, lineNumber - 1, currentLine);
currentLine = '';
}
section = line.substring(1, line.length - 1);
} else {
let shouldWrite = false;
if (line.includes(Config.DEFAULT_MULTI_LINE_SEPARATOR)) {
currentLine += line.substring(0, line.length - 1).trim();
} else {
currentLine += line;
shouldWrite = true;
}
if (shouldWrite || lineNumber === linesCount) {
this.write(section, lineNumber, currentLine);
currentLine = '';
}
}
});
}
private write(section: string, lineNum: number, line: string): void {
const equalIndex = line.indexOf('=');
if (equalIndex === -1) {
throw new Error(`parse the content error : line ${lineNum}`);
}
const key = line.substring(0, equalIndex);
const value = line.substring(equalIndex + 1);
this.addConfig(section, key.trim(), value.trim());
}
public getBool(key: string): boolean {
return !!this.get(key);
}
public getInt(key: string): number {
return Number.parseInt(this.get(key), 10);
}
public getFloat(key: string): number {
return Number.parseFloat(this.get(key));
}
public getString(key: string): string {
return this.get(key);
}
public getStrings(key: string): string[] {
const v = this.get(key);
return v.split(',');
}
public set(key: string, value: string): void {
if (!key) {
throw new Error('key is empty');
}
let section = '';
let option;
const keys = key.toLowerCase().split('::');
if (keys.length >= 2) {
section = keys[0];
option = keys[1];
} else {
option = keys[0];
}
this.addConfig(section, option, value);
}
public get(key: string): string {
let section;
let option;
const keys = key.toLowerCase().split('::');
if (keys.length >= 2) {
section = keys[0];
option = keys[1];
} else {
section = Config.DEFAULT_SECTION;
option = keys[0];
}
const item = this.data.get(section);
const itemChild = item && item.get(option);
return itemChild ? itemChild : '';
}
}