mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-05-05 06:37:10 -04:00
Added tree.
This commit is contained in:
parent
81a9ef924f
commit
f9fd9805c0
2 changed files with 515 additions and 472 deletions
|
@ -29,6 +29,7 @@
|
|||
|
||||
import {MapArrayType} from "../types/MapType";
|
||||
import {SettingsNode, SettingsTree} from "./SettingsTree";
|
||||
import {coerce} from "semver";
|
||||
|
||||
const absolutePaths = require('./AbsolutePaths');
|
||||
const deepEqual = require('fast-deep-equal/es6');
|
||||
|
@ -53,10 +54,12 @@ const nonSettings = [
|
|||
|
||||
// This is a function to make it easy to create a new instance. It is important to not reuse a
|
||||
// config object after passing it to log4js.configure() because that method mutates the object. :(
|
||||
const defaultLogConfig = (level:string) => ({appenders: {console: {type: 'console'}},
|
||||
const defaultLogConfig = (level: string) => ({
|
||||
appenders: {console: {type: 'console'}},
|
||||
categories: {
|
||||
default: {appenders: ['console'], level},
|
||||
}});
|
||||
}
|
||||
});
|
||||
const defaultLogLevel = 'INFO';
|
||||
|
||||
const initLogging = (config: any) => {
|
||||
|
@ -557,11 +560,16 @@ const coerceValue = (stringValue:string) => {
|
|||
}
|
||||
|
||||
switch (stringValue) {
|
||||
case 'true': return true;
|
||||
case 'false': return false;
|
||||
case 'undefined': return undefined;
|
||||
case 'null': return null;
|
||||
default: return stringValue;
|
||||
case 'true':
|
||||
return true;
|
||||
case 'false':
|
||||
return false;
|
||||
case 'undefined':
|
||||
return undefined;
|
||||
case 'null':
|
||||
return null;
|
||||
default:
|
||||
return stringValue;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -715,21 +723,12 @@ const lookupEnvironmentVariables = (obj: MapArrayType<any>) => {
|
|||
currentNode.addChild(pathToKey, value!)
|
||||
})
|
||||
|
||||
console.log("Root is", JSON.stringify(root, (k,v)=>{
|
||||
if(v instanceof Map) {
|
||||
return {
|
||||
dataType: 'Map',
|
||||
value: Array.from(v.entries()), // or with spread: value: [...value]
|
||||
};
|
||||
} else {
|
||||
return v;
|
||||
}
|
||||
}))
|
||||
|
||||
console.log(root.collectFromLeafsUpwards())
|
||||
obj = Object.assign(obj, envVars)
|
||||
return obj;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* - reads the JSON configuration file settingsFilename from disk
|
||||
* - strips the comments
|
||||
|
@ -864,7 +863,8 @@ exports.reloadSettings = () => {
|
|||
try {
|
||||
exports.sessionKey = fs.readFileSync(sessionkeyFilename, 'utf8');
|
||||
logger.info(`Session key loaded from: ${sessionkeyFilename}`);
|
||||
} catch (err) { /* ignored */ }
|
||||
} catch (err) { /* ignored */
|
||||
}
|
||||
const keyRotationEnabled = exports.cookie.keyRotationInterval && exports.cookie.sessionLifetime;
|
||||
if (!exports.sessionKey && !keyRotationEnabled) {
|
||||
logger.info(
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import {MapArrayType} from "../types/MapType";
|
||||
|
||||
export class SettingsTree {
|
||||
private children: Map<string, SettingsNode>;
|
||||
constructor() {
|
||||
|
@ -24,46 +26,87 @@ export class SettingsTree {
|
|||
|
||||
export class SettingsNode {
|
||||
private readonly key: string;
|
||||
private value: string|undefined;
|
||||
private children: Map<string, SettingsNode>;
|
||||
private value: string | number | boolean | null | undefined;
|
||||
private children: MapArrayType<SettingsNode>;
|
||||
|
||||
constructor(key: string, value?: string) {
|
||||
constructor(key: string, value?: string | number | boolean | null | undefined) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
this.children = new Map();
|
||||
this.children = {}
|
||||
}
|
||||
|
||||
public addChild(key: string[], value?: string) {
|
||||
let depth = 0
|
||||
|
||||
while (depth < key.length) {
|
||||
const k = key[depth];
|
||||
const slicedKey = key.slice(depth + 1)
|
||||
depth++;
|
||||
if(this.key === k) {
|
||||
console.log("same key")
|
||||
public addChild(path: string[], value: string) {
|
||||
let currentNode:SettingsNode = this;
|
||||
for (let i = 0; i < path.length; i++) {
|
||||
const key = path[i];
|
||||
/*
|
||||
Skip the current node if the key is the same as the current node's key
|
||||
*/
|
||||
if (key === this.key ) {
|
||||
continue
|
||||
}
|
||||
if (this.children.has(k)) {
|
||||
console.log("has child", k)
|
||||
this.children.get(k)!.addChild(slicedKey, value);
|
||||
/*
|
||||
If the current node does not have a child with the key, create a new node with the key
|
||||
*/
|
||||
if (!currentNode.hasChild(key)) {
|
||||
currentNode = currentNode.children[key] = new SettingsNode(key, this.coerceValue(value));
|
||||
} else {
|
||||
const newNode = new SettingsNode(k);
|
||||
this.children.set(k, newNode);
|
||||
if(slicedKey.length > 0)
|
||||
newNode.addChild(slicedKey, value);
|
||||
else
|
||||
newNode.value = value;
|
||||
this.children.get(k)!.addChild(slicedKey, undefined);
|
||||
/*
|
||||
Else move to the child node
|
||||
*/
|
||||
currentNode = currentNode.getChild(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public collectFromLeafsUpwards() {
|
||||
let collected:MapArrayType<any> = {};
|
||||
for (const key in this.children) {
|
||||
const child = this.children[key];
|
||||
if (child.hasChildren()) {
|
||||
collected[key] = child.collectFromLeafsUpwards();
|
||||
} else {
|
||||
collected[key] = child.value;
|
||||
}
|
||||
}
|
||||
return collected;
|
||||
}
|
||||
|
||||
coerceValue = (stringValue: string) => {
|
||||
// cooked from https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number
|
||||
// @ts-ignore
|
||||
const isNumeric = !isNaN(stringValue) && !isNaN(parseFloat(stringValue) && isFinite(stringValue));
|
||||
|
||||
if (isNumeric) {
|
||||
// detected numeric string. Coerce to a number
|
||||
|
||||
return +stringValue;
|
||||
}
|
||||
|
||||
switch (stringValue) {
|
||||
case 'true':
|
||||
return true;
|
||||
case 'false':
|
||||
return false;
|
||||
case 'undefined':
|
||||
return undefined;
|
||||
case 'null':
|
||||
return null;
|
||||
default:
|
||||
return stringValue;
|
||||
}
|
||||
};
|
||||
|
||||
public hasChildren() {
|
||||
return Object.keys(this.children).length > 0;
|
||||
}
|
||||
|
||||
public getChild(key: string) {
|
||||
return this.children.get(key);
|
||||
return this.children[key];
|
||||
}
|
||||
|
||||
public hasChild(key: string) {
|
||||
return this.children.has(key);
|
||||
return this.children[key] !== undefined;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue