From fb4deaf057543080362a2796163422754f43f915 Mon Sep 17 00:00:00 2001 From: kreativmonkey Date: Sat, 1 Jul 2017 18:23:28 +0200 Subject: [PATCH] Fix Pointer Issue --- shrty/short.go | 12 ++++++------ shrty/short_test.go | 13 +++++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/shrty/short.go b/shrty/short.go index 6aec8e8..233926e 100644 --- a/shrty/short.go +++ b/shrty/short.go @@ -31,11 +31,11 @@ type Meta struct{ } func (s *Store) Short(URL string, value *string) error { - d := &Data{ + d := Data{ URL: URL, } - err := CheckURL(d) + err := CheckURL(&d) if err != nil { return err } @@ -44,7 +44,7 @@ func (s *Store) Short(URL string, value *string) error { d.Hash = fmt.Sprintf("%x", sha256.Sum256([]byte(d.OriginalURL))) // Check if the URL already in the Storage - if ok := s.Exist(d); ok { + if ok := s.Exist(&d); ok { *value = d.Token return nil } @@ -56,7 +56,7 @@ func (s *Store) Short(URL string, value *string) error { d.Token = d.Hash[:hashShortestLen] d.Created = time.Now().String() - s.Token[d.Token] = d + s.Token[d.Token] = &d s.Url[d.Hash] = d.Token *value = d.Token @@ -70,8 +70,8 @@ func (s *Store) Short(URL string, value *string) error { // URL already Exist in the func (s *Store) Exist(d *Data) bool { - if val, ok := s.Url[d.Hash]; ok { - d = s.Token[val] + if token, ok := s.Url[d.Hash]; ok { + *d = *s.Token[token] return true } return false diff --git a/shrty/short_test.go b/shrty/short_test.go index 76a3972..315afcc 100644 --- a/shrty/short_test.go +++ b/shrty/short_test.go @@ -42,6 +42,11 @@ func TestStorage_Short(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expected, s.Token[token]) + + // If already exist the same Token will be return + err = s.Short("http://bit.ly/2scBYES", &token) + assert.NoError(t, err) + assert.Equal(t, expected, s.Token[token]) } // Whats wrong with this Test? @@ -88,10 +93,10 @@ func TestStorage_Get(t *testing.T) { } func TestStore_Exist(t *testing.T) { - s, err := Open("test.db") + s, err := Open(path) defer os.Remove(path) - d := &Data{ + d := Data{ URL:"http://bit.ly/2scBYES", URLFetched:"https://www.youtube.com/watch?v=bouIpFd9VGM", CanonicalURL:"", @@ -111,7 +116,7 @@ func TestStore_Exist(t *testing.T) { Clicks:0, } - notexist := s.Exist(d) + notexist := s.Exist(&d) assert.Equal(t, false, notexist) var token string @@ -119,7 +124,7 @@ func TestStore_Exist(t *testing.T) { err = s.Short(d.URL, &token) assert.NoError(t, err) - exist := s.Exist(d) + exist := s.Exist(&d) assert.Equal(t, true, exist) }