Skip to content

Commit

Permalink
Add comment on AfterLoad usage
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Richardson <[email protected]>
  • Loading branch information
awrichar committed May 9, 2024
1 parent cf40dfe commit d9ac6df
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/dbsql/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type CrudBase[T Resource] struct {
ReadTableAlias string
ReadOnlyColumns []string
ReadQueryModifier QueryModifier
AfterLoad func(ctx context.Context, inst T) error
AfterLoad func(ctx context.Context, inst T) error // perform final validation/formatting after an instance is loaded from db
}

func (c *CrudBase[T]) Scoped(scope sq.Eq) CRUD[T] {
Expand Down
7 changes: 3 additions & 4 deletions pkg/dbsql/crud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type TestCRUDable struct {
Field4 *int64 `json:"f4"`
Field5 *bool `json:"f5"`
Field6 *fftypes.FFDuration `json:"f6"`
Gen *string `json:"gen"`
}

var CRUDableQueryFactory = &ffapi.QueryFields{
Expand Down Expand Up @@ -379,17 +378,17 @@ func TestCRUDWithDBEnd2End(t *testing.T) {
collection.events = nil

// Install an AfterLoad handler
afterLoadCalled := false
collection.AfterLoad = func(ctx context.Context, inst *TestCRUDable) error {
inst.Gen = ptrTo("generated")
afterLoadCalled = true
return nil
}

// Check we get it back
c1copy, err := iCrud.GetByID(ctx, c1.ID.String())
assert.NoError(t, err)
assert.Equal(t, "generated", *c1copy.Gen)
c1copy.Gen = nil
checkEqualExceptTimes(t, *c1, *c1copy)
assert.True(t, afterLoadCalled)
collection.AfterLoad = nil

// Check we get it back by name
Expand Down

0 comments on commit d9ac6df

Please sign in to comment.