config.c 14.5 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define _DEFAULT_SOURCE
#include "cfgInt.h"

SConfig *cfgInit() {
  SConfig *pConfig = calloc(1, sizeof(SConfig));
S
cfgtest  
Shengliang Guan 已提交
21 22 23 24 25 26 27 28 29 30 31 32
  if (pConfig == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  pConfig->hash = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
  if (pConfig->hash == NULL) {
    free(pConfig);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
33 34 35
  return pConfig;
}

S
Shengliang Guan 已提交
36
int32_t cfgLoad(SConfig *pConfig, ECfgSrcType cfgType, const char *sourceStr) {
S
Shengliang Guan 已提交
37
  switch (cfgType) {
S
config  
Shengliang Guan 已提交
38
    case CFG_STYPE_CFG_FILE:
S
Shengliang Guan 已提交
39
      return cfgLoadFromCfgFile(pConfig, sourceStr);
S
config  
Shengliang Guan 已提交
40
    case CFG_STYPE_ENV_FILE:
S
Shengliang Guan 已提交
41
      return cfgLoadFromEnvFile(pConfig, sourceStr);
S
config  
Shengliang Guan 已提交
42
    case CFG_STYPE_ENV_VAR:
S
Shengliang Guan 已提交
43
      return cfgLoadFromEnvVar(pConfig);
S
config  
Shengliang Guan 已提交
44
    case CFG_STYPE_APOLLO_URL:
S
Shengliang Guan 已提交
45 46 47 48 49 50
      return cfgLoadFromApollUrl(pConfig, sourceStr);
    default:
      return -1;
  }
}

S
Shengliang Guan 已提交
51
void cfgCleanup(SConfig *pConfig) {
S
Shengliang Guan 已提交
52 53 54 55 56 57
  if (pConfig != NULL) {
    if (pConfig->hash != NULL) {
      taosHashCleanup(pConfig->hash);
      pConfig->hash == NULL;
    }
    free(pConfig);
S
Shengliang Guan 已提交
58 59 60 61 62 63 64 65 66
  }
}

int32_t cfgGetSize(SConfig *pConfig) { return taosHashGetSize(pConfig->hash); }

SConfigItem *cfgIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashIterate(pConfig->hash, pIter); }

void cfgCancelIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashCancelIterate(pConfig->hash, pIter); }

S
config  
Shengliang Guan 已提交
67
static int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
68 69 70 71 72 73 74
  bool tmp = false;
  if (strcasecmp(value, "true") == 0) {
    tmp = true;
  }
  if (atoi(value) > 0) {
    tmp = true;
  }
S
config  
Shengliang Guan 已提交
75

S
config  
Shengliang Guan 已提交
76 77 78 79 80
  pItem->boolVal = tmp;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
81
static int32_t cfgSetInt8(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
82 83 84 85 86 87 88 89
  int8_t ival = (int8_t)atoi(value);
  if (ival < pItem->minIntVal || ival > pItem->maxIntVal) {
    uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d",
           pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal,
           cfgStypeStr(pItem->stype), pItem->int8Val);
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
90

S
config  
Shengliang Guan 已提交
91 92 93 94 95
  pItem->int8Val = ival;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
96
static int32_t cfgSetUInt16(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
97 98 99 100 101 102 103 104
  uint16_t ival = (uint16_t)atoi(value);
  if (ival < pItem->minIntVal || ival > pItem->maxIntVal) {
    uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d",
           pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal,
           cfgStypeStr(pItem->stype), pItem->uint16Val);
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
105

S
config  
Shengliang Guan 已提交
106 107 108 109 110
  pItem->uint16Val = ival;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
111
static int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
112 113 114 115 116 117 118 119
  int32_t ival = (int32_t)atoi(value);
  if (ival < pItem->minIntVal || ival > pItem->maxIntVal) {
    uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d",
           pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal,
           cfgStypeStr(pItem->stype), pItem->int32Val);
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
120

S
config  
Shengliang Guan 已提交
121 122 123 124 125
  pItem->int32Val = ival;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
126
static int32_t cfgSetInt64(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
127 128
  int64_t ival = (int64_t)atoi(value);
  if (ival < pItem->minIntVal || ival > pItem->maxIntVal) {
S
config  
Shengliang Guan 已提交
129 130
    uError("cfg:%s, type:%s src:%s value:%" PRId64 " out of range[%" PRId64 ", %" PRId64
           "], use last src:%s value:%" PRId64,
S
config  
Shengliang Guan 已提交
131 132 133 134 135
           pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal,
           cfgStypeStr(pItem->stype), pItem->int64Val);
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
136

S
config  
Shengliang Guan 已提交
137 138 139 140 141
  pItem->int64Val = ival;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
142
static int32_t cfgSetFloat(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
143 144 145 146 147 148 149 150
  float fval = (float)atof(value);
  if (fval < pItem->minFloatVal || fval > pItem->maxFloatVal) {
    uError("cfg:%s, type:%s src:%s value:%f out of range[%f, %f], use last src:%s value:%f", pItem->name,
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), fval, pItem->minFloatVal, pItem->maxFloatVal,
           cfgStypeStr(pItem->stype), pItem->floatVal);
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
151

S
config  
Shengliang Guan 已提交
152 153 154 155 156
  pItem->floatVal = fval;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
157
static int32_t cfgSetString(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
158 159 160 161
  char *tmp = strdup(value);
  if (tmp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s, use last src:%s value:%s", pItem->name,
S
config  
Shengliang Guan 已提交
162
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal);
S
config  
Shengliang Guan 已提交
163 164
    return -1;
  }
S
config  
Shengliang Guan 已提交
165

S
config  
Shengliang Guan 已提交
166 167 168 169 170 171
  free(pItem->strVal);
  pItem->strVal = tmp;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
172
static int32_t cfgSetIpStr(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
173 174 175 176
  char *tmp = strdup(value);
  if (tmp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s, use last src:%s value:%s", pItem->name,
S
config  
Shengliang Guan 已提交
177
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal);
S
config  
Shengliang Guan 已提交
178 179
    return -1;
  }
S
config  
Shengliang Guan 已提交
180

S
config  
Shengliang Guan 已提交
181 182 183 184 185 186
  free(pItem->strVal);
  pItem->strVal = tmp;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
187
static int32_t cfgSetDir(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
188 189 190 191
  char *tmp = strdup(value);
  if (tmp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s, use last src:%s value:%s", pItem->name,
S
config  
Shengliang Guan 已提交
192
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal);
S
config  
Shengliang Guan 已提交
193 194
    return -1;
  }
S
config  
Shengliang Guan 已提交
195

S
config  
Shengliang Guan 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  free(pItem->strVal);
  pItem->strVal = tmp;
  pItem->stype = stype;
  return 0;
}

int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSrcType stype) {
  SConfigItem *pItem = cfgGetItem(pConfig, name);
  if (pItem == NULL) {
    return -1;
  }

  switch (pItem->dtype) {
    case CFG_DTYPE_BOOL:
      return cfgSetBool(pItem, value, stype);
    case CFG_DTYPE_INT8:
      return cfgSetInt8(pItem, value, stype);
    case CFG_DTYPE_UINT16:
      return cfgSetUInt16(pItem, value, stype);
    case CFG_DTYPE_INT32:
      return cfgSetInt32(pItem, value, stype);
    case CFG_DTYPE_INT64:
      return cfgSetInt64(pItem, value, stype);
    case CFG_DTYPE_FLOAT:
      return cfgSetFloat(pItem, value, stype);
    case CFG_DTYPE_STRING:
      return cfgSetString(pItem, value, stype);
    case CFG_DTYPE_IPSTR:
      return cfgSetIpStr(pItem, value, stype);
    case CFG_DTYPE_DIR:
S
config  
Shengliang Guan 已提交
226
      return cfgSetDir(pItem, value, stype);
S
config  
Shengliang Guan 已提交
227 228 229 230 231 232 233 234 235 236
    case CFG_DTYPE_NONE:
    default:
      break;
  }

  terrno = TSDB_CODE_INVALID_CFG;
  return -1;
}

SConfigItem *cfgGetItem(SConfig *pConfig, const char *name) {
S
config  
Shengliang Guan 已提交
237 238 239
  char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0};
  memcpy(lowcaseName, name, CFG_NAME_MAX_LEN);
  strntolower(lowcaseName, name, CFG_NAME_MAX_LEN);
S
config  
Shengliang Guan 已提交
240 241 242 243 244 245 246 247

  SConfigItem *pItem = taosHashGet(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1);
  if (pItem == NULL) {
    terrno = TSDB_CODE_CFG_NOT_FOUND;
  }

  return pItem;
}
S
Shengliang Guan 已提交
248 249

static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name, ECfgUnitType utype) {
S
config  
Shengliang Guan 已提交
250
  pItem->stype = CFG_STYPE_DEFAULT;
S
Shengliang Guan 已提交
251 252
  pItem->utype = utype;
  pItem->name = strdup(name);
S
Shengliang Guan 已提交
253
  if (pItem->name == NULL) {
S
Shengliang Guan 已提交
254 255 256 257
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
config  
Shengliang Guan 已提交
258 259 260 261
  char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0};
  memcpy(lowcaseName, name, CFG_NAME_MAX_LEN);
  strntolower(lowcaseName, name, CFG_NAME_MAX_LEN);

S
config  
Shengliang Guan 已提交
262
  if (taosHashPut(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1, pItem, sizeof(SConfigItem)) != 0) {
S
cfgtest  
Shengliang Guan 已提交
263 264 265 266 267 268 269 270
    if (pItem->dtype == CFG_DTYPE_STRING) {
      free(pItem->strVal);
    } else if (pItem->dtype == CFG_DTYPE_IPSTR) {
      free(pItem->ipstrVal);
    } else if (pItem->dtype == CFG_DTYPE_DIR) {
      free(pItem->dirVal);
    }
    free(pItem->name);
S
Shengliang Guan 已提交
271 272 273 274 275 276 277 278 279 280 281 282
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

int32_t cfgAddBool(SConfig *pConfig, const char *name, bool defaultVal, ECfgUnitType utype) {
  SConfigItem item = {.dtype = CFG_DTYPE_BOOL, .boolVal = defaultVal};
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
283 284 285 286 287 288
int32_t cfgAddInt8(SConfig *pConfig, const char *name, int8_t defaultVal, int64_t minval, int64_t maxval,
                   ECfgUnitType utype) {
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
289

S
config  
Shengliang Guan 已提交
290
  SConfigItem item = {.dtype = CFG_DTYPE_INT8, .int8Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
S
Shengliang Guan 已提交
291 292 293
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
294 295 296 297 298 299
int32_t cfgAddUInt16(SConfig *pConfig, const char *name, uint16_t defaultVal, int64_t minval, int64_t maxval,
                     ECfgUnitType utype) {
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
300

S
config  
Shengliang Guan 已提交
301
  SConfigItem item = {.dtype = CFG_DTYPE_UINT16, .uint16Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
S
Shengliang Guan 已提交
302 303 304
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
305 306 307 308 309 310
int32_t cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval,
                    ECfgUnitType utype) {
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
311

S
config  
Shengliang Guan 已提交
312
  SConfigItem item = {.dtype = CFG_DTYPE_INT32, .int32Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
S
Shengliang Guan 已提交
313 314 315
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
316 317 318 319 320 321
int32_t cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval,
                    ECfgUnitType utype) {
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
322

S
config  
Shengliang Guan 已提交
323
  SConfigItem item = {.dtype = CFG_DTYPE_INT64, .int64Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
S
Shengliang Guan 已提交
324 325 326
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
327 328 329 330 331 332
int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, double minval, double maxval,
                    ECfgUnitType utype) {
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
333

S
config  
Shengliang Guan 已提交
334
  SConfigItem item = {.dtype = CFG_DTYPE_FLOAT, .floatVal = defaultVal, .minFloatVal = minval, .maxFloatVal = maxval};
S
Shengliang Guan 已提交
335 336
  return cfgAddItem(pConfig, &item, name, utype);
}
S
Shengliang Guan 已提交
337

S
Shengliang Guan 已提交
338
int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
S
config  
Shengliang Guan 已提交
339 340 341 342 343
  if (defaultVal == NULL) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }

S
Shengliang Guan 已提交
344 345
  SConfigItem item = {.dtype = CFG_DTYPE_STRING};
  item.strVal = strdup(defaultVal);
S
Shengliang Guan 已提交
346
  if (item.strVal == NULL) {
S
Shengliang Guan 已提交
347 348 349 350 351
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return cfgAddItem(pConfig, &item, name, utype);
}
S
Shengliang Guan 已提交
352

S
config  
Shengliang Guan 已提交
353 354 355 356
static int32_t cfgCheckIpStr(const char *ip) {
  uint32_t value = taosInetAddr(ip);
  if (value == INADDR_NONE) {
    uError("ip:%s is not a valid ip address", ip);
S
Shengliang Guan 已提交
357 358
    return -1;
  }
S
config  
Shengliang Guan 已提交
359 360

  return 0;
S
Shengliang Guan 已提交
361 362 363
}

int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
S
config  
Shengliang Guan 已提交
364 365 366 367 368
  if (cfgCheckIpStr(defaultVal) != 0) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }

S
Shengliang Guan 已提交
369 370
  SConfigItem item = {.dtype = CFG_DTYPE_IPSTR};
  item.ipstrVal = strdup(defaultVal);
S
Shengliang Guan 已提交
371
  if (item.ipstrVal == NULL) {
S
Shengliang Guan 已提交
372 373 374 375 376 377
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) {
  char fullDir[PATH_MAX] = {0};
  if (taosExpandDir(inputDir, fullDir, PATH_MAX) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to expand dir:%s since %s", inputDir, terrstr());
    return -1;
  }

  if (taosRealPath(fullDir, PATH_MAX) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to get realpath of dir:%s since %s", inputDir, terrstr());
    return -1;
  }

  if (taosMkDir(fullDir) != 0) {
    uError("failed to create dir:%s realpath:%s since %s", inputDir, fullDir, terrstr());
    return -1;
  }

  tfree(pItem->dirVal);
  pItem->dirVal = strdup(fullDir);
  if (pItem->dirVal == NULL) {
S
Shengliang Guan 已提交
400 401 402
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
config  
Shengliang Guan 已提交
403 404

  return 0;
S
Shengliang Guan 已提交
405 406
}

S
config  
Shengliang Guan 已提交
407 408 409
int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
  SConfigItem item = {.dtype = CFG_DTYPE_DIR};
  if (cfgCheckAndSetDir(&item, defaultVal) != 0) {
S
Shengliang Guan 已提交
410 411 412 413
    return -1;
  }
  return cfgAddItem(pConfig, &item, name, utype);
}
S
Shengliang Guan 已提交
414 415 416

const char *cfgStypeStr(ECfgSrcType type) {
  switch (type) {
S
config  
Shengliang Guan 已提交
417
    case CFG_STYPE_DEFAULT:
S
Shengliang Guan 已提交
418
      return "default";
S
config  
Shengliang Guan 已提交
419
    case CFG_STYPE_CFG_FILE:
S
Shengliang Guan 已提交
420
      return "cfg_file";
S
config  
Shengliang Guan 已提交
421
    case CFG_STYPE_ENV_FILE:
S
Shengliang Guan 已提交
422
      return "env_file";
S
config  
Shengliang Guan 已提交
423
    case CFG_STYPE_ENV_VAR:
S
Shengliang Guan 已提交
424
      return "env_var";
S
config  
Shengliang Guan 已提交
425
    case CFG_STYPE_APOLLO_URL:
S
Shengliang Guan 已提交
426
      return "apollo_url";
S
config  
Shengliang Guan 已提交
427 428 429 430
    case CFG_STYPE_ARG_LIST:
      return "arg_list";
    case CFG_STYPE_API_OPTION:
      return "api_option";
S
Shengliang Guan 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    default:
      return "invalid";
  }
}

const char *cfgDtypeStr(ECfgDataType type) {
  switch (type) {
    case CFG_DTYPE_NONE:
      return "none";
    case CFG_DTYPE_BOOL:
      return "bool";
    case CFG_DTYPE_INT8:
      return "int8";
    case CFG_DTYPE_UINT16:
      return "uint16";
    case CFG_DTYPE_INT32:
      return "int32";
    case CFG_DTYPE_INT64:
      return "int64";
    case CFG_DTYPE_FLOAT:
      return "float";
    case CFG_DTYPE_STRING:
      return "string";
    case CFG_DTYPE_IPSTR:
      return "ipstr";
    case CFG_DTYPE_DIR:
      return "dir";
    default:
      return "invalid";
  }
}

const char *cfgUtypeStr(ECfgUnitType type) {
  switch (type) {
    case CFG_UTYPE_NONE:
      return "";
    case CFG_UTYPE_PERCENT:
      return "(%)";
    case CFG_UTYPE_GB:
      return "(GB)";
    case CFG_UTYPE_MB:
      return "(Mb)";
    case CFG_UTYPE_BYTE:
      return "(byte)";
    case CFG_UTYPE_SECOND:
      return "(s)";
    case CFG_UTYPE_MS:
      return "(ms)";
    default:
      return "invalid";
  }
}