Skip to content

Commit

Permalink
applied gofmt; added some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tobischo committed Aug 4, 2015
1 parent db44715 commit 08afaf9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
38 changes: 21 additions & 17 deletions content.go
Expand Up @@ -2,11 +2,11 @@
package gokeepasslib

import (
"encoding/xml"
"encoding/base64"
"time"
"crypto/rand"
"encoding/base64"
"encoding/xml"
"errors"
"time"
)

// DBContent is a container for all elements of a keepass database
Expand Down Expand Up @@ -91,40 +91,44 @@ func NewRootData() *RootData {
return root
}

// ErrInvalidUUIDLength is an error which is returned during unmarshaling if the UUID does not have 16 bytes length
var ErrInvalidUUIDLength = errors.New("gokeepasslib: length of decoded UUID was not 16")

// UUID stores a universal identifier for each group+entry
type UUID [16]byte

// NewUUID returns a new randomly generated UUID
func NewUUID () UUID {
func NewUUID() UUID {
var id UUID
rand.Read(id[:])
return id
}

func (u UUID) MarshalText () (text []byte,err error) {
text = make([]byte,24)
base64.StdEncoding.Encode(text,u[:])
return
// MarshalText is a marshaler method to encode uuid content as base 64 and return it
func (u UUID) MarshalText() ([]byte, error) {
text := make([]byte, 24)
base64.StdEncoding.Encode(text, u[:])
return text, nil
}

// UnmarshalText unmarshals a byte slice into a UUID by decoding the given data from base64
func (u *UUID) UnmarshalText(text []byte) error {
id := make([]byte,base64.StdEncoding.DecodedLen(len(text)))
length,err := base64.StdEncoding.Decode(id,text)
id := make([]byte, base64.StdEncoding.DecodedLen(len(text)))
length, err := base64.StdEncoding.Decode(id, text)
if err != nil {
return err
}
if length != 16 {
return ErrInvalidUUIDLength
}
copy((*u)[:],id[:16])
copy((*u)[:], id[:16])
return nil
}

//Compares check if c and u are equal, used for searching for a uuid
func (u UUID) Compare (c UUID) bool {
for i,v := range c {
// Compare allowes to check whether two instance of UUID are equal in value.
// This is used for searching a uuid
func (u UUID) Compare(c UUID) bool {
for i, v := range c {
if u[i] != v {
return false
}
Expand All @@ -149,7 +153,7 @@ type Group struct {
}

//NewGroup returns a new group with time data and uuid set
func NewGroup () Group {
func NewGroup() Group {
g := Group{}
g.Times = NewTimeData()
g.UUID = NewUUID()
Expand Down Expand Up @@ -198,7 +202,7 @@ type Entry struct {
}

// NewEntry return a new entry with time data and uuid set
func NewEntry () Entry {
func NewEntry() Entry {
e := Entry{}
e.Times = NewTimeData()
e.UUID = NewUUID()
Expand Down Expand Up @@ -290,6 +294,6 @@ type AutoTypeAssociation struct {

type DeletedObjectData struct {
XMLName xml.Name `xml:"DeletedObject"`
UUID UUID `xml:"UUID"`
UUID UUID `xml:"UUID"`
DeletionTime *time.Time `xml:"DeletionTime"`
}
4 changes: 2 additions & 2 deletions content_test.go
Expand Up @@ -42,10 +42,10 @@ func TestUUID(t *testing.T) {
if !two.Compare(one) {
t.Fatalf("One and Two UUIDs should be equal, are not")
}

three := UUID{}
err = three.UnmarshalText([]byte("rGnBe1gIikK89aZD6n/plABBBB=="))
if err != ErrInvalidUUIDLength {
t.Fatal("Expected invalid uuid error, got: %s",err)
t.Fatal("Expected invalid uuid error, got: %s", err)
}
}

0 comments on commit 08afaf9

Please sign in to comment.