mirror of
				https://github.com/taigrr/yq
				synced 2025-01-18 04:53:17 -08:00 
			
		
		
		
	fixed tests for write array splat
This commit is contained in:
		
							parent
							
								
									53289366a5
								
							
						
					
					
						commit
						323089eb64
					
				| @ -806,6 +806,46 @@ func TestWriteCmd_SplatArray(t *testing.T) { | |||||||
| 	assertResult(t, expectedOutput, result.Output) | 	assertResult(t, expectedOutput, result.Output) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func TestWriteCmd_SplatMap(t *testing.T) { | ||||||
|  | 	content := `b: | ||||||
|  |   c: thing | ||||||
|  |   d: another thing | ||||||
|  | ` | ||||||
|  | 	filename := writeTempYamlFile(content) | ||||||
|  | 	defer removeTempYamlFile(filename) | ||||||
|  | 
 | ||||||
|  | 	cmd := getRootCommand() | ||||||
|  | 	result := runCmd(cmd, fmt.Sprintf("write -v %s b.* new", filename)) | ||||||
|  | 	if result.Error != nil { | ||||||
|  | 		t.Error(result.Error) | ||||||
|  | 	} | ||||||
|  | 	expectedOutput := `b: | ||||||
|  |   c: new | ||||||
|  |   d: new | ||||||
|  | ` | ||||||
|  | 	assertResult(t, expectedOutput, result.Output) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func TestWriteCmd_SplatMapEmpty(t *testing.T) { | ||||||
|  | 	content := `b: | ||||||
|  |   c: thing | ||||||
|  |   d: another thing | ||||||
|  | ` | ||||||
|  | 	filename := writeTempYamlFile(content) | ||||||
|  | 	defer removeTempYamlFile(filename) | ||||||
|  | 
 | ||||||
|  | 	cmd := getRootCommand() | ||||||
|  | 	result := runCmd(cmd, fmt.Sprintf("write -v %s b.c.* new", filename)) | ||||||
|  | 	if result.Error != nil { | ||||||
|  | 		t.Error(result.Error) | ||||||
|  | 	} | ||||||
|  | 	expectedOutput := `b: | ||||||
|  |   c: thing | ||||||
|  |   d: another thing | ||||||
|  | ` | ||||||
|  | 	assertResult(t, expectedOutput, result.Output) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func TestDeleteYaml(t *testing.T) { | func TestDeleteYaml(t *testing.T) { | ||||||
| 	content := `a: 2 | 	content := `a: 2 | ||||||
| b: | b: | ||||||
|  | |||||||
| @ -8,14 +8,15 @@ import ( | |||||||
| 	yaml "gopkg.in/mikefarah/yaml.v2" | 	yaml "gopkg.in/mikefarah/yaml.v2" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func entryInSlice(context yaml.MapSlice, key interface{}) *yaml.MapItem { | func entriesInSlice(context yaml.MapSlice, key interface{}) []*yaml.MapItem { | ||||||
|  | 	var matches = make([]*yaml.MapItem, 0) | ||||||
| 	for idx := range context { | 	for idx := range context { | ||||||
| 		var entry = &context[idx] | 		var entry = &context[idx] | ||||||
| 		if fmt.Sprintf("%v", entry.Key) == key { | 		if key == "*" || fmt.Sprintf("%v", entry.Key) == key { | ||||||
| 			return entry | 			matches = append(matches, entry) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return matches | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func getMapSlice(context interface{}) yaml.MapSlice { | func getMapSlice(context interface{}) yaml.MapSlice { | ||||||
| @ -41,27 +42,33 @@ func getArray(context interface{}) (array []interface{}, ok bool) { | |||||||
| 	return | 	return | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func writeMap(context interface{}, paths []string, value interface{}) yaml.MapSlice { | func writeMap(context interface{}, paths []string, value interface{}) interface{} { | ||||||
| 	log.Debugf("writeMap with path %v for %v to set value %v\n", paths, context, value) | 	log.Debugf("writeMap with path %v for %v to set value %v\n", paths, context, value) | ||||||
| 
 | 
 | ||||||
| 	mapSlice := getMapSlice(context) | 	mapSlice := getMapSlice(context) | ||||||
| 
 | 
 | ||||||
| 	if len(paths) == 0 { | 	if len(paths) == 0 { | ||||||
| 		return mapSlice | 		return context | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	child := entryInSlice(mapSlice, paths[0]) | 	children := entriesInSlice(mapSlice, paths[0]) | ||||||
| 	if child == nil { | 
 | ||||||
|  | 	if len(children) == 0 && paths[0] == "*" { | ||||||
|  | 		log.Debugf("\tNo matches, return map as is") | ||||||
|  | 		return context | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if len(children) == 0 { | ||||||
| 		newChild := yaml.MapItem{Key: paths[0]} | 		newChild := yaml.MapItem{Key: paths[0]} | ||||||
| 		mapSlice = append(mapSlice, newChild) | 		mapSlice = append(mapSlice, newChild) | ||||||
| 		child = entryInSlice(mapSlice, paths[0]) | 		children = entriesInSlice(mapSlice, paths[0]) | ||||||
| 		log.Debugf("\tAppended child at %v for mapSlice %v\n", paths[0], mapSlice) | 		log.Debugf("\tAppended child at %v for mapSlice %v\n", paths[0], mapSlice) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	log.Debugf("\tchild.Value %v\n", child.Value) |  | ||||||
| 
 |  | ||||||
| 	remainingPaths := paths[1:] | 	remainingPaths := paths[1:] | ||||||
| 	child.Value = updatedChildValue(child.Value, remainingPaths, value) | 	for _, child := range children { | ||||||
|  | 		child.Value = updatedChildValue(child.Value, remainingPaths, value) | ||||||
|  | 	} | ||||||
| 	log.Debugf("\tReturning mapSlice %v\n", mapSlice) | 	log.Debugf("\tReturning mapSlice %v\n", mapSlice) | ||||||
| 	return mapSlice | 	return mapSlice | ||||||
| } | } | ||||||
| @ -131,13 +138,24 @@ func readMap(context yaml.MapSlice, head string, tail []string) (interface{}, er | |||||||
| 	if head == "*" { | 	if head == "*" { | ||||||
| 		return readMapSplat(context, tail) | 		return readMapSplat(context, tail) | ||||||
| 	} | 	} | ||||||
| 	var value interface{} |  | ||||||
| 
 | 
 | ||||||
| 	entry := entryInSlice(context, head) | 	entries := entriesInSlice(context, head) | ||||||
| 	if entry != nil { | 	if len(entries) == 1 { | ||||||
| 		value = entry.Value | 		return calculateValue(entries[0].Value, tail) | ||||||
|  | 	} else if len(entries) == 0 { | ||||||
|  | 		return nil, nil | ||||||
| 	} | 	} | ||||||
| 	return calculateValue(value, tail) | 	var errInIdx error | ||||||
|  | 	values := make([]interface{}, len(entries)) | ||||||
|  | 	for idx, entry := range entries { | ||||||
|  | 		values[idx], errInIdx = calculateValue(entry.Value, tail) | ||||||
|  | 		if errInIdx != nil { | ||||||
|  | 			log.Errorf("Error updating index %v in %v", idx, context) | ||||||
|  | 			return nil, errInIdx | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 	} | ||||||
|  | 	return values, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func readMapSplat(context yaml.MapSlice, tail []string) (interface{}, error) { | func readMapSplat(context yaml.MapSlice, tail []string) (interface{}, error) { | ||||||
|  | |||||||
| @ -4,8 +4,6 @@ import ( | |||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"sort" | 	"sort" | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 |  | ||||||
| 	yaml "gopkg.in/mikefarah/yaml.v2" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestReadMap_simple(t *testing.T) { | func TestReadMap_simple(t *testing.T) { | ||||||
| @ -189,8 +187,7 @@ func TestWrite_really_simple(t *testing.T) { | |||||||
| `) | `) | ||||||
| 
 | 
 | ||||||
| 	updated := writeMap(data, []string{"b"}, "4") | 	updated := writeMap(data, []string{"b"}, "4") | ||||||
| 	b := entryInSlice(updated, "b").Value | 	assertResult(t, "[{b 4}]", fmt.Sprintf("%v", updated)) | ||||||
| 	assertResult(t, "4", b) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestWrite_simple(t *testing.T) { | func TestWrite_simple(t *testing.T) { | ||||||
| @ -200,9 +197,7 @@ b: | |||||||
| `) | `) | ||||||
| 
 | 
 | ||||||
| 	updated := writeMap(data, []string{"b", "c"}, "4") | 	updated := writeMap(data, []string{"b", "c"}, "4") | ||||||
| 	b := entryInSlice(updated, "b").Value.(yaml.MapSlice) | 	assertResult(t, "[{b [{c 4}]}]", fmt.Sprintf("%v", updated)) | ||||||
| 	c := entryInSlice(b, "c").Value |  | ||||||
| 	assertResult(t, "4", c) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestWrite_new(t *testing.T) { | func TestWrite_new(t *testing.T) { | ||||||
| @ -212,9 +207,7 @@ b: | |||||||
| `) | `) | ||||||
| 
 | 
 | ||||||
| 	updated := writeMap(data, []string{"b", "d"}, "4") | 	updated := writeMap(data, []string{"b", "d"}, "4") | ||||||
| 	b := entryInSlice(updated, "b").Value.(yaml.MapSlice) | 	assertResult(t, "[{b [{c 2} {d 4}]}]", fmt.Sprintf("%v", updated)) | ||||||
| 	d := entryInSlice(b, "d").Value |  | ||||||
| 	assertResult(t, "4", d) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestWrite_new_deep(t *testing.T) { | func TestWrite_new_deep(t *testing.T) { | ||||||
| @ -224,8 +217,7 @@ b: | |||||||
| `) | `) | ||||||
| 
 | 
 | ||||||
| 	updated := writeMap(data, []string{"b", "d", "f"}, "4") | 	updated := writeMap(data, []string{"b", "d", "f"}, "4") | ||||||
| 	got, _ := readMap(updated, "b", []string{"d", "f"}) | 	assertResult(t, "[{b [{c 2} {d [{f 4}]}]}]", fmt.Sprintf("%v", updated)) | ||||||
| 	assertResult(t, "4", got) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestWrite_array(t *testing.T) { | func TestWrite_array(t *testing.T) { | ||||||
| @ -236,8 +228,7 @@ b: | |||||||
| 
 | 
 | ||||||
| 	updated := writeMap(data, []string{"b", "0"}, "bb") | 	updated := writeMap(data, []string{"b", "0"}, "bb") | ||||||
| 
 | 
 | ||||||
| 	b := entryInSlice(updated, "b").Value.([]interface{}) | 	assertResult(t, "[{b [bb]}]", fmt.Sprintf("%v", updated)) | ||||||
| 	assertResult(t, "bb", b[0].(string)) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestWrite_new_array(t *testing.T) { | func TestWrite_new_array(t *testing.T) { | ||||||
| @ -247,8 +238,7 @@ b: | |||||||
| `) | `) | ||||||
| 
 | 
 | ||||||
| 	updated := writeMap(data, []string{"b", "0"}, "4") | 	updated := writeMap(data, []string{"b", "0"}, "4") | ||||||
| 	got, _ := readMap(updated, "b", []string{"0"}) | 	assertResult(t, "[{b [{c 2} {0 4}]}]", fmt.Sprintf("%v", updated)) | ||||||
| 	assertResult(t, "4", got) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestWrite_new_array_deep(t *testing.T) { | func TestWrite_new_array_deep(t *testing.T) { | ||||||
| @ -270,10 +260,14 @@ func TestWrite_new_map_array_deep(t *testing.T) { | |||||||
| b: | b: | ||||||
|   c: 2 |   c: 2 | ||||||
| `) | `) | ||||||
|  | 	var expected = `b: | ||||||
|  |   c: 2 | ||||||
|  |   d: | ||||||
|  |   - "4"` | ||||||
| 
 | 
 | ||||||
| 	updated := writeMap(data, []string{"b", "d", "0"}, "4") | 	updated := writeMap(data, []string{"b", "d", "0"}, "4") | ||||||
| 	got, _ := readMap(updated, "b", []string{"d", "0"}) | 	got, _ := yamlToString(updated) | ||||||
| 	assertResult(t, "4", got) | 	assertResult(t, expected, got) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestWrite_add_to_array(t *testing.T) { | func TestWrite_add_to_array(t *testing.T) { | ||||||
| @ -298,8 +292,7 @@ b: | |||||||
| `) | `) | ||||||
| 	updated := writeMap(data, []string{"b"}, "4") | 	updated := writeMap(data, []string{"b"}, "4") | ||||||
| 
 | 
 | ||||||
| 	b := entryInSlice(updated, "b").Value | 	assertResult(t, "[{b 4}]", fmt.Sprintf("%v", updated)) | ||||||
| 	assertResult(t, "4", fmt.Sprintf("%v", b)) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestWriteMap_no_paths(t *testing.T) { | func TestWriteMap_no_paths(t *testing.T) { | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								examples/numbered_keys.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								examples/numbered_keys.yml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | |||||||
|  | 5: | ||||||
|  |   6: camel! | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user