Repository: valkey-io/valkey-glide
Pull request #3668: Go: Script show command added
Author: login: prateek-kumar-improving
Labels: name: go 🏃
description: golang wrapper
Created: 2025-04-24T17:15:29Z | Merged: 2025-04-26T18:15:34Z

<!--
Thanks for contributing to Valkey GLIDE!

Please make sure you are aware of our contributing guidelines [available
here](https://github.com/valkey-io/valkey-glide/blob/main/CONTRIBUTING.md)

-->

### Issue link

This Pull Request is linked to issue (URL): #3481 

### Checklist

Before submitting the PR make sure the following are checked:

-   [x] This Pull Request is related to one issue.
-   [x] Commit message has a detailed description of what changed and why.
-   [x] Tests are added or updated.
-   [x] CHANGELOG.md and documentation files are updated.
-   [x] Destination branch is correct - main or release
-   [x] Create merge commit if merging release branch into main, squash otherwise.



Diff:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f07db37bd8d..a9a91bae082 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -62,6 +62,7 @@
 * Python: Moved the tests folder to be under the root folder ([#3661](https://github.com/valkey-io/valkey-glide/pull/3661))
 * Go: Add script flush command([#3662](https://github.com/valkey-io/valkey-glide/pull/3662))
 * Go: Add Script Kill ([#3670](https://github.com/valkey-io/valkey-glide/pull/3670))
+* Go: Add Script Show ([#3668](https://github.com/valkey-io/valkey-glide/pull/3668))
 
 #### Breaking Changes
 
diff --git a/go/api/base_client.go b/go/api/base_client.go
index 1b238d61de4..2c0dd0ab734 100644
--- a/go/api/base_client.go
+++ b/go/api/base_client.go
@@ -8023,6 +8023,28 @@ func (client *baseClient) ScriptFlushWithMode(mode options.FlushMode) (string, e
 	return handleOkResponse(result)
 }
 
+// ScriptShow returns the original source code of a script in the script cache.
+//
+// Arguments:
+//
+//	sha1: The SHA1 digest of the script.
+//
+// Return value:
+//
+//	The original source code of the script, if present in the cache.
+//	If the script is not found in the cache, an error is thrown.
+//
+// Since: Valkey 8.0.0
+//
+// [valkey.io]: https://valkey.io/commands/script-show
+func (client *baseClient) ScriptShow(sha1 string) (string, error) {
+	result, err := client.executeCommand(C.ScriptShow, []string{sha1})
+	if err != nil {
+		return DefaultStringResponse, err
+	}
+	return handleStringResponse(result)
+}
+
 // Kills the currently executing Lua script, assuming no write operation was yet performed by the
 // script.
 //
diff --git a/go/api/scripting_and_function_base_commands.go b/go/api/scripting_and_function_base_commands.go
index 20c95b2aab2..5e2d8fe15a5 100644
--- a/go/api/scripting_and_function_base_commands.go
+++ b/go/api/scripting_and_function_base_commands.go
@@ -39,5 +39,7 @@ type ScriptingAndFunctionBaseCommands interface {
 
 	ScriptFlushWithMode(mode options.FlushMode) (string, error)
 
+	ScriptShow(sha1 string) (string, error)
+
 	ScriptKill() (string, error)
 }
diff --git a/go/api/scripting_and_function_commands_test.go b/go/api/scripting_and_function_commands_test.go
index 44762ef27ab..cbfb41a51f3 100644
--- a/go/api/scripting_and_function_commands_test.go
+++ b/go/api/scripting_and_function_commands_test.go
@@ -1207,3 +1207,54 @@ func ExampleGlideClusterClient_ScriptKill_withRoute() {
 	// Output:
 	// Expected error: An error was signalled by the server: - NotBusy: No scripts in execution right now.
 }
+
+// ScriptShow Examples
+func ExampleGlideClient_ScriptShow() {
+	client := getExampleGlideClient()
+
+	// First, create and invoke a script
+	scriptText := "return 'Hello, World!'"
+	script := options.NewScript(scriptText)
+	_, err := client.InvokeScript(*script)
+	if err != nil {
+		fmt.Println("Glide example failed with an error: ", err)
+		return
+	}
+
+	// Now show the script source using ScriptShow
+	scriptSource, err := client.ScriptShow(script.GetHash())
+	if err != nil {
+		fmt.Println("Glide example failed with an error: ", err)
+		return
+	}
+
+	fmt.Println(scriptSource)
+
+	// Output:
+	// return 'Hello, World!'
+}
+
+func ExampleGlideClusterClient_ScriptShow() {
+	client := getExampleGlideClusterClient()
+
+	// First, create and invoke a script
+	scriptText := "return 'Hello World'"
+	script := options.NewScript(scriptText)
+	_, err := client.InvokeScript(*script)
+	if err != nil {
+		fmt.Println("Glide example failed with an error: ", err)
+		return
+	}
+
+	// Now show the script source using ScriptShow
+	scriptSource, err := client.ScriptShow(script.GetHash())
+	if err != nil {
+		fmt.Println("Glide example failed with an error: ", err)
+		return
+	}
+
+	fmt.Println(scriptSource)
+
+	// Output:
+	// return 'Hello World'
+}
diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go
index 56ba1708e54..07d9df22398 100644
--- a/go/integTest/shared_commands_test.go
+++ b/go/integTest/shared_commands_test.go
@@ -10381,3 +10381,34 @@ func (suite *GlideTestSuite) TestScriptFlush() {
 		script.Close()
 	})
 }
+
+func (suite *GlideTestSuite) TestScriptShow() {
+	suite.SkipIfServerVersionLowerThanBy("8.0.0")
+
+	suite.runWithDefaultClients(func(client api.BaseClient) {
+		// Create a unique script code
+		uuid1 := uuid.NewString()
+		code := fmt.Sprintf("return '%s'", uuid1[:5])
+		script := options.NewScript(code)
+
+		// Load the script
+		_, err := client.InvokeScript(*script)
+		assert.Nil(suite.T(), err)
+
+		// Get the SHA1 digest of the script
+		sha1 := script.GetHash()
+
+		// Test with String
+		scriptSource, err := client.ScriptShow(sha1)
+		assert.Nil(suite.T(), err)
+		assert.Equal(suite.T(), code, scriptSource)
+
+		// Test with non-existing SHA1
+		nonExistingSha1 := uuid.NewString()
+		_, err = client.ScriptShow(nonExistingSha1)
+		assert.NotNil(suite.T(), err)
+
+		// Clean up
+		script.Close()
+	})
+}


Reviews:
user: login: prateek-kumar-improving
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-04-25T08:04:47Z
user: login: Yury-Fridlyand
body: Please add example and changelog
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-25T17:42:08Z
user: login: prateek-kumar-improving
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-04-25T18:04:30Z
user: login: edlng
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-04-25T18:05:00Z
user: login: prateek-kumar-improving
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-04-25T20:48:41Z
user: login: prateek-kumar-improving
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-04-25T20:48:55Z
user: login: prateek-kumar-improving
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-04-25T20:49:35Z
user: login: edlng
body: lgtm!
state: APPROVED
author_association: COLLABORATOR
submitted_at: 2025-04-25T21:30:27Z
user: login: Yury-Fridlyand
body: Please add changelog
state: APPROVED
author_association: NONE
submitted_at: 2025-04-26T02:37:42Z

Review comments:
diff_hunk: @@ -8022,3 +8022,23 @@ func (client *baseClient) ScriptFlushWithMode(mode options.FlushMode) (string, e
 	}
 	return handleOkResponse(result)
path: go/api/base_client.go
user: login: prateek-kumar-improving
body: No examples required. This is not an external function.
created_at: 2025-04-25T08:04:47Z
author_association: COLLABORATOR
diff_hunk: @@ -8022,3 +8022,23 @@ func (client *baseClient) ScriptFlushWithMode(mode options.FlushMode) (string, e
 	}
 	return handleOkResponse(result)
 }
+
+// ScriptShow returns the original source code of a script in the script cache.
+//
+// Arguments:
+//
path: go/api/base_client.go
user: login: Yury-Fridlyand
body: Add since section
created_at: 2025-04-25T17:41:36Z
author_association: NONE
diff_hunk: @@ -8022,3 +8022,23 @@ func (client *baseClient) ScriptFlushWithMode(mode options.FlushMode) (string, e
 	}
 	return handleOkResponse(result)
path: go/api/base_client.go
user: login: Yury-Fridlyand
body: Why? It is a part of API provided to users.
created_at: 2025-04-25T17:41:52Z
author_association: NONE
diff_hunk: @@ -10381,3 +10381,34 @@ func (suite *GlideTestSuite) TestScriptFlush() {
 		script.Close()
 	})
 }
+
+func (suite *GlideTestSuite) TestScriptShow() {
+	suite.SkipIfServerVersionLowerThanBy("8.0.0")
+
+	suite.runWithDefaultClients(func(client api.BaseClient) {
+		// Create a unique script code
+		uuid1 := uuid.NewString()
+		code := fmt.Sprintf("return '%s'", uuid1[:5])
+		script := options.NewScript(code)
+
+		// Load the script
+		_, err := client.InvokeScript(*script)
+		assert.Nil(suite.T(), err)
+
+		// Get the SHA1 digest of the script
+		sha1 := script.GetHash()
+
+		// Test with String
+		scriptSource, err := client.ScriptShow(sha1)
+		assert.Nil(suite.T(), err)
+		assert.Equal(suite.T(), code, scriptSource)
+
+		// Test with non-existing SHA1
+		nonExistingSha1 := uuid.NewString()
path: go/integTest/shared_commands_test.go
user: login: edlng
body: nit: probably don't need to use uuid for this
created_at: 2025-04-25T17:59:52Z
author_association: COLLABORATOR
diff_hunk: @@ -8022,3 +8022,23 @@ func (client *baseClient) ScriptFlushWithMode(mode options.FlushMode) (string, e
 	}
 	return handleOkResponse(result)
path: go/api/base_client.go
user: login: prateek-kumar-improving
body: okay, it is not part of the either the wiki or the task list. But I can add.
created_at: 2025-04-25T18:04:30Z
author_association: COLLABORATOR
diff_hunk: @@ -10381,3 +10381,34 @@ func (suite *GlideTestSuite) TestScriptFlush() {
 		script.Close()
 	})
 }
+
+func (suite *GlideTestSuite) TestScriptShow() {
+	suite.SkipIfServerVersionLowerThanBy("8.0.0")
+
+	suite.runWithDefaultClients(func(client api.BaseClient) {
+		// Create a unique script code
+		uuid1 := uuid.NewString()
+		code := fmt.Sprintf("return '%s'", uuid1[:5])
+		script := options.NewScript(code)
+
+		// Load the script
+		_, err := client.InvokeScript(*script)
+		assert.Nil(suite.T(), err)
+
+		// Get the SHA1 digest of the script
+		sha1 := script.GetHash()
+
+		// Test with String
+		scriptSource, err := client.ScriptShow(sha1)
+		assert.Nil(suite.T(), err)
+		assert.Equal(suite.T(), code, scriptSource)
+
+		// Test with non-existing SHA1
+		nonExistingSha1 := uuid.NewString()
path: go/integTest/shared_commands_test.go
user: login: prateek-kumar-improving
body: It is uuid in JAVA, so keeping it for consistency.
created_at: 2025-04-25T20:48:41Z
author_association: COLLABORATOR
diff_hunk: @@ -8022,3 +8022,23 @@ func (client *baseClient) ScriptFlushWithMode(mode options.FlushMode) (string, e
 	}
 	return handleOkResponse(result)
 }
+
+// ScriptShow returns the original source code of a script in the script cache.
+//
+// Arguments:
+//
path: go/api/base_client.go
user: login: prateek-kumar-improving
body: Updated.
created_at: 2025-04-25T20:48:55Z
author_association: COLLABORATOR
diff_hunk: @@ -8022,3 +8022,23 @@ func (client *baseClient) ScriptFlushWithMode(mode options.FlushMode) (string, e
 	}
 	return handleOkResponse(result)
path: go/api/base_client.go
user: login: prateek-kumar-improving
body: Example added. Updated.
created_at: 2025-04-25T20:49:35Z
author_association: COLLABORATOR

PR comments:
user: login: prateek-kumar-improving
created_at: 2025-04-25T20:49:47Z
body: > Please add example and changelog

Updated.
author_association: COLLABORATOR
user: login: prateek-kumar-improving
created_at: 2025-04-26T17:20:44Z
body: > Please add changelog

It got removed during rebasing. Updated again.
author_association: COLLABORATOR