Skip to content

Instantly share code, notes, and snippets.

@caalberts
Created December 27, 2017 15:11
Show Gist options
  • Save caalberts/1b7b2b14a29695e0f663c6ece219e62a to your computer and use it in GitHub Desktop.
Save caalberts/1b7b2b14a29695e0f663c6ece219e62a to your computer and use it in GitHub Desktop.
func (r *AccountRepository) FetchAccount(id string) (*Account, error) {
data, err := r.client.HGetAll(id)
if err != nil {
return nil, err
}
return toAccount(data)
}
type Account struct {
Id string
Name string
Balance int
}
func toAccount(data map[string]string) (*Account, error) {
if _, ok := data["Id"]; !ok {
return nil, errors.New("Missing account ID")
}
balance, err := strconv.Atoi(data["Balance"])
if err != nil {
return nil, err
}
return &Account{
Id: data["Id"],
Name: data["Name"],
Balance: balance,
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment