generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # backend/controller/controller.go
- Loading branch information
Showing
12 changed files
with
764 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package configuration | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
// InMemoryProvider is a configuration provider that keeps values in memory | ||
type InMemoryProvider[R Role] struct { | ||
values map[Ref][]byte | ||
} | ||
|
||
var _ MutableProvider[Configuration] = &InMemoryProvider[Configuration]{} | ||
|
||
func NewInMemoryProvider[R Role]() *InMemoryProvider[R] { | ||
return &InMemoryProvider[R]{values: map[Ref][]byte{}} | ||
} | ||
|
||
func (p *InMemoryProvider[R]) Role() R { var r R; return r } | ||
func (p *InMemoryProvider[R]) Key() string { return "grpc" } | ||
|
||
func (p *InMemoryProvider[R]) Load(ctx context.Context, ref Ref, key *url.URL) ([]byte, error) { | ||
if bytes, found := p.values[ref]; found { | ||
return bytes, nil | ||
} | ||
return nil, fmt.Errorf("key %q not found", ref.Name) | ||
} | ||
|
||
func (p *InMemoryProvider[R]) Writer() bool { | ||
return true | ||
} | ||
|
||
// Store a configuration value and return its key. | ||
func (p *InMemoryProvider[R]) Store(ctx context.Context, ref Ref, value []byte) (*url.URL, error) { | ||
p.values[ref] = value | ||
return &url.URL{Scheme: p.Key()}, nil | ||
} | ||
|
||
// Delete a configuration value. | ||
func (p *InMemoryProvider[R]) Delete(ctx context.Context, ref Ref) error { | ||
delete(p.values, ref) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package configuration | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
type InMemoryResolver[R Role] struct { | ||
keyMap map[Ref]*url.URL | ||
} | ||
|
||
var _ Resolver[Configuration] = InMemoryResolver[Configuration]{} | ||
var _ Resolver[Secrets] = InMemoryResolver[Secrets]{} | ||
|
||
func NewInMemoryResolver[R Role]() *InMemoryResolver[R] { | ||
return &InMemoryResolver[R]{keyMap: map[Ref]*url.URL{}} | ||
} | ||
|
||
func (k InMemoryResolver[R]) Role() R { var r R; return r } | ||
|
||
func (k InMemoryResolver[R]) Get(ctx context.Context, ref Ref) (*url.URL, error) { | ||
if key, found := k.keyMap[ref]; found { | ||
return key, nil | ||
} | ||
return nil, fmt.Errorf("key %q not found", ref.Name) | ||
} | ||
|
||
func (k InMemoryResolver[R]) List(ctx context.Context) ([]Entry, error) { | ||
entries := []Entry{} | ||
for ref, url := range k.keyMap { | ||
entries = append(entries, Entry{Ref: ref, Accessor: url}) | ||
} | ||
return entries, nil | ||
} | ||
|
||
func (k InMemoryResolver[R]) Set(ctx context.Context, ref Ref, key *url.URL) error { | ||
k.keyMap[ref] = key | ||
return nil | ||
} | ||
|
||
func (k InMemoryResolver[R]) Unset(ctx context.Context, ref Ref) error { | ||
delete(k.keyMap, ref) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.