Skip to content

Commit f7ef4d6

Browse files
committed
Multiple new recursion fixes made for loops in loops
1 parent b104cd2 commit f7ef4d6

File tree

1 file changed

+81
-19
lines changed

1 file changed

+81
-19
lines changed

translate.go

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ func handleMultiListItems(translatedInput []interface{}, parentKey string, parse
957957
if parsedSubitem, ok := subItem.(map[string]interface{}); ok {
958958
translatedInput = handleMultiListItems(translatedInput, newKey, parsedSubitem, listDepth+1, cnt)
959959
} else {
960-
log.Printf("[ERROR] Schemaless: List item '%s' in key '%s' is not a map[string]interface{}, but %T. This is not handled yet.", childKey, parentKey, subItem)
960+
log.Printf("[ERROR] Schemaless: List item '%s' in key '%s' is not a map[string]interface{}, but %T. This is not handled yet (1).", childKey, parentKey, subItem)
961961
}
962962
}
963963

@@ -1009,7 +1009,7 @@ func handleMultiListItems(translatedInput []interface{}, parentKey string, parse
10091009

10101010
}
10111011

1012-
// Reference Item
1012+
// Reference Item in the child list
10131013
firstItem := modificationList[0]
10141014
var found bool
10151015
for cnt, listValue := range unmarshalledList {
@@ -1027,7 +1027,11 @@ func handleMultiListItems(translatedInput []interface{}, parentKey string, parse
10271027
// log.Printf("Setting value %#v for key '%s' in modificationList[%d]", listValue, newKey, cnt)
10281028
//}
10291029

1030-
modificationList[cnt], found = setNestedMap(modificationList[cnt].(map[string]interface{}), newKey, listValue)
1030+
log.Printf("Listvalue to put in index '%d': %#v", childIndex, listValue)
1031+
newModList := modificationList[cnt].(map[string]interface{})
1032+
1033+
newModList, found = setNestedMap(newModList, newKey, listValue)
1034+
modificationList[cnt] = newModList
10311035
if !found {
10321036
if debug {
10331037
log.Printf("[ERROR] Schemaless: Could not set nested map for key '%s' with value '%s'. Count: %#v", newKey, listValue, cnt)
@@ -1038,41 +1042,71 @@ func handleMultiListItems(translatedInput []interface{}, parentKey string, parse
10381042
if listDepth > 0 {
10391043
// Updates the child & here
10401044
// This shit also needs recursion.. gahh
1041-
10421045
if debug {
1043-
log.Printf("Updating CHILD LOOP INDEX %#v", childIndex)
1046+
log.Printf("UPDATING CHILD LOOP INDEX %#v", childIndex)
10441047
}
10451048

1046-
parsedValues = modificationList[childIndex].(map[string]interface{})
10471049

10481050
// And it needs to automatically find the right one
10491051
if strings.HasPrefix(oldParentKey, ".") {
10501052
oldParentKey = strings.Trim(oldParentKey, ".")
10511053
}
1052-
//oldParentKey = fmt.Sprintf("cve.cvssloop.#")
10531054

10541055
if debug {
1055-
log.Printf("[DEBUG] Potential LOOP problem: KEY TO PUT IN: %#v. Value: %s", oldParentKey, modificationList)
1056+
log.Printf("[DEBUG] Potential LOOP issue: KEY TO PUT IN: %#v. '%#v' -> %#v", oldParentKey, parsedValues, modificationList)
10561057
}
10571058

1059+
updated := false
10581060
for inputKey, _ := range translatedInput {
1059-
translatedInput[inputKey], found = setNestedMap(translatedInput[inputKey].(map[string]interface{}), oldParentKey, modificationList)
1060-
if found {
1061-
break
1061+
//FIXME: Need to NOT update the index unless there is a schemaless_list[] in the key in question
1062+
newMap := translatedInput[inputKey].(map[string]interface{})
1063+
1064+
// This part makes it show the first one ONLY for some reason
1065+
// Without it, it shows ONLY the last one...
1066+
1067+
// FIXME: Re-add this for outer looping. Gotta fix inner now.
1068+
marshalledMap, err := json.Marshal(newMap)
1069+
if err == nil {
1070+
comparisonString := fmt.Sprintf(`"%s":"schemaless_list[`, childKey)
1071+
if !strings.Contains(string(marshalledMap), comparisonString) {
1072+
log.Printf("HANDLED ALREADY: %#v", string(marshalledMap))
1073+
continue
1074+
}
1075+
}
1076+
1077+
newMap, _ = setNestedMap(newMap, oldParentKey, modificationList)
1078+
translatedInput[inputKey] = newMap
1079+
updated = true
1080+
1081+
//parsedValues = newMap
1082+
1083+
break
1084+
}
1085+
1086+
if !updated {
1087+
if debug {
1088+
log.Printf("[DEBUG] Appended new index to parent list as it wasn't found, but needed.")
10621089
}
1090+
1091+
translatedInput = append(translatedInput, map[string]interface{}{})
1092+
newMap := translatedInput[len(translatedInput)-1].(map[string]interface{})
1093+
newMap, found = setNestedMap(newMap, oldParentKey, modificationList)
1094+
translatedInput[len(translatedInput)-1] = newMap
10631095
}
1096+
1097+
parsedValues = modificationList[childIndex].(map[string]interface{})
1098+
10641099
} else {
10651100
translatedInput = modificationList
10661101
}
10671102

1068-
//marshalled, _ := json.MarshalIndent(translatedInput, "", "\t")
1069-
//log.Printf("MARSHALLED (%d): %s.", listDepth, string(marshalled))
1070-
1103+
marshalled, _ := json.MarshalIndent(translatedInput, "", "\t")
1104+
log.Printf("MARSHALLED (%d): %s.", listDepth, string(marshalled))
10711105
//translatedInput = modificationList
10721106

10731107
}
10741108
} else {
1075-
log.Printf("[ERROR] Schemaless: String value '%s' found in key '%s', but not a schemaless_list. This is not handled yet.", val, childKey)
1109+
log.Printf("[ERROR] Schemaless: String value '%s' found in key '%s', but not a schemaless_list. This is not handled yet (2).", val, childKey)
10761110
}
10771111
} else {
10781112
log.Printf("OTHER: %s (UNHANDLED)", childKey)
@@ -1130,12 +1164,39 @@ func runJsonTranslation(ctx context.Context, inputValue []byte, translation map[
11301164
// The list part here doesn't really work as this is checking the length of the list in the STANDARD - NOT in the value from the user
11311165
// This makes it so that the append really does... nothing
11321166
for _, v := range val {
1133-
if v, ok := v.(map[string]interface{}); !ok {
1134-
log.Printf("[ERROR] Schemaless: Parsed input value is not a map[string]interface{} for key '%s': %v. Type: %#v", translationKey, v, reflect.TypeOf(v))
1135-
newOutput = append(newOutput, v)
1136-
continue
1167+
if _, ok := v.(map[string]interface{}); !ok {
1168+
1169+
if stringVal, stringValOk := v.(string); stringValOk {
1170+
stringKey := stringVal
1171+
if strings.Contains(stringKey, "[]") {
1172+
stringKey = strings.ReplaceAll(stringKey, "[]", ".#")
1173+
}
1174+
1175+
if strings.Contains(stringKey, `"`) {
1176+
stringKey = strings.ReplaceAll(stringKey, `"`, "")
1177+
}
1178+
1179+
recursed, err := recurseFindKey(parsedInput, stringKey, 0)
1180+
if err == nil {
1181+
translationValue = []string{recursed}
1182+
} else {
1183+
log.Printf("[ERROR] Schemaless: Error in RecurseFindKey for key string '%s': %v", translationKey, err)
1184+
translationValue = []string{}
1185+
1186+
//modifiedParsedInput[translationKey] = []string{}
1187+
//translatedInput[translationKey] = []string{}
1188+
}
1189+
1190+
continue
1191+
} else {
1192+
log.Printf("[ERROR] Schemaless: Parsed input value is not a map[string]interface{} for key '%s': %v. Type: %#v", translationKey, v, reflect.TypeOf(v))
1193+
newOutput = append(newOutput, v)
1194+
continue
1195+
}
11371196
}
11381197

1198+
v = v.(map[string]interface{})
1199+
11391200
// If the value is a map[string]interface{}, we need to recurse it
11401201
newValue := make(map[string]interface{})
11411202
marshalled, err := json.Marshal(v)
@@ -1150,6 +1211,7 @@ func runJsonTranslation(ctx context.Context, inputValue []byte, translation map[
11501211
continue
11511212
}
11521213

1214+
// Runs an actual translation
11531215
output, _, err := runJsonTranslation(ctx, inputValue, newValue)
11541216
if err != nil {
11551217
log.Printf("[ERROR] Schemaless: Error in runJsonTranslation for key '%s': %v", translationKey, err)

0 commit comments

Comments
 (0)