Skip to content

Commit

Permalink
feat: rename MachineFactory struct into NumscriptParser
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone authored and gfyrag committed Oct 16, 2024
1 parent 9eb3510 commit 861f7f2
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 69 deletions.
21 changes: 11 additions & 10 deletions internal/controller/ledger/controller_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"database/sql"
"fmt"
"math/big"
"reflect"

. "github.com/formancehq/go-libs/v2/collectionutils"
"go.opentelemetry.io/otel/metric"
noopmetrics "go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/trace"
nooptracer "go.opentelemetry.io/otel/trace/noop"
"math/big"
"reflect"

"github.com/formancehq/go-libs/v2/migrations"
"github.com/formancehq/ledger/internal/tracing"
Expand All @@ -30,9 +31,9 @@ import (
)

type DefaultController struct {
store Store
machineFactory MachineFactory
ledger ledger.Ledger
store Store
parser NumscriptParser
ledger ledger.Ledger

tracer trace.Tracer
meter metric.Meter
Expand All @@ -51,13 +52,13 @@ type DefaultController struct {
func NewDefaultController(
l ledger.Ledger,
store Store,
machineFactory MachineFactory,
numscriptParser NumscriptParser,
opts ...DefaultControllerOption,
) *DefaultController {
ret := &DefaultController{
store: store,
ledger: l,
machineFactory: machineFactory,
store: store,
ledger: l,
parser: numscriptParser,
}

for _, opt := range append(defaultOptions, opts...) {
Expand Down Expand Up @@ -250,7 +251,7 @@ func (ctrl *DefaultController) createTransaction(ctx context.Context, sqlTX TX,
logger := logging.FromContext(ctx).WithField("req", uuid.NewString()[:8])
ctx = logging.ContextWithLogger(ctx, logger)

m, err := ctrl.machineFactory.Make(parameters.Input.Plain)
m, err := ctrl.parser.Parse(parameters.Input.Plain)
if err != nil {
return nil, fmt.Errorf("failed to compile script: %w", err)
}
Expand Down
64 changes: 32 additions & 32 deletions internal/controller/ledger/controller_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func TestCreateTransaction(t *testing.T) {

store := NewMockStore(ctrl)
machine := NewMockMachine(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
sqlTX := NewMockTX(ctrl)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)

runScript := RunScript{}

machineFactory.EXPECT().
Make(runScript.Plain).
parser.EXPECT().
Parse(runScript.Plain).
Return(machine, nil)

store.EXPECT().
Expand Down Expand Up @@ -73,11 +73,11 @@ func TestRevertTransaction(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
sqlTX := NewMockTX(ctrl)
ctx := logging.TestingContext()

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)

store.EXPECT().
WithTX(gomock.Any(), nil, gomock.Any()).
Expand Down Expand Up @@ -121,11 +121,11 @@ func TestSaveTransactionMetadata(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
sqlTX := NewMockTX(ctrl)
ctx := logging.TestingContext()

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)

store.EXPECT().
WithTX(gomock.Any(), nil, gomock.Any()).
Expand Down Expand Up @@ -161,11 +161,11 @@ func TestDeleteTransactionMetadata(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
sqlTX := NewMockTX(ctrl)
ctx := logging.TestingContext()

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)

store.EXPECT().
WithTX(gomock.Any(), nil, gomock.Any()).
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestListTransactions(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
ctx := logging.TestingContext()

cursor := &bunpaginate.Cursor[ledger.Transaction]{}
Expand All @@ -207,7 +207,7 @@ func TestListTransactions(t *testing.T) {
ListTransactions(gomock.Any(), query).
Return(cursor, nil)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)
ret, err := l.ListTransactions(ctx, query)
require.NoError(t, err)
require.Equal(t, cursor, ret)
Expand All @@ -218,13 +218,13 @@ func TestCountAccounts(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
ctx := logging.TestingContext()

query := NewListAccountsQuery(NewPaginatedQueryOptions[PITFilterWithVolumes](PITFilterWithVolumes{}))
store.EXPECT().CountAccounts(gomock.Any(), query).Return(1, nil)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)
count, err := l.CountAccounts(ctx, query)
require.NoError(t, err)
require.Equal(t, 1, count)
Expand All @@ -235,7 +235,7 @@ func TestGetTransaction(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
ctx := logging.TestingContext()

tx := ledger.Transaction{}
Expand All @@ -244,7 +244,7 @@ func TestGetTransaction(t *testing.T) {
GetTransaction(gomock.Any(), query).
Return(&tx, nil)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)
ret, err := l.GetTransaction(ctx, query)
require.NoError(t, err)
require.Equal(t, tx, *ret)
Expand All @@ -255,7 +255,7 @@ func TestGetAccount(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
ctx := logging.TestingContext()

account := ledger.Account{}
Expand All @@ -264,7 +264,7 @@ func TestGetAccount(t *testing.T) {
GetAccount(gomock.Any(), query).
Return(&account, nil)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)
ret, err := l.GetAccount(ctx, query)
require.NoError(t, err)
require.Equal(t, account, *ret)
Expand All @@ -275,13 +275,13 @@ func TestCountTransactions(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
ctx := logging.TestingContext()

query := NewListTransactionsQuery(NewPaginatedQueryOptions[PITFilterWithVolumes](PITFilterWithVolumes{}))
store.EXPECT().CountTransactions(gomock.Any(), query).Return(1, nil)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)
count, err := l.CountTransactions(ctx, query)
require.NoError(t, err)
require.Equal(t, 1, count)
Expand All @@ -292,7 +292,7 @@ func TestListAccounts(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
ctx := logging.TestingContext()

cursor := &bunpaginate.Cursor[ledger.Account]{}
Expand All @@ -301,7 +301,7 @@ func TestListAccounts(t *testing.T) {
ListAccounts(gomock.Any(), query).
Return(cursor, nil)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)
ret, err := l.ListAccounts(ctx, query)
require.NoError(t, err)
require.Equal(t, cursor, ret)
Expand All @@ -312,7 +312,7 @@ func TestGetAggregatedBalances(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
ctx := logging.TestingContext()

balancesByAssets := ledger.BalancesByAssets{}
Expand All @@ -321,7 +321,7 @@ func TestGetAggregatedBalances(t *testing.T) {
GetAggregatedBalances(gomock.Any(), query).
Return(balancesByAssets, nil)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)
ret, err := l.GetAggregatedBalances(ctx, query)
require.NoError(t, err)
require.Equal(t, balancesByAssets, ret)
Expand All @@ -332,7 +332,7 @@ func TestListLogs(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
ctx := logging.TestingContext()

cursor := &bunpaginate.Cursor[ledger.Log]{}
Expand All @@ -341,7 +341,7 @@ func TestListLogs(t *testing.T) {
ListLogs(gomock.Any(), query).
Return(cursor, nil)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)
ret, err := l.ListLogs(ctx, query)
require.NoError(t, err)
require.Equal(t, cursor, ret)
Expand All @@ -352,7 +352,7 @@ func TestGetVolumesWithBalances(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
ctx := logging.TestingContext()

balancesByAssets := &bunpaginate.Cursor[ledger.VolumesWithBalanceByAssetByAccount]{}
Expand All @@ -361,7 +361,7 @@ func TestGetVolumesWithBalances(t *testing.T) {
GetVolumesWithBalances(gomock.Any(), query).
Return(balancesByAssets, nil)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)
ret, err := l.GetVolumesWithBalances(ctx, query)
require.NoError(t, err)
require.Equal(t, balancesByAssets, ret)
Expand All @@ -372,15 +372,15 @@ func TestGetMigrationsInfo(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
ctx := logging.TestingContext()

migrationsInfo := make([]migrations.Info, 0)
store.EXPECT().
GetMigrationsInfo(gomock.Any()).
Return(migrationsInfo, nil)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)
ret, err := l.GetMigrationsInfo(ctx)
require.NoError(t, err)
require.Equal(t, migrationsInfo, ret)
Expand All @@ -391,14 +391,14 @@ func TestIsDatabaseUpToDate(t *testing.T) {
ctrl := gomock.NewController(t)

store := NewMockStore(ctrl)
machineFactory := NewMockMachineFactory(ctrl)
parser := NewMockNumscriptParser(ctrl)
ctx := logging.TestingContext()

store.EXPECT().
IsUpToDate(gomock.Any()).
Return(true, nil)

l := NewDefaultController(ledger.Ledger{}, store, machineFactory)
l := NewDefaultController(ledger.Ledger{}, store, parser)
ret, err := l.IsDatabaseUpToDate(ctx)
require.NoError(t, err)
require.True(t, ret)
Expand Down
16 changes: 8 additions & 8 deletions internal/controller/ledger/machine_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ package ledger

//go:generate mockgen -write_source_comment=false -write_package_comment=false -source machine_factory.go -destination machine_factory_generated_test.go -package ledger . MachineFactory

type MachineFactory interface {
// Make can return following errors:
type NumscriptParser interface {
// Parse can return following errors:
// * ErrCompilationFailed
Make(script string) (Machine, error)
Parse(script string) (Machine, error)
}

type DefaultMachineFactory struct {
type DefaultNumscriptParser struct {
compiler Compiler
}

func (d *DefaultMachineFactory) Make(script string) (Machine, error) {
func (d *DefaultNumscriptParser) Parse(script string) (Machine, error) {
ret, err := d.compiler.Compile(script)
if err != nil {
return nil, err
}
return NewDefaultMachine(*ret), nil
}

func NewDefaultMachineFactory(compiler Compiler) *DefaultMachineFactory {
return &DefaultMachineFactory{
func NewDefaultMachineFactory(compiler Compiler) *DefaultNumscriptParser {
return &DefaultNumscriptParser{
compiler: compiler,
}
}

var _ MachineFactory = (*DefaultMachineFactory)(nil)
var _ NumscriptParser = (*DefaultNumscriptParser)(nil)
Loading

0 comments on commit 861f7f2

Please sign in to comment.