tree schema BUGFIX correct predicate order in lysc_path (#2244)

* tree schema BUGFIX correct predicate order in lysc_path

* correct uncrustify errors

* test non-empty path after predicates

---------

Co-authored-by: Alexandre Snarskii <snar@retn.net>
diff --git a/src/tree_schema.c b/src/tree_schema.c
index f7dac07..1dd9319 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -715,22 +715,32 @@
         }
 
         if ((pathtype == LYSC_PATH_DATA_PATTERN) && (iter->nodetype == LYS_LIST)) {
+            char *predicates = NULL;
+
             key = NULL;
             while ((key = lys_getnext(key, iter, NULL, 0)) && lysc_is_key(key)) {
-                s = buffer ? strdup(buffer) : path;
+                s = predicates;
 
                 /* print key predicate */
-                if (buffer) {
-                    len = snprintf(buffer, buflen, "%s[%s='%%s']", s ? s : "", key->name);
-                } else {
-                    len = asprintf(&path, "%s[%s='%%s']", s ? s : "", key->name);
+                asprintf(&predicates, "%s[%s='%%s']", s ? s : "", key->name);
+                if (s) {
+                    free(s);
                 }
-                free(s);
+            }
+            s = buffer ? strdup(buffer) : path;
+            if (buffer) {
+                len = snprintf(buffer, buflen, "%s%s", predicates ? predicates : "", s ? s : "");
+            } else {
+                len = asprintf(&path, "%s%s", predicates ? predicates : "", s ? s : "");
+            }
+            if (predicates) {
+                free(predicates);
+            }
+            free(s);
 
-                if (buffer && (buflen <= (size_t)len)) {
-                    /* not enough space in buffer */
-                    break;
-                }
+            if (buffer && (buflen <= (size_t)len)) {
+                /* not enough space in buffer */
+                break;
             }
         }