Skip to content

Commit

Permalink
fix: support comments after expression
Browse files Browse the repository at this point in the history
  • Loading branch information
nodece committed May 20, 2020
1 parent 5984ba5 commit c97cb26
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
22 changes: 18 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { readFileSync } from 'fs';
import { removeComments } from './util';

// 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;
}

Expand Down Expand Up @@ -92,15 +98,23 @@ export class Config implements ConfigInterface {
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();
const lineNumber = index + 1;
if (!line) {
return;
}

if (line.startsWith(Config.DEFAULT_COMMENT) || line.startsWith(Config.DEFAULT_COMMENT_SEM)) {
return;
} else if (line.startsWith('[') && line.endsWith(']')) {
const lineNumber = index + 1;

if (line.startsWith('[') && line.endsWith(']')) {
if (currentLine.length !== 0) {
this.write(section, lineNumber - 1, currentLine);
currentLine = '';
Expand Down
4 changes: 2 additions & 2 deletions src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ export class Model {
value = value.replace(n, `$<${index}>`);
});

value = util.removeComments(util.escapeAssertion(value));
value = util.escapeAssertion(value);

stringArguments.forEach((n, index) => {
value = value.replace(`$<${index}>`, n);
});

ast.value = value;
} else {
ast.value = util.removeComments(util.escapeAssertion(value));
ast.value = util.escapeAssertion(value);
}

const nodeMap = this.model.get(sec);
Expand Down
7 changes: 7 additions & 0 deletions test/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ describe('multi-line test', () => {
it('should config.get("multi5::name") to equal r.sub==p.sub&&r.obj==p.obj', function() {
expect(config.get('multi5::name')).toEqual('r.sub==p.sub&&r.obj==p.obj');
});

it('should config.get("mysql::mysql.master.host") to equal 10.0.0.1', function() {
expect(config.get('mysql::mysql.master.host')).toEqual('10.0.0.1');
});
it('should config.get("mysql::mysql.master.user") to equal root', function() {
expect(config.get('mysql::mysql.master.user')).toEqual('root');
});
});
4 changes: 2 additions & 2 deletions test/config/testini.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ mysql.dev.user = root
mysql.dev.pass = 123456
mysql.dev.db = test

mysql.master.host = 10.0.0.1
mysql.master.user = root
mysql.master.host = 10.0.0.1 # host # 10.0.0.1
mysql.master.user = root ; user name
mysql.master.pass = 89dds)2$#d
mysql.master.db = act

Expand Down

0 comments on commit c97cb26

Please sign in to comment.