diff --git a/internal/controller/ledger/controller_default.go b/internal/controller/ledger/controller_default.go index 242a460b2..a4a92b89b 100644 --- a/internal/controller/ledger/controller_default.go +++ b/internal/controller/ledger/controller_default.go @@ -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" @@ -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 @@ -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...) { @@ -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) } diff --git a/internal/controller/ledger/controller_default_test.go b/internal/controller/ledger/controller_default_test.go index 1cb3fa4b9..627ab6fa2 100644 --- a/internal/controller/ledger/controller_default_test.go +++ b/internal/controller/ledger/controller_default_test.go @@ -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(). @@ -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()). @@ -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()). @@ -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()). @@ -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]{} @@ -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) @@ -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) @@ -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{} @@ -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) @@ -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{} @@ -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) @@ -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) @@ -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]{} @@ -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) @@ -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{} @@ -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) @@ -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]{} @@ -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) @@ -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]{} @@ -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) @@ -372,7 +372,7 @@ 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) @@ -380,7 +380,7 @@ func TestGetMigrationsInfo(t *testing.T) { 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) @@ -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) diff --git a/internal/controller/ledger/machine_factory.go b/internal/controller/ledger/machine_factory.go index cc2729016..ef96a10b1 100644 --- a/internal/controller/ledger/machine_factory.go +++ b/internal/controller/ledger/machine_factory.go @@ -2,17 +2,17 @@ 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 @@ -20,10 +20,10 @@ func (d *DefaultMachineFactory) Make(script string) (Machine, error) { 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) diff --git a/internal/controller/ledger/machine_factory_generated_test.go b/internal/controller/ledger/machine_factory_generated_test.go index e08fd528e..af26e407f 100644 --- a/internal/controller/ledger/machine_factory_generated_test.go +++ b/internal/controller/ledger/machine_factory_generated_test.go @@ -11,40 +11,40 @@ import ( gomock "go.uber.org/mock/gomock" ) -// MockMachineFactory is a mock of MachineFactory interface. -type MockMachineFactory struct { +// MockNumscriptParser is a mock of NumscriptParser interface. +type MockNumscriptParser struct { ctrl *gomock.Controller - recorder *MockMachineFactoryMockRecorder + recorder *MockNumscriptParserMockRecorder } -// MockMachineFactoryMockRecorder is the mock recorder for MockMachineFactory. -type MockMachineFactoryMockRecorder struct { - mock *MockMachineFactory +// MockNumscriptParserMockRecorder is the mock recorder for MockNumscriptParser. +type MockNumscriptParserMockRecorder struct { + mock *MockNumscriptParser } -// NewMockMachineFactory creates a new mock instance. -func NewMockMachineFactory(ctrl *gomock.Controller) *MockMachineFactory { - mock := &MockMachineFactory{ctrl: ctrl} - mock.recorder = &MockMachineFactoryMockRecorder{mock} +// NewMockNumscriptParser creates a new mock instance. +func NewMockNumscriptParser(ctrl *gomock.Controller) *MockNumscriptParser { + mock := &MockNumscriptParser{ctrl: ctrl} + mock.recorder = &MockNumscriptParserMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockMachineFactory) EXPECT() *MockMachineFactoryMockRecorder { +func (m *MockNumscriptParser) EXPECT() *MockNumscriptParserMockRecorder { return m.recorder } -// Make mocks base method. -func (m *MockMachineFactory) Make(script string) (Machine, error) { +// Parse mocks base method. +func (m *MockNumscriptParser) Parse(script string) (Machine, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Make", script) + ret := m.ctrl.Call(m, "Parse", script) ret0, _ := ret[0].(Machine) ret1, _ := ret[1].(error) return ret0, ret1 } -// Make indicates an expected call of Make. -func (mr *MockMachineFactoryMockRecorder) Make(script any) *gomock.Call { +// Parse indicates an expected call of Parse. +func (mr *MockNumscriptParserMockRecorder) Parse(script any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Make", reflect.TypeOf((*MockMachineFactory)(nil).Make), script) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Parse", reflect.TypeOf((*MockNumscriptParser)(nil).Parse), script) } diff --git a/internal/controller/ledger/stats_test.go b/internal/controller/ledger/stats_test.go index 4f2b5dbf4..e314c6d02 100644 --- a/internal/controller/ledger/stats_test.go +++ b/internal/controller/ledger/stats_test.go @@ -14,7 +14,7 @@ func TestStats(t *testing.T) { ctx := logging.TestingContext() ctrl := gomock.NewController(t) store := NewMockStore(ctrl) - machineFactory := NewMockMachineFactory(ctrl) + parser := NewMockNumscriptParser(ctrl) store.EXPECT(). CountTransactions(gomock.Any(), NewListTransactionsQuery(NewPaginatedQueryOptions(PITFilterWithVolumes{}))). @@ -27,7 +27,7 @@ func TestStats(t *testing.T) { ledgerController := NewDefaultController( ledger.MustNewWithDefault("foo"), store, - machineFactory, + parser, ) stats, err := ledgerController.GetStats(ctx) require.NoError(t, err)