Skip to content

Commit

Permalink
FactRepository: encapsulate not found case in persistence implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dermetfan committed Jun 21, 2022
1 parent b354969 commit f4351ab
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/application/component/web/main.go
Expand Up @@ -541,7 +541,7 @@ func (self *Web) InvocationIdGet(w http.ResponseWriter, req *http.Request) {
run = run_
}

var inputs map[string]domain.Fact
var inputs map[string]*domain.Fact
if inputFactIds, err := self.InvocationService.GetInputFactIdsById(id); err != nil {
self.ServerError(w, errors.WithMessage(err, "Failed to fetch input facts IDs"))
return
Expand Down Expand Up @@ -621,7 +621,7 @@ func (self *Web) RunIdGet(w http.ResponseWriter, req *http.Request) {
allocsWithLogsByGroup[alloc.TaskGroup] = append(allocsWithLogsByGroup[alloc.TaskGroup], alloc)
}

var inputs map[string]domain.Fact
var inputs map[string]*domain.Fact
if inputFactIds, err := self.InvocationService.GetInputFactIdsById(run.InvocationId); err != nil {
self.ServerError(w, errors.WithMessage(err, "Failed to fetch input facts IDs"))
return
Expand Down
4 changes: 2 additions & 2 deletions src/application/service/action.go
Expand Up @@ -213,11 +213,11 @@ func (self actionService) IsRunnable(action *domain.Action) (bool, map[string]*d
return err
default:
if !input.Not {
inputs[name] = &fact
inputs[name] = fact
}

// Match candidate fact.
if matchErr, err := matchFact(action.InOut.Input(name, inputs).Match, &fact); err != nil {
if matchErr, err := matchFact(action.InOut.Input(name, inputs).Match, fact); err != nil {
return err
} else if (matchErr == nil) == input.Not {
if !input.Optional || input.Not {
Expand Down
16 changes: 8 additions & 8 deletions src/application/service/fact.go
Expand Up @@ -21,13 +21,13 @@ type FactService interface {
WithQuerier(config.PgxIface) FactService
withQuerier(config.PgxIface, FactServiceCyclicDependencies) FactService

GetById(uuid.UUID) (domain.Fact, error)
GetById(uuid.UUID) (*domain.Fact, error)
GetByRunId(uuid.UUID) ([]*domain.Fact, error)
GetBinaryById(pgx.Tx, uuid.UUID) (io.ReadSeekCloser, error)
GetLatestByCue(cue.Value) (domain.Fact, error)
GetLatestByCue(cue.Value) (*domain.Fact, error)
GetByCue(cue.Value) ([]*domain.Fact, error)
Save(*domain.Fact, io.Reader) error
GetInvocationInputFacts(map[string]uuid.UUID) (map[string]domain.Fact, error)
GetInvocationInputFacts(map[string]uuid.UUID) (map[string]*domain.Fact, error)
}

type FactServiceCyclicDependencies struct {
Expand Down Expand Up @@ -72,7 +72,7 @@ func (self factService) withQuerier(querier config.PgxIface, cyclicDeps FactServ
return &result
}

func (self factService) GetById(id uuid.UUID) (fact domain.Fact, err error) {
func (self factService) GetById(id uuid.UUID) (fact *domain.Fact, err error) {
self.logger.Trace().Str("id", id.String()).Msg("Getting Fact by ID")
fact, err = self.factRepository.GetById(id)
err = errors.WithMessagef(err, "Could not select existing Fact with ID %q", id)
Expand Down Expand Up @@ -128,7 +128,7 @@ func (self factService) Save(fact *domain.Fact, binary io.Reader) error {
return nil
}

func (self factService) GetLatestByCue(value cue.Value) (fact domain.Fact, err error) {
func (self factService) GetLatestByCue(value cue.Value) (fact *domain.Fact, err error) {
self.logger.Trace().Interface("cue", value).Msg("Getting latest Facts by CUE")
fact, err = self.factRepository.GetLatestByCue(value)
err = errors.WithMessagef(err, "Could not select latest Facts by CUE %q", value)
Expand All @@ -144,12 +144,12 @@ func (self factService) GetByCue(value cue.Value) (facts []*domain.Fact, err err

// XXX Could probably be done entirely in the DB with a single SQL query
// XXX Should this be `InvocationService.GetInputsById()`?
func (self factService) GetInvocationInputFacts(inputFactIds map[string]uuid.UUID) (map[string]domain.Fact, error) {
inputs := map[string]domain.Fact{}
func (self factService) GetInvocationInputFacts(inputFactIds map[string]uuid.UUID) (map[string]*domain.Fact, error) {
inputs := map[string]*domain.Fact{}

type Res struct {
input string
fact domain.Fact
fact *domain.Fact
err error
}
res := make(chan *Res, len(inputFactIds))
Expand Down
8 changes: 1 addition & 7 deletions src/application/service/invocation.go
Expand Up @@ -143,13 +143,7 @@ func (self invocationService) GetOutputById(id uuid.UUID) (domain.OutputDefiniti
} else if inputs, err := (*self.factService).GetInvocationInputFacts(inputFactIds); err != nil {
return domain.OutputDefinition{}, err
} else {
// XXX decide on map[string]*Fact vs map[string]Fact
// and refactor everywhere (where applicable)
inputsPtr := map[string]*domain.Fact{}
for k, v := range inputs {
inputsPtr[k] = &v
}
return action.InOut.Output(inputsPtr), nil
return action.InOut.Output(inputs), nil
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/domain/repository/fact.go
Expand Up @@ -14,10 +14,10 @@ import (
type FactRepository interface {
WithQuerier(config.PgxIface) FactRepository

GetById(uuid.UUID) (domain.Fact, error)
GetById(uuid.UUID) (*domain.Fact, error)
GetByRunId(uuid.UUID) ([]*domain.Fact, error)
GetBinaryById(pgx.Tx, uuid.UUID) (io.ReadSeekCloser, error)
GetLatestByCue(cue.Value) (domain.Fact, error)
GetLatestByCue(cue.Value) (*domain.Fact, error)
GetByCue(cue.Value) ([]*domain.Fact, error)
Save(*domain.Fact, io.Reader) error
}
22 changes: 14 additions & 8 deletions src/infrastructure/persistence/fact.go
Expand Up @@ -30,13 +30,16 @@ func (a *factRepository) WithQuerier(querier config.PgxIface) repository.FactRep
return &factRepository{querier}
}

func (a *factRepository) GetById(id uuid.UUID) (fact domain.Fact, err error) {
err = pgxscan.Get(
context.Background(), a.DB, &fact,
func (a *factRepository) GetById(id uuid.UUID) (*domain.Fact, error) {
fact, err := get(
a.DB, &domain.Fact{},
`SELECT id, run_id, value, created_at, binary_hash FROM fact WHERE id = $1`,
id,
)
return
if fact == nil {
return nil, err
}
return fact.(*domain.Fact), err
}

func (a *factRepository) GetByRunId(id uuid.UUID) (facts []*domain.Fact, err error) {
Expand Down Expand Up @@ -68,14 +71,17 @@ func (a *factRepository) GetBinaryById(tx pgx.Tx, id uuid.UUID) (binary io.ReadS
return
}

func (a *factRepository) GetLatestByCue(value cue.Value) (fact domain.Fact, err error) {
func (a *factRepository) GetLatestByCue(value cue.Value) (*domain.Fact, error) {
where, args := sqlWhereCue(value, nil, 0)
err = pgxscan.Get(
context.Background(), a.DB, &fact,
fact, err := get(
a.DB, &domain.Fact{},
`SELECT id, run_id, value, created_at, binary_hash FROM fact WHERE `+where+` ORDER BY created_at DESC FETCH FIRST ROW ONLY`,
args...,
)
return
if fact == nil {
return nil, err
}
return fact.(*domain.Fact), err
}

func (a *factRepository) GetByCue(value cue.Value) (facts []*domain.Fact, err error) {
Expand Down

0 comments on commit f4351ab

Please sign in to comment.