From c5388d27c4d6b7118b326b4d0f928faee3d26508 Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Fri, 3 Aug 2018 16:23:21 -0700 Subject: [PATCH] add hash function for self link name (#1836) --- google/self_link_helpers.go | 6 ++++++ google/self_link_helpers_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/google/self_link_helpers.go b/google/self_link_helpers.go index 893ed053..4191c468 100644 --- a/google/self_link_helpers.go +++ b/google/self_link_helpers.go @@ -60,6 +60,12 @@ func getRelativePath(selfLink string) (string, error) { return "projects/" + stringParts[1], nil } +// Hash the name path of a self link. +func selfLinkNameHash(selfLink interface{}) int { + name := GetResourceNameFromSelfLink(selfLink.(string)) + return hashcode.String(name) +} + func ConvertSelfLinkToV1(link string) string { reg := regexp.MustCompile("/compute/[a-zA-Z0-9]*/projects/") return reg.ReplaceAllString(link, "/compute/v1/projects/") diff --git a/google/self_link_helpers_test.go b/google/self_link_helpers_test.go index 60cf0ffa..d6aed6a6 100644 --- a/google/self_link_helpers_test.go +++ b/google/self_link_helpers_test.go @@ -86,3 +86,27 @@ func TestGetResourceNameFromSelfLink(t *testing.T) { } } } + +func TestSelfLinkNameHash(t *testing.T) { + cases := map[string]struct { + SelfLink, Name string + Expect bool + }{ + "same": { + SelfLink: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network", + Name: "a-network", + Expect: true, + }, + "different": { + SelfLink: "https://www.googleapis.com/compute/v1/projects/your-project/global/networks/a-network", + Name: "another-network", + Expect: false, + }, + } + + for tn, tc := range cases { + if (selfLinkNameHash(tc.SelfLink) == selfLinkNameHash(tc.Name)) != tc.Expect { + t.Errorf("%s: expected %t for whether hashes matched for self link = %q, name = %q", tn, tc.Expect, tc.SelfLink, tc.Name) + } + } +}