const log = require('t-log');

function valueChain(data, chain, defaultVal) {
  const keys = chain.split('.');
  const l = keys.length;
  let i = 0;
  let value = data;

  try {
    for (; i < l; i++) {
      value = value[keys[i]];
    }
  } catch (e) {
    log.error(`Read value of ${chain} on '${keys[i]}' error:\n`, e.stack);
    value = defaultVal;
  }

  if (value === undefined || value === null) {
    value = defaultVal;
  }

  return value;
}
// strange! 非常奇怪，getValue函数定义不能换行，否则只要对data做了原型扩展，
// 暂不清楚，换行对headers设置有何种影响
// 在代理转发的地方(request-proxy.js)pipe(res)就会报如下错误：
// TypeError: The header content contains invalid characters
// at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:358:11)
//
// https://www.alexkras.com/typeerror-the-header-content-contains-invalid-characters/
function getValue(chain, defaultVal) { return valueChain(this, chain, defaultVal); }

valueChain.set = obj => {
  if (!obj) {
    return obj;
  }
  // 扩展数据原型，增加getValue方法
  const proto = Object.getPrototypeOf(obj);
  proto.getValue = getValue;

  return obj;
};

module.exports = valueChain;

@@ -836,24 +836,39 @@
 l) %7B return 
+valueChain.set(
 valueChain(t
@@ -890,16 +890,150 @@
 aultVal)
+); %7D%0Afunction getList(chain) %7B return getValue.call(this, chain, %5B%5D); %7D%0Afunction getMap(chain) %7B return getValue.call(this, chain, %7B%7D)
 ; %7D%0A%0Aval
@@ -1158,16 +1158,16 @@
 f(obj);%0A
-
   proto.
@@ -1186,16 +1186,68 @@
 etValue;
+%0A  proto.getList = getList;%0A  proto.getMap = getMap;
 %0A%0A  retu

update value-chain to provide new method