1 /*************************************************************************
3 * Copyright 2020 highstreet technologies GmbH and others
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ***************************************************************************/
20 #include "type_utils.h"
21 #include "log_utils.h"
26 const char* typeutils_yang_type_to_str(const LY_DATA_TYPE type_base) {
33 return "LY_TYPE_BINARY";
37 return "LY_TYPE_BITS";
41 return "LY_TYPE_BOOL";
45 return "LY_TYPE_DEC64";
49 return "LY_TYPE_EMPTY";
53 return "LY_TYPE_ENUM";
57 return "LY_TYPE_IDENT";
61 return "LY_TYPE_INST";
65 return "LY_TYPE_LEAFREF";
69 return "LY_TYPE_STRING";
73 return "LY_TYPE_UNION";
77 return "LY_TYPE_INT8";
81 return "LY_TYPE_UINT8";
85 return "LY_TYPE_INT16";
89 return "LY_TYPE_UINT16";
93 return "LY_TYPE_INT32";
97 return "LY_TYPE_UINT32";
101 return "LY_TYPE_INT64";
105 return "LY_TYPE_UINT64";
108 case LY_TYPE_UNKNOWN:
109 return "LY_TYPE_UNKNOWN";
113 return "(unknown LY_TYPE)";
116 const char* typeutils_yang_nodetype_to_str(const LYS_NODE nodetype) {
119 return "LYS_UNKNOWN";
123 return "LYS_CONTAINER";
135 return "LYS_LEAFLIST";
167 return "LYS_GROUPING";
175 return "LYS_AUGMENT";
183 return "LYS_ANYDATA";
191 return "(unknown node type)";
197 char* typeutils_type_to_str(const struct lys_type *type) {
200 struct lys_ident *ref = 0;
205 if(type->info.enums.count) {
206 ret = (char*)realloc(ret, sizeof(char) * 1024 * 1024);
208 log_error("bad malloc\n");
212 sprintf(ret, "enum(%d):", type->info.enums.count);
213 for(int i = 0; i < type->info.enums.count; i++) {
215 asprintf(&add, " %s(%d)", type->info.enums.enm[i].name, type->info.enums.enm[i].value);
220 ret = (char*)realloc(ret, sizeof(char) * (strlen(ret) + 1)); //resize back
224 char *add = typeutils_type_to_str(&type->der->type);
225 if(type->der->module) {
226 asprintf(&ret, "%s:%s >>> %s", type->der->module->name, type->der->name, add);
229 asprintf(&ret, "%s >>> %s", type->der->name, add);
237 if(type->info.ident.count) {
238 ref = type->info.ident.ref[0];
241 asprintf(&ret, "ident: %s:%s", ref->module->name, ref->name);
244 asprintf(&ret, "ident: %s", ref->name);
248 else if(type->der->module) {
249 return typeutils_type_to_str(&type->der->type);
255 if(type->der->module) {
256 asprintf(&ret, "union: %s:%s", type->der->module->name, type->der->name);
259 asprintf(&ret, "union: %s", type->der->name);
265 if(type->info.str.length) {
266 asprintf(&ret, "%s:%s", type->der->name, type->info.str.length->expr);
269 asprintf(&ret, "%s", type->der->name);
275 if(type->der->module) {
276 asprintf(&ret, "%s:%s", type->der->module->name, type->der->name);
279 asprintf(&ret, "%s", type->der->name);