Skip to content

Commit

Permalink
fix: missing big ints un openapispec / sdk (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored Nov 7, 2023
1 parent 1cb459c commit f34d53c
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 66 deletions.
2 changes: 1 addition & 1 deletion components/fctl/cmd/ledger/internal/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func printCommonInformation(
out io.Writer,
txID int64,
txID *big.Int,
reference string,
postings []shared.Posting,
timestamp time.Time,
Expand Down
25 changes: 17 additions & 8 deletions components/fctl/cmd/ledger/internal/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ import (
"context"
"errors"
"fmt"
"math/big"
"strconv"
"strings"

formance "github.com/formancehq/formance-sdk-go"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
)

func TransactionIDOrLastN(ctx context.Context, ledgerClient *formance.Formance, ledger, id string) (int64, error) {
func TransactionIDOrLastN(ctx context.Context, ledgerClient *formance.Formance, ledger, id string) (*big.Int, error) {
if strings.HasPrefix(id, "last") {
id = strings.TrimPrefix(id, "last")
sub := int64(0)
if id != "" {
var err error
sub, err = strconv.ParseInt(id, 10, 64)
if err != nil {
return 0, err
return nil, err
}
}
pageSize := int64(1)
Expand All @@ -29,22 +30,30 @@ func TransactionIDOrLastN(ctx context.Context, ledgerClient *formance.Formance,
}
response, err := ledgerClient.Ledger.ListTransactions(ctx, request)
if err != nil {
return 0, err
return nil, err
}

if response.ErrorResponse != nil {
return 0, fmt.Errorf("%s: %s", response.ErrorResponse.ErrorCode, response.ErrorResponse.ErrorMessage)
return nil, fmt.Errorf("%s: %s", response.ErrorResponse.ErrorCode, response.ErrorResponse.ErrorMessage)
}

if response.StatusCode >= 300 {
return 0, fmt.Errorf("unexpected status code: %d", response.StatusCode)
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

if len(response.TransactionsCursorResponse.Cursor.Data) == 0 {
return 0, errors.New("no transaction found")
return nil, errors.New("no transaction found")
}
return response.TransactionsCursorResponse.Cursor.Data[0].ID + sub, nil
return response.TransactionsCursorResponse.Cursor.Data[0].ID.Sub(
response.TransactionsCursorResponse.Cursor.Data[0].ID,
big.NewInt(sub),
), nil
}

return strconv.ParseInt(id, 10, 64)
v, ok := big.NewInt(0).SetString(id, 10)
if !ok {
return nil, fmt.Errorf("invalid bigint: %s", id)
}

return v, nil
}
3 changes: 1 addition & 2 deletions components/fctl/cmd/ledger/transactions/revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package transactions

import (
"fmt"
"math/big"

"github.com/formancehq/fctl/cmd/ledger/internal"
fctl "github.com/formancehq/fctl/pkg"
Expand Down Expand Up @@ -77,7 +76,7 @@ func (c *RevertController) Run(cmd *cobra.Command, args []string) (fctl.Renderab

request := operations.RevertTransactionRequest{
Ledger: ledger,
ID: big.NewInt(txId),
ID: txId,
}

response, err := ledgerClient.Ledger.RevertTransaction(cmd.Context(), request)
Expand Down
48 changes: 24 additions & 24 deletions components/ledger/internal/machine/vm/machine_overdraft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

ledger "github.com/formancehq/ledger/internal"
internal2 "github.com/formancehq/ledger/internal/machine/internal"
"github.com/formancehq/ledger/internal/machine/internal"
)

func TestOverdraftNotEnough(t *testing.T) {
Expand All @@ -15,7 +15,7 @@ func TestOverdraftNotEnough(t *testing.T) {
)`)
tc.setBalance("foo", "GEM", 89)
tc.expected = CaseResult{
Printed: []internal2.Value{},
Printed: []internal.Value{},
Postings: []Posting{},
Error: ledger.ErrInsufficientFund,
}
Expand All @@ -30,11 +30,11 @@ func TestOverdraftEnough(t *testing.T) {
)`)
tc.setBalance("foo", "GEM", 90)
tc.expected = CaseResult{
Printed: []internal2.Value{},
Printed: []internal.Value{},
Postings: []Posting{
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(100),
Amount: internal.NewMonetaryInt(100),
Source: "foo",
Destination: "world",
},
Expand All @@ -52,11 +52,11 @@ func TestOverdraftUnbounded(t *testing.T) {
)`)
tc.setBalance("foo", "GEM", 90)
tc.expected = CaseResult{
Printed: []internal2.Value{},
Printed: []internal.Value{},
Postings: []Posting{
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(1000),
Amount: internal.NewMonetaryInt(1000),
Source: "foo",
Destination: "world",
},
Expand All @@ -82,23 +82,23 @@ func TestOverdraftSourceAllotmentSuccess(t *testing.T) {
tc.setBalance("bar", "GEM", 20)
tc.setBalance("baz", "GEM", 0)
tc.expected = CaseResult{
Printed: []internal2.Value{},
Printed: []internal.Value{},
Postings: []Posting{
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(50),
Amount: internal.NewMonetaryInt(50),
Source: "foo",
Destination: "world",
},
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(40),
Amount: internal.NewMonetaryInt(40),
Source: "bar",
Destination: "world",
},
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(10),
Amount: internal.NewMonetaryInt(10),
Source: "baz",
Destination: "world",
},
Expand Down Expand Up @@ -126,29 +126,29 @@ func TestOverdraftSourceInOrderSuccess(t *testing.T) {
tc.setBalance("baz", "GEM", 0)
tc.setBalance("qux", "GEM", 0)
tc.expected = CaseResult{
Printed: []internal2.Value{},
Printed: []internal.Value{},
Postings: []Posting{
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(10),
Amount: internal.NewMonetaryInt(10),
Source: "foo",
Destination: "world",
},
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(20),
Amount: internal.NewMonetaryInt(20),
Source: "bar",
Destination: "world",
},
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(20),
Amount: internal.NewMonetaryInt(20),
Source: "baz",
Destination: "world",
},
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(50),
Amount: internal.NewMonetaryInt(50),
Source: "qux",
Destination: "world",
},
Expand All @@ -175,23 +175,23 @@ func TestOverdraftBalanceTracking(t *testing.T) {
`)
tc.setBalance("foo", "GEM", 0)
tc.expected = CaseResult{
Printed: []internal2.Value{},
Printed: []internal.Value{},
Postings: []Posting{
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(100),
Amount: internal.NewMonetaryInt(100),
Source: "foo",
Destination: "world",
},
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(200),
Amount: internal.NewMonetaryInt(200),
Source: "foo",
Destination: "world",
},
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(300),
Amount: internal.NewMonetaryInt(300),
Source: "foo",
Destination: "world",
},
Expand All @@ -213,17 +213,17 @@ func TestWorldIsUnbounded(t *testing.T) {
)
`)
tc.expected = CaseResult{
Printed: []internal2.Value{},
Printed: []internal.Value{},
Postings: []Posting{
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(100),
Amount: internal.NewMonetaryInt(100),
Source: "world",
Destination: "foo",
},
{
Asset: "GEM",
Amount: internal2.NewMonetaryInt(200),
Amount: internal.NewMonetaryInt(200),
Source: "world",
Destination: "foo",
},
Expand All @@ -249,7 +249,7 @@ func TestOverdraftComplexFailure(t *testing.T) {
tc.setBalance("bar", "GEM", 20)
tc.setBalance("baz", "GEM", 0)
tc.expected = CaseResult{
Printed: []internal2.Value{},
Printed: []internal.Value{},
Postings: []Posting{},
Error: ledger.ErrInsufficientFund,
}
Expand All @@ -264,7 +264,7 @@ func TestNegativeBalance(t *testing.T) {
)`)
tc.setBalance("foo", "GEM", -50)
tc.expected = CaseResult{
Printed: []internal2.Value{},
Printed: []internal.Value{},
Postings: []Posting{},
Error: ledger.ErrInsufficientFund,
}
Expand Down
14 changes: 7 additions & 7 deletions components/ledger/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ paths:
Count:
schema:
type: integer
format: int64
format: bigint
minimum: 0
default:
description: Error
Expand Down Expand Up @@ -555,7 +555,7 @@ paths:
required: true
schema:
type: integer
format: int64
format: bigint
minimum: 0
example: 1234
- name: expand
Expand Down Expand Up @@ -605,7 +605,7 @@ paths:
required: true
schema:
type: integer
format: int64
format: bigint
minimum: 0
example: 1234
- name: dryRun
Expand Down Expand Up @@ -658,7 +658,7 @@ paths:
required: true
schema:
type: integer
format: int64
format: bigint
minimum: 0
example: 1234
- name: key
Expand Down Expand Up @@ -1109,7 +1109,7 @@ components:
$ref: '#/components/schemas/Metadata'
id:
type: integer
format: int64
format: bigint
minimum: 0
reverted:
type: boolean
Expand Down Expand Up @@ -1172,7 +1172,7 @@ components:
minimum: 0
transactions:
type: integer
format: int64
format: bigint
minimum: 0
required:
- accounts
Expand All @@ -1183,7 +1183,7 @@ components:
properties:
id:
type: integer
format: int64
format: bigint
minimum: 0
example: 1234
type:
Expand Down
14 changes: 7 additions & 7 deletions openapi/build/generate.json
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@
"Count": {
"schema": {
"type": "integer",
"format": "int64",
"format": "bigint",
"minimum": 0
}
}
Expand Down Expand Up @@ -1424,7 +1424,7 @@
"required": true,
"schema": {
"type": "integer",
"format": "int64",
"format": "bigint",
"minimum": 0,
"example": 1234
}
Expand Down Expand Up @@ -1499,7 +1499,7 @@
"required": true,
"schema": {
"type": "integer",
"format": "int64",
"format": "bigint",
"minimum": 0,
"example": 1234
}
Expand Down Expand Up @@ -1577,7 +1577,7 @@
"required": true,
"schema": {
"type": "integer",
"format": "int64",
"format": "bigint",
"minimum": 0,
"example": 1234
}
Expand Down Expand Up @@ -4668,7 +4668,7 @@
},
"id": {
"type": "integer",
"format": "int64",
"format": "bigint",
"minimum": 0
},
"reverted": {
Expand Down Expand Up @@ -4756,7 +4756,7 @@
},
"transactions": {
"type": "integer",
"format": "int64",
"format": "bigint",
"minimum": 0
}
},
Expand All @@ -4770,7 +4770,7 @@
"properties": {
"id": {
"type": "integer",
"format": "int64",
"format": "bigint",
"minimum": 0,
"example": 1234
},
Expand Down
Loading

1 comment on commit f34d53c

@vercel
Copy link

@vercel vercel bot commented on f34d53c Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.