Browse code

modernize: Use fmt.Appendf

Added in Go 1.19

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>

Paweł Gronowski authored on 2025/12/16 02:25:00
Showing 6 changed files
... ...
@@ -188,7 +188,7 @@ func TestStoreLen(t *testing.T) {
188 188
 
189 189
 	expected := 10
190 190
 	for i := range expected {
191
-		_, err := imgStore.Create([]byte(fmt.Sprintf(`{"comment": "abc%d", "rootfs": {"type": "layers"}}`, i)))
191
+		_, err := imgStore.Create(fmt.Appendf(nil, `{"comment": "abc%d", "rootfs": {"type": "layers"}}`, i))
192 192
 		assert.NilError(t, err)
193 193
 	}
194 194
 	numImages := imgStore.Len()
... ...
@@ -393,7 +393,7 @@ func (nDB *NetworkDB) GetEntry(tname, nid, key string) ([]byte, error) {
393 393
 }
394 394
 
395 395
 func (nDB *NetworkDB) getEntry(tname, nid, key string) (*entry, error) {
396
-	e, ok := nDB.indexes[byTable].Get([]byte(fmt.Sprintf("/%s/%s/%s", tname, nid, key)))
396
+	e, ok := nDB.indexes[byTable].Get(fmt.Appendf(nil, "/%s/%s/%s", tname, nid, key))
397 397
 	if !ok {
398 398
 		return nil, types.NotFoundErrorf("could not get entry in table %s with network id %s and key %s", tname, nid, key)
399 399
 	}
... ...
@@ -470,7 +470,7 @@ func (nDB *NetworkDB) GetTableByNetwork(tname, nid string) map[string]*TableElem
470 470
 	root := nDB.indexes[byTable].Root()
471 471
 	nDB.RUnlock()
472 472
 	entries := make(map[string]*TableElem)
473
-	root.WalkPrefix([]byte(fmt.Sprintf("/%s/%s", tname, nid)), func(k []byte, v *entry) bool {
473
+	root.WalkPrefix(fmt.Appendf(nil, "/%s/%s", tname, nid), func(k []byte, v *entry) bool {
474 474
 		if v.deleting {
475 475
 			return false
476 476
 		}
... ...
@@ -794,8 +794,8 @@ func (nDB *NetworkDB) updateLocalNetworkTime() {
794 794
 // createOrUpdateEntry this function handles the creation or update of entries into the local
795 795
 // tree store. It is also used to keep in sync the entries number of the network (all tables are aggregated)
796 796
 func (nDB *NetworkDB) createOrUpdateEntry(nid, tname, key string, v *entry) (okTable bool, okNetwork bool) {
797
-	nDB.indexes[byTable], _, okTable = nDB.indexes[byTable].Insert([]byte(fmt.Sprintf("/%s/%s/%s", tname, nid, key)), v)
798
-	nDB.indexes[byNetwork], _, okNetwork = nDB.indexes[byNetwork].Insert([]byte(fmt.Sprintf("/%s/%s/%s", nid, tname, key)), v)
797
+	nDB.indexes[byTable], _, okTable = nDB.indexes[byTable].Insert(fmt.Appendf(nil, "/%s/%s/%s", tname, nid, key), v)
798
+	nDB.indexes[byNetwork], _, okNetwork = nDB.indexes[byNetwork].Insert(fmt.Appendf(nil, "/%s/%s/%s", nid, tname, key), v)
799 799
 	if !okNetwork {
800 800
 		// Add only if it is an insert not an update
801 801
 		n, ok := nDB.thisNodeNetworks[nid]
... ...
@@ -809,8 +809,8 @@ func (nDB *NetworkDB) createOrUpdateEntry(nid, tname, key string, v *entry) (okT
809 809
 // deleteEntry this function handles the deletion of entries into the local tree store.
810 810
 // It is also used to keep in sync the entries number of the network (all tables are aggregated)
811 811
 func (nDB *NetworkDB) deleteEntry(nid, tname, key string) (okTable bool, okNetwork bool) {
812
-	nDB.indexes[byTable], _, okTable = nDB.indexes[byTable].Delete([]byte(fmt.Sprintf("/%s/%s/%s", tname, nid, key)))
813
-	nDB.indexes[byNetwork], _, okNetwork = nDB.indexes[byNetwork].Delete([]byte(fmt.Sprintf("/%s/%s/%s", nid, tname, key)))
812
+	nDB.indexes[byTable], _, okTable = nDB.indexes[byTable].Delete(fmt.Appendf(nil, "/%s/%s/%s", tname, nid, key))
813
+	nDB.indexes[byNetwork], _, okNetwork = nDB.indexes[byNetwork].Delete(fmt.Appendf(nil, "/%s/%s/%s", nid, tname, key))
814 814
 	if okNetwork {
815 815
 		// Remove only if the delete is successful
816 816
 		n, ok := nDB.thisNodeNetworks[nid]
... ...
@@ -324,14 +324,14 @@ func TestNetworkDBCRUDTableEntries(t *testing.T) {
324 324
 	for i := 1; i <= n; i++ {
325 325
 		err = dbs[0].CreateEntry("test_table", "network1",
326 326
 			fmt.Sprintf("test_key0%d", i),
327
-			[]byte(fmt.Sprintf("test_value0%d", i)))
327
+			fmt.Appendf(nil, "test_value0%d", i))
328 328
 		assert.NilError(t, err)
329 329
 	}
330 330
 
331 331
 	for i := 1; i <= n; i++ {
332 332
 		err = dbs[1].CreateEntry("test_table", "network1",
333 333
 			fmt.Sprintf("test_key1%d", i),
334
-			[]byte(fmt.Sprintf("test_value1%d", i)))
334
+			fmt.Appendf(nil, "test_value1%d", i))
335 335
 		assert.NilError(t, err)
336 336
 	}
337 337
 
... ...
@@ -445,7 +445,7 @@ func TestNetworkDBBulkSync(t *testing.T) {
445 445
 	for i := 1; i <= n; i++ {
446 446
 		err = dbs[0].CreateEntry("test_table", "network1",
447 447
 			fmt.Sprintf("test_key0%d", i),
448
-			[]byte(fmt.Sprintf("test_value0%d", i)))
448
+			fmt.Appendf(nil, "test_value0%d", i))
449 449
 		assert.NilError(t, err)
450 450
 	}
451 451
 
... ...
@@ -5645,7 +5645,7 @@ func (s *DockerCLIBuildSuite) TestBuildMultiStageCopyFromSyntax(c *testing.T) {
5645 5645
 	assert.Equal(c, strings.Count(result.Combined(), "Using cache"), 7)
5646 5646
 	assert.Equal(c, getIDByName(c, "build1"), getIDByName(c, "build2"))
5647 5647
 
5648
-	err := os.WriteFile(filepath.Join(ctx.Dir, "Dockerfile"), []byte(fmt.Sprintf(dockerfile, "COPY baz/aa foo")), 0o644)
5648
+	err := os.WriteFile(filepath.Join(ctx.Dir, "Dockerfile"), fmt.Appendf(nil, dockerfile, "COPY baz/aa foo"), 0o644)
5649 5649
 	assert.NilError(c, err)
5650 5650
 
5651 5651
 	// changing file in parent block should not affect last block
... ...
@@ -2125,7 +2125,7 @@ func (s *DockerDaemonSuite) TestShmSizeReload(c *testing.T) {
2125 2125
 	configFile := filepath.Join(configPath, "config.json")
2126 2126
 
2127 2127
 	size := 67108864 * 2
2128
-	configData := []byte(fmt.Sprintf(`{"default-shm-size": "%dM"}`, size/1024/1024))
2128
+	configData := fmt.Appendf(nil, `{"default-shm-size": "%dM"}`, size/1024/1024)
2129 2129
 	assert.Assert(c, os.WriteFile(configFile, configData, 0o666) == nil, "could not write temp file for config reload")
2130 2130
 	pattern := regexp.MustCompile(fmt.Sprintf("shm on /dev/shm type tmpfs(.*)size=%dk", size/1024))
2131 2131
 
... ...
@@ -2140,7 +2140,7 @@ func (s *DockerDaemonSuite) TestShmSizeReload(c *testing.T) {
2140 2140
 	assert.Equal(c, strings.TrimSpace(out), fmt.Sprintf("%v", size))
2141 2141
 
2142 2142
 	size = 67108864 * 3
2143
-	configData = []byte(fmt.Sprintf(`{"default-shm-size": "%dM"}`, size/1024/1024))
2143
+	configData = fmt.Appendf(nil, `{"default-shm-size": "%dM"}`, size/1024/1024)
2144 2144
 	assert.Assert(c, os.WriteFile(configFile, configData, 0o666) == nil, "could not write temp file for config reload")
2145 2145
 	pattern = regexp.MustCompile(fmt.Sprintf("shm on /dev/shm type tmpfs(.*)size=%dk", size/1024))
2146 2146
 
... ...
@@ -77,7 +77,7 @@ func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithWrongHostnamesStored(c *
77 77
 	c.Setenv("PATH", testPath)
78 78
 
79 79
 	cmd := exec.Command("docker-credential-shell-test", "store")
80
-	stdin := bytes.NewReader([]byte(fmt.Sprintf(`{"ServerURL": "https://%s", "Username": %q, "Secret": %q}`, privateRegistryURL, s.reg.Username(), s.reg.Password())))
80
+	stdin := bytes.NewReader(fmt.Appendf(nil, `{"ServerURL": "https://%s", "Username": %q, "Secret": %q}`, privateRegistryURL, s.reg.Username(), s.reg.Password()))
81 81
 	cmd.Stdin = stdin
82 82
 	assert.NilError(c, cmd.Run())
83 83