From a695ef61c51ebba9df9a8549fe523a171730d5f0 Mon Sep 17 00:00:00 2001
From: Douwe <d.geurtjens@students.uu.nl>
Date: Tue, 9 Nov 2021 10:32:40 +0100
Subject: [PATCH] Hierarchy code duplication reduction

---
 aql/hierarchy.go | 126 ++++++++++++++---------------------------------
 1 file changed, 38 insertions(+), 88 deletions(-)

diff --git a/aql/hierarchy.go b/aql/hierarchy.go
index f79d91f..a905af2 100644
--- a/aql/hierarchy.go
+++ b/aql/hierarchy.go
@@ -107,19 +107,7 @@ func RelToEnt(JSONQuery *entity.IncomingQueryJSON, rel pdict) {
 		// If our layercounter is equal to 0 we are in the first "layer" of the hierarchy
 		// Because we add the entities IN FRONT OF their respective relation we don't have to move the layercounter before prepending
 		// If our layercounter is not equal to 0 we lower the layercounter and then add each item to the newly selected layer
-		if layercounter == 0 {
-			listoflists = prepend(listoflists, newlist)
-			fmt.Println("RelToEnt Layercounter 0 prepend entity")
-
-		} else {
-			layercounter--
-			for i := range newlist {
-				listoflists[layercounter] = append(listoflists[layercounter], newlist[i])
-
-				fmt.Println("RelToEnt Layercounter " + strconv.Itoa(layercounter) + " append to layer above us, appending type: " + newlist[i].typename + " with pointer: " + strconv.Itoa(newlist[i].pointer))
-			}
-
-		}
+		listoflists = AboveAppend(listoflists, layercounter, newlist)
 
 		// After getting a list of entities we can only go towards a list of relation
 		// So we recurse by calling EntToRel
@@ -169,18 +157,7 @@ func EntToRel(JSONQuery *entity.IncomingQueryJSON, ent pdict) {
 		// Because we add the relations BEHIND their respective entities we don't have to move the layercounter before appending
 		// TODO TAKE OUT UNNEEDED LAYERCOUNTER INCREMENTS AND DECREMENTS
 		// If our layercounter is any other value we increase the layercounter and then add each item to the newly selected layer
-		if layercounter == len(listoflists)-1 {
-			listoflists = append(listoflists, newlist)
-			layercounter++
-			fmt.Println("EntToRel Layercounter last appending below: type relation")
-		} else {
-			layercounter++
-			for i := range newlist {
-				listoflists[layercounter] = append(listoflists[layercounter], newlist[i])
-				fmt.Println("EntToRel Layercounter " + strconv.Itoa(layercounter) + " append to layer below us, appending type: " + newlist[i].typename + " with pointer: " + strconv.Itoa(newlist[i].pointer))
-			}
-
-		}
+		listoflists = BelowAppend(listoflists, layercounter, newlist)
 
 		// After getting a list of relations we can only go towards a list of entities or a list of functions
 		// So we recurse by calling RelToEnt and RelToAllFunc
@@ -250,19 +227,7 @@ func RelToAllFunc(JSONQuery *entity.IncomingQueryJSON, rel pdict) {
 	layercounterthree := layercounter
 	// See main function comment to see which sublist gets put where in the hierarchy
 	if len(functowhichrelapplies) != 0 {
-
-		if layercountertwo == 0 {
-			listoflists = prepend(listoflists, functowhichrelapplies)
-			fmt.Println("RellToAllFunc Layercounter 0 prepend, prepending functowhichrelapplies")
-
-		} else {
-			layercountertwo--
-			for i := range functowhichrelapplies {
-				listoflists[layercountertwo] = append(listoflists[layercountertwo], functowhichrelapplies[i])
-				fmt.Println("RellToAllFunc Layercounter " + strconv.Itoa(layercountertwo) + " append to layer below us, appending type: " + functowhichrelapplies[i].typename + " with pointer: " + strconv.Itoa(functowhichrelapplies[i].pointer))
-			}
-
-		}
+		listoflists = AboveAppend(listoflists, layercountertwo, functowhichrelapplies)
 
 		for i := range functowhichrelapplies {
 			fmt.Println("FuncToAllRell being called with index?: " + strconv.Itoa(functowhichrelapplies[i].pointer))
@@ -273,19 +238,7 @@ func RelToAllFunc(JSONQuery *entity.IncomingQueryJSON, rel pdict) {
 
 	if len(funcappliedtosubquery) != 0 {
 		//newlayercounter := layercounter
-		if layercounterthree == len(listoflists)-1 {
-			listoflists = append(listoflists, funcappliedtosubquery)
-			layercounterthree++
-			fmt.Println("RellToAllFunc Layercounter last prepend, appending funcappliedtosubquery")
-		} else {
-			layercounterthree++
-			for i := range funcappliedtosubquery {
-				listoflists[layercounterthree] = append(listoflists[layercounterthree], funcappliedtosubquery[i])
-				fmt.Println("RellToAllFunc Layercounter " + strconv.Itoa(layercounterthree) + " append to layer below us, appending type: " + funcappliedtosubquery[i].typename + " with pointer: " + strconv.Itoa(funcappliedtosubquery[i].pointer))
-
-			}
-
-		}
+		listoflists = BelowAppend(listoflists, layercounterthree, funcappliedtosubquery)
 
 		for i := range funcappliedtosubquery {
 			fmt.Println("FuncToAllRel being called with index?: " + strconv.Itoa(funcappliedtosubquery[i].pointer))
@@ -345,19 +298,7 @@ func FuncToAllRel(JSONQuery *entity.IncomingQueryJSON, function pdict) {
 	layercounterthree := layercounter
 	if len(funcappliedtosubquery) != 0 {
 		//newlayercounter := layercounter
-		if layercountertwo == 0 {
-			listoflists = prepend(listoflists, funcappliedtosubquery)
-			fmt.Println("FuncToAllRel Layercounter 0 prepend, prepending funcappliedtosubquery")
-
-		} else {
-			layercountertwo--
-			for i := range funcappliedtosubquery {
-				listoflists[layercountertwo] = append(listoflists[layercountertwo], funcappliedtosubquery[i])
-
-				fmt.Println("FuncToAllRel Layercounter " + strconv.Itoa(layercountertwo) + " append to layer below us, appending type: " + funcappliedtosubquery[i].typename + " with pointer: " + strconv.Itoa(funcappliedtosubquery[i].pointer))
-			}
-
-		}
+		listoflists = AboveAppend(listoflists, layercountertwo, funcappliedtosubquery)
 
 		for i := range funcappliedtosubquery {
 			fmt.Println("RelToEnt being called with index?: " + strconv.Itoa(funcappliedtosubquery[i].pointer))
@@ -369,20 +310,7 @@ func FuncToAllRel(JSONQuery *entity.IncomingQueryJSON, function pdict) {
 	}
 
 	if len(relattachedtofunc) != 0 {
-
-		if layercounterthree == len(listoflists)-1 {
-			listoflists = append(listoflists, relattachedtofunc)
-			layercounterthree++
-			fmt.Println("FuncToAllRel Layercounter last append, appending relattachedtofunc")
-
-		} else {
-			layercounterthree++
-			for i := range relattachedtofunc {
-				listoflists[layercounterthree] = append(listoflists[layercounterthree], relattachedtofunc[i])
-				fmt.Println("FuncToAllRel Layercounter " + strconv.Itoa(layercounterthree) + " append to layer below us, appending type: " + relattachedtofunc[i].typename + " with pointer: " + strconv.Itoa(relattachedtofunc[i].pointer))
-			}
-
-		}
+		listoflists = BelowAppend(listoflists, layercounterthree, relattachedtofunc)
 
 		for i := range relattachedtofunc {
 			fmt.Println("RelToEnt being called with index?: " + strconv.Itoa(relattachedtofunc[i].pointer))
@@ -413,11 +341,11 @@ func AddFilters(JSONQuery *entity.IncomingQueryJSON) {
 func addOneFilter(filterPDict pdict, JSONQuery *entity.IncomingQueryJSON, p pdict, filterDone *map[int]bool) {
 	if p.typename == "filter" && (*filterDone)[p.pointer] {
 		l := FindCurrentLayer(listoflists, p)
-		k := pdictList{}
+		k := pdictList{filterPDict}
 		if len(listoflists) > l+1 && listoflists[l+1][0].typename == "filter" {
 			listoflists[l+1] = append(listoflists[l+1], filterPDict)
 		} else {
-			listoflists = BelowAppend(listoflists, l, k)
+			listoflists = FilterAppend(listoflists, l, k)
 		}
 		(*filterDone)[filterPDict.pointer] = true
 	} else if p.typename == "filter" {
@@ -431,7 +359,7 @@ func addOneFilter(filterPDict pdict, JSONQuery *entity.IncomingQueryJSON, p pdic
 		if len(listoflists) > l+1 && listoflists[l+1][0].typename == "filter" {
 			listoflists[l+1] = append(listoflists[l+1], filterPDict)
 		} else {
-			listoflists = BelowAppend(listoflists, l, k)
+			listoflists = FilterAppend(listoflists, l, k)
 		}
 		(*filterDone)[filterPDict.pointer] = true
 	} else {
@@ -440,31 +368,53 @@ func addOneFilter(filterPDict pdict, JSONQuery *entity.IncomingQueryJSON, p pdic
 		if len(listoflists) > l+1 && listoflists[l+1][0].typename == "filter" {
 			listoflists[l+1] = append(listoflists[l+1], filterPDict)
 		} else {
-			listoflists = BelowAppend(listoflists, l, k)
+			listoflists = FilterAppend(listoflists, l, k)
 		}
 		(*filterDone)[filterPDict.pointer] = true
 	}
 }
 
 // A function that appends 1 level above (if index is 0 this won't work)
-func AboveAppend(list []pdictList, index int, value pdictList) []pdictList {
+func AboveAppend(lists []pdictList, index int, values pdictList) []pdictList {
 	if index == 0 {
-		return prepend(list, value)
+		return prepend(lists, values)
+	} else {
+		index--
+		for i := range values {
+			lists[index] = append(lists[index], values[i])
+		}
+		return lists
 	}
-	return BelowAppend(list, index-1, value)
+
 }
 
 // A function that appends 1 level below
-func BelowAppend(lists []pdictList, index int, value pdictList) []pdictList {
+func BelowAppend(lists []pdictList, index int, values pdictList) []pdictList {
+	if index == len(listoflists)-1 {
+		lists = append(listoflists, values)
+		return lists
+
+	} else {
+		index++
+		for i := range values {
+			lists[index] = append(lists[index], values[i])
+		}
+		return lists
+
+	}
+}
+
+func FilterAppend(lists []pdictList, index int, values pdictList) []pdictList {
 	if len(lists)-1 == index { // nil or empty slice or after last element
-		return append(lists, value)
+		return append(lists, values)
 	}
 	k := make([]pdictList, len(lists[index+1:]))
 	copy(k, lists[index+1:])
 	l := make([]pdictList, len(lists[:index+1]))
 	copy(l, lists[:index+1])
-	lists = append(l, value) // index < len(a)
+	lists = append(l, values) // index < len(a)
 	return append(lists, k...)
+
 }
 
 // A simple double-for loop that finds the layer in which an element resides in the hierarchy
-- 
GitLab