Skip to content

Commit

Permalink
Merge pull request #48 from calvinmclean/fix/SetStorage
Browse files Browse the repository at this point in the history
Re-add SetStorage method
  • Loading branch information
calvinmclean committed Feb 27, 2024
2 parents 71d1ae0 + 55ade53 commit d785a01
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions babyapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ func (a *API[T]) ChildAPIs() map[string]RelatedAPI {
return children
}

func (a *API[T]) SetStorage(s Storage[T]) *API[T] {
a.panicIfReadOnly()

a.Storage = s
return a
}

func (a *API[T]) panicIfReadOnly() {
if !a.readOnly.TryLock() {
panic(errors.New("API cannot be modified after starting"))
Expand Down
2 changes: 1 addition & 1 deletion examples/storage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
panic(err)
}

api.Storage = storage.NewClient[*User](db, "User")
api.SetStorage(storage.NewClient[*User](db, "User"))

api.RunCLI()
}
2 changes: 1 addition & 1 deletion extensions/kv_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h KeyValueStorage[T]) Apply(api *babyapi.API[T]) error {
storageKeyPrefix = api.Name()
}

api.Storage = storage.NewClient[T](db, storageKeyPrefix)
api.SetStorage(storage.NewClient[T](db, storageKeyPrefix))

return nil
}
Expand Down

0 comments on commit d785a01

Please sign in to comment.