Fix Pointer Issue

master
kreativmonkey 8 years ago
parent d48ec14a73
commit fb4deaf057

@ -31,11 +31,11 @@ type Meta struct{
} }
func (s *Store) Short(URL string, value *string) error { func (s *Store) Short(URL string, value *string) error {
d := &Data{ d := Data{
URL: URL, URL: URL,
} }
err := CheckURL(d) err := CheckURL(&d)
if err != nil { if err != nil {
return err 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))) d.Hash = fmt.Sprintf("%x", sha256.Sum256([]byte(d.OriginalURL)))
// Check if the URL already in the Storage // Check if the URL already in the Storage
if ok := s.Exist(d); ok { if ok := s.Exist(&d); ok {
*value = d.Token *value = d.Token
return nil return nil
} }
@ -56,7 +56,7 @@ func (s *Store) Short(URL string, value *string) error {
d.Token = d.Hash[:hashShortestLen] d.Token = d.Hash[:hashShortestLen]
d.Created = time.Now().String() d.Created = time.Now().String()
s.Token[d.Token] = d s.Token[d.Token] = &d
s.Url[d.Hash] = d.Token s.Url[d.Hash] = d.Token
*value = d.Token *value = d.Token
@ -70,8 +70,8 @@ func (s *Store) Short(URL string, value *string) error {
// URL already Exist in the // URL already Exist in the
func (s *Store) Exist(d *Data) bool { func (s *Store) Exist(d *Data) bool {
if val, ok := s.Url[d.Hash]; ok { if token, ok := s.Url[d.Hash]; ok {
d = s.Token[val] *d = *s.Token[token]
return true return true
} }
return false return false

@ -42,6 +42,11 @@ func TestStorage_Short(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expected, s.Token[token]) 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? // Whats wrong with this Test?
@ -88,10 +93,10 @@ func TestStorage_Get(t *testing.T) {
} }
func TestStore_Exist(t *testing.T) { func TestStore_Exist(t *testing.T) {
s, err := Open("test.db") s, err := Open(path)
defer os.Remove(path) defer os.Remove(path)
d := &Data{ d := Data{
URL:"http://bit.ly/2scBYES", URL:"http://bit.ly/2scBYES",
URLFetched:"https://www.youtube.com/watch?v=bouIpFd9VGM", URLFetched:"https://www.youtube.com/watch?v=bouIpFd9VGM",
CanonicalURL:"", CanonicalURL:"",
@ -111,7 +116,7 @@ func TestStore_Exist(t *testing.T) {
Clicks:0, Clicks:0,
} }
notexist := s.Exist(d) notexist := s.Exist(&d)
assert.Equal(t, false, notexist) assert.Equal(t, false, notexist)
var token string var token string
@ -119,7 +124,7 @@ func TestStore_Exist(t *testing.T) {
err = s.Short(d.URL, &token) err = s.Short(d.URL, &token)
assert.NoError(t, err) assert.NoError(t, err)
exist := s.Exist(d) exist := s.Exist(&d)
assert.Equal(t, true, exist) assert.Equal(t, true, exist)
} }

Loading…
Cancel
Save