From b16d0da418937276b03449baaddb0cd979cf12cb Mon Sep 17 00:00:00 2001 From: Archie Fox Date: Wed, 12 Mar 2025 22:37:08 +0300 Subject: [PATCH] Add interface --- go-demo-4/account/vault.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/go-demo-4/account/vault.go b/go-demo-4/account/vault.go index dbc25dd..8954904 100644 --- a/go-demo-4/account/vault.go +++ b/go-demo-4/account/vault.go @@ -2,13 +2,17 @@ package account import ( "encoding/json" - "password/files" "strings" "time" "github.com/fatih/color" ) +type Db interface { + Read() ([]byte, error) + Write([]byte) +} + type Vault struct { Accounts []Account `json:"accounts"` UpdatedAt time.Time `json:"updatedAt"` @@ -16,10 +20,10 @@ type Vault struct { type VaultWithDb struct { Vault - db files.JsonDb + db Db } -func NewVault(db *files.JsonDb) *VaultWithDb { +func NewVault(db Db) *VaultWithDb { file, err := db.Read() if err != nil { return &VaultWithDb{ @@ -27,7 +31,7 @@ func NewVault(db *files.JsonDb) *VaultWithDb { Accounts: []Account{}, UpdatedAt: time.Now(), }, - db: *db, + db: db, } } var vault Vault @@ -39,12 +43,12 @@ func NewVault(db *files.JsonDb) *VaultWithDb { Accounts: []Account{}, UpdatedAt: time.Now(), }, - db: *db, + db: db, } } return &VaultWithDb{ Vault: vault, - db: *db, + db: db, } }