Skip to content

Commit 846bdf9

Browse files
committed
SO CLOSE (:
1 parent c09cf89 commit 846bdf9

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

translate.go

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ func deepMerge(dst, src map[string]interface{}) map[string]interface{} {
927927
// translatedInput = the parent object to modify. It is actually the parent of the parents' object.
928928
// jsonKey = the key in the parent object WHICH IS A LIST
929929
// parsedValues = the child values from where we have the list
930-
func handleMultiListItems(translatedInput []interface{}, parentKey string, parsedValues map[string]interface{}, listDepth int) ([]interface{}) {
930+
func handleMultiListItems(translatedInput []interface{}, parentKey string, parsedValues map[string]interface{}, listDepth, childIndex int) ([]interface{}) {
931931
if !strings.Contains(parentKey, ".") && len(translatedInput) == 0 {
932932
translatedInput = append(translatedInput, parsedValues)
933933
}
@@ -939,31 +939,29 @@ func handleMultiListItems(translatedInput []interface{}, parentKey string, parse
939939
// strings -> build it out.
940940
for childKey, v := range parsedValues {
941941
if val, ok := v.(map[string]interface{}); ok {
942-
log.Printf("PARENT Map: %s", childKey)
943-
944942
// By passing in translatedInput we allow child objects to modify the parent?
945943
newKey := fmt.Sprintf("%s.%s", parentKey, childKey)
946-
translatedInput = handleMultiListItems(translatedInput, newKey, val, listDepth)
944+
translatedInput = handleMultiListItems(translatedInput, newKey, val, listDepth, childIndex)
947945

948946
} else if val, ok := v.([]interface{}); ok {
949947
log.Printf("\n\nPARENT LIST: %s\n\n", childKey)
950948

951949
newKey := fmt.Sprintf("%s.%s.#", parentKey, childKey)
950+
_ = newKey
951+
_ = val
952952

953-
// TranslatedInput: handles the OUTER layer properly
954-
// val: handles the INNER layer properly
955-
// Problem: Both are wrong
956-
for _, subItem := range val {
957-
958-
translatedInput = handleMultiListItems(translatedInput, newKey, subItem.(map[string]interface{}), listDepth+1)
959-
960-
//val := handleMultiListItems(val, "newKey", subItem.(map[string]interface{}))
961-
//log.Printf("New val KEY: %#v (%d): %#v", newKey, cnt, val)
953+
// FIXME: Re-enable this for sub-lists
954+
for cnt, subItem := range val {
955+
translatedInput = handleMultiListItems(translatedInput, newKey, subItem.(map[string]interface{}), listDepth+1, cnt)
962956
}
963957

964958

965959
} else if val, ok := v.(string); ok {
966960
if strings.Contains(val, "schemaless_list[") {
961+
log.Printf("CONTAINS: %#v!!\n\n\n", val)
962+
963+
//if val == "schemaless_list[]" || val == "schemaless_list" {
964+
//}
967965

968966
foundList := strings.Split(val, "schemaless_list")
969967
if len(foundList) >= 2 {
@@ -982,8 +980,6 @@ func handleMultiListItems(translatedInput []interface{}, parentKey string, parse
982980
newParentKey := ""
983981
modificationList := translatedInput
984982
if listDepth > 0 {
985-
log.Printf("\n\n\n\n\nHandle listdepth '%d' from key %s\n\n\n\n", listDepth, parentKey)
986-
987983
parentKeySplit := strings.Split(parentKey, ".")
988984

989985
counter := 0
@@ -1023,31 +1019,42 @@ func handleMultiListItems(translatedInput []interface{}, parentKey string, parse
10231019
newKey := fmt.Sprintf("%s.%s", parentKey, childKey)
10241020
newKey = strings.SplitN(newKey, ".", 2)[1]
10251021

1026-
cntItem := modificationList[cnt].(map[string]interface{})
1027-
modificationList[cnt], found = setNestedMap(cntItem, newKey, listValue)
1022+
//cntItem := modificationList[cnt].(map[string]interface{})
1023+
1024+
log.Printf("VALUE: %#v", listValue)
1025+
//modificationList[cnt], found = setNestedMap(cntItem, newKey, listValue)
1026+
modificationList[cnt], found = setNestedMap(modificationList[cnt].(map[string]interface{}), newKey, listValue)
10281027
if !found {
1029-
log.Printf("[ERROR] Schemaless: Could not set nested map for key '%s' with value '%s'", newKey, listValue)
1028+
log.Printf("[ERROR] Schemaless: Could not set nested map for key '%s' with value '%s'. Count: %#v", newKey, listValue, cnt)
10301029
}
1031-
10321030
}
10331031

10341032
if listDepth > 0 {
1035-
// Updates the parent here too
1036-
parsedValues = modificationList[0].(map[string]interface{})
1033+
// Updates the child & here
1034+
// This shit also needs recursion.. gahh
1035+
1036+
log.Printf("Updating CHILD INDEX %#v", childIndex)
1037+
parsedValues = modificationList[childIndex].(map[string]interface{})
10371038

1038-
// FIXME: Problem here is it could be multiple recursion parents.
10391039
// And it needs to automatically find the right one
1040-
oldParentKey = fmt.Sprintf("cve.cvssloop")
1040+
if strings.HasPrefix(oldParentKey, ".") {
1041+
oldParentKey = strings.Trim(oldParentKey, ".")
1042+
}
1043+
//oldParentKey = fmt.Sprintf("cve.cvssloop.#")
10411044
log.Printf("KEY TO PUT IN: %#v. Value: %s", oldParentKey, modificationList)
1042-
translatedInput[0], found = setNestedMap(translatedInput[0].(map[string]interface{}), oldParentKey, modificationList)
1043-
if !found {
1044-
log.Printf("[ERROR] Schemaless (2): Could not set nested map for key '%s' with value '%s'", oldParentKey, modificationList)
1045+
for inputKey, _ := range translatedInput {
1046+
translatedInput[inputKey], found = setNestedMap(translatedInput[inputKey].(map[string]interface{}), oldParentKey, modificationList)
1047+
if found {
1048+
log.Printf("\n\n\nBREAKING - FOUND WHERE TO PUT IT!")
1049+
break
1050+
}
10451051
}
1046-
1052+
} else {
1053+
translatedInput = modificationList
10471054
}
10481055

10491056
marshalled, _ := json.MarshalIndent(translatedInput, "", "\t")
1050-
log.Printf("MARSHALLED (%d): %s. Parsed values: %#v", listDepth, string(marshalled), parsedValues)
1057+
log.Printf("MARSHALLED (%d): %s.", listDepth, string(marshalled))
10511058

10521059
//translatedInput = modificationList
10531060

@@ -1081,8 +1088,6 @@ func runJsonTranslation(ctx context.Context, inputValue []byte, translation map[
10811088
// Creating a new map to store the translated values
10821089
translatedInput := make(map[string]interface{})
10831090
for translationKey, translationValue := range translation {
1084-
log.Printf("[DEBUG] Schemaless: Translating key '%s' with value '%v'. ", translationKey, translationValue)
1085-
10861091
// Find the field in the parsedInput
10871092
found := false
10881093
for inputKey, inputValue:= range parsedInput {
@@ -1154,7 +1159,7 @@ func runJsonTranslation(ctx context.Context, inputValue []byte, translation map[
11541159
// Hard to optimise for subkeys -> parent control tho
11551160
if strings.Contains(string(output), "schemaless_list[") {
11561161
//newTranslatedInput := handleMultiListItems(newOutput, translationKey, outputParsed)
1157-
newTranslatedInput := handleMultiListItems(newOutput, "#", outputParsed, 0)
1162+
newTranslatedInput := handleMultiListItems(newOutput, "", outputParsed, 0, 0)
11581163
translationValue = newTranslatedInput
11591164

11601165
newOutput = []interface{}{}
@@ -1167,7 +1172,10 @@ func runJsonTranslation(ctx context.Context, inputValue []byte, translation map[
11671172

11681173
translatedInput[translationKey] = newOutput
11691174
} else {
1170-
log.Printf("[ERROR] Schemaless: No output found for key '%s' after translation. Keeping original value.", translationKey)
1175+
if debug {
1176+
log.Printf("[ERROR] Schemaless DEBUG issue: No output found for key '%s' after translation. This COULD be working as intended.", translationKey)
1177+
}
1178+
11711179
translatedInput[translationKey] = translationValue
11721180
}
11731181

0 commit comments

Comments
 (0)