Skip to content

Commit 09ea5d2

Browse files
authored
Allow IDs of length 11. (#7)
Modern Go Playground IDs have length 11, so we want to treat them as valid. Update internal comment about salt to reflect current state. Follows golang/playground@cb45f7d.
1 parent ea620c5 commit 09ea5d2

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

id.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import (
99

1010
// snippetBodyToID mimics the mapping scheme used by the Go Playground.
1111
func snippetBodyToID(body []byte) string {
12-
// This is the actual salt value used by Go Playground, it comes from
13-
// https://code.google.com/p/go-playground/source/browse/goplay/share.go#18.
12+
// This was the actual salt value used by Go Playground at some time in the past.
13+
// It came from https://code.google.com/p/go-playground/source/browse/goplay/share.go#18.
1414
// See https://github.com/gopherjs/snippet-store/pull/1#discussion_r22512198 for more details.
15+
//
16+
// It has since changed. We continue to use the same value for now to keep things consistent.
17+
// See https://github.com/golang/playground/blob/a72214bb7a8781349b57129256dc0c64d233ef08/share.go#L17-L19
18+
// for the current status. It's possible to change our hashing algorithm to be in sync with
19+
// the Go Playground.
1520
const salt = "[replace this with something unique]"
1621

1722
h := sha1.New()
@@ -22,9 +27,10 @@ func snippetBodyToID(body []byte) string {
2227
}
2328

2429
// validateID returns an error if id is of unexpected format.
30+
// ID of length 10 and 11 are both supported, so that historical IDs continue to work.
2531
func validateID(id string) error {
26-
if len(id) != 10 {
27-
return fmt.Errorf("id length is %v instead of 10", len(id))
32+
if len(id) != 10 && len(id) != 11 {
33+
return fmt.Errorf("id length is %v instead of 10 or 11", len(id))
2834
}
2935

3036
for _, b := range []byte(id) {

id_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "fmt"
55
func Example_validateID() {
66
fmt.Println(validateID("D9L6MbPfE4"))
77
fmt.Println(validateID("ABZdez09-_"))
8+
fmt.Println(validateID("N_M_YelfGeR"))
89
fmt.Println(validateID("Abc"))
910
fmt.Println(validateID("Abc?q=1235"))
1011
fmt.Println(validateID("../../file"))
@@ -13,7 +14,8 @@ func Example_validateID() {
1314
// Output:
1415
// <nil>
1516
// <nil>
16-
// id length is 3 instead of 10
17+
// <nil>
18+
// id length is 3 instead of 10 or 11
1719
// id contains unexpected character '?'
1820
// id contains unexpected character '.'
1921
// id contains unexpected character '\u00e4'

0 commit comments

Comments
 (0)