-
Notifications
You must be signed in to change notification settings - Fork 5
feat: specs format #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
40308f2
feat: define and parse specs format
ascandone 782292b
quick and dirty prototype
ascandone 4b6f9bd
prototype v2
ascandone 3e9beba
colored diff!
ascandone 4264a63
minor
ascandone 8e7ea3d
proto
ascandone db11997
impl better UI for balances
ascandone 9fa0cad
improve prettyprint function
ascandone 8a9abff
improve pprint
ascandone d4218a8
improve ui
ascandone 434e8ec
improve run cmd ui
ascandone 9d324b7
improve UI
ascandone a2c4f96
refactor
ascandone f6983ae
edit col
ascandone 0e625ab
show files stats
ascandone 9d5df76
improve schema
ascandone b6631bd
change field name
ascandone 7ab8d47
add col
ascandone 6e26269
add util
ascandone ab69c69
add interactive mode mvp
ascandone 59c01eb
allow feature flags
ascandone 81c6eb2
refactor assertions
ascandone c6e87af
show failed assertion's name
ascandone 7f1c691
better handling of panic
ascandone b6d0624
refactor
ascandone f862e77
improve err
ascandone ef9fcf1
test cmd flags
ascandone a620797
removed comments
ascandone 9dec055
refactor
ascandone a8ba47b
WIP broken
ascandone c6d8d56
fix assertions
ascandone a3dfec2
fix newline
ascandone dc28320
defined schema for specs format
ascandone 420fd5c
improve description
ascandone 33ee124
change fields name
ascandone 6394369
change color
ascandone 779d6c3
fix schema
ascandone 2262251
add required field to schema
ascandone d695d09
update schema
ascandone be21b89
minor
ascandone 1d9901f
polish errors
ascandone 8bc2ded
fix
ascandone f099c1c
linter fix
ascandone a888a9d
improve coverage
ascandone d905a87
improved coverage
ascandone a7a2285
more tests
ascandone da404ed
removed comment
ascandone 46ab23d
removed dead code
ascandone 1618b8b
edit gitignore
ascandone 856a89b
more tests
ascandone db16df7
fix lint errs
ascandone 32d6e05
removed unused code
ascandone 6f9f25c
fix typo
ascandone 29a351f
refactor: moved files in the specs_format module
ascandone 1302479
feat: make it work recursively
ascandone 16d12a8
feat: also allow single file
ascandone 5829de1
fix
ascandone 4eead8a
refactor: moved function in different module
ascandone 6be6937
feat: renamed fields
ascandone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,5 @@ | |
| dist/ | ||
|
|
||
| coverage.* | ||
|
|
||
| .direnv | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,95 @@ | ||
| package ansi | ||
|
|
||
| import "fmt" | ||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| const resetCol = "\033[0m" | ||
|
|
||
| func Compose(cols ...func(string) string) func(string) string { | ||
| return func(s string) string { | ||
| for _, mod := range cols { | ||
| s = mod(s) | ||
| } | ||
| return s | ||
| } | ||
| } | ||
|
|
||
| func replaceLast(s, oldStr, newStr string) string { | ||
| lastIndex := strings.LastIndex(s, oldStr) | ||
| if lastIndex == -1 { | ||
| return s | ||
| } | ||
| return s[:lastIndex] + newStr + s[lastIndex+len(oldStr):] | ||
| } | ||
|
|
||
| func col(s string, code int) string { | ||
| c := fmt.Sprintf("\033[%dm", code) | ||
| return c + s + resetCol | ||
| colorCode := fmt.Sprintf("\033[%dm", code) | ||
| // This trick should allow to stack colors (TODO test) | ||
| s = replaceLast(s, resetCol, resetCol+colorCode) | ||
| return colorCode + s + resetCol | ||
| } | ||
|
|
||
| func ColorRed(s string) string { | ||
| return col(s, 31) | ||
| } | ||
|
|
||
| func ColorWhite(s string) string { | ||
| return col(s, 37) | ||
| } | ||
|
|
||
| func ColorGreen(s string) string { | ||
| return col(s, 32) | ||
| } | ||
|
|
||
| func ColorYellow(s string) string { | ||
| return col(s, 33) | ||
| } | ||
|
|
||
| func ColorCyan(s string) string { | ||
| return col(s, 36) | ||
| } | ||
|
|
||
| func ColorLight(s string) string { | ||
| return col(s, 97) // Bright white → light | ||
| } | ||
|
|
||
| func ColorBrightBlack(s string) string { | ||
| return col(s, 90) | ||
| } | ||
|
|
||
| func ColorBrightRed(s string) string { | ||
| return col(s, 91) | ||
| } | ||
|
|
||
| func ColorBrightGreen(s string) string { | ||
| return col(s, 92) | ||
| } | ||
|
|
||
| func ColorBrightYellow(s string) string { | ||
| return col(s, 93) | ||
| } | ||
|
|
||
| // BG | ||
| func BgDark(s string) string { | ||
| return col(s, 100) | ||
| } | ||
|
|
||
| func BgRed(s string) string { | ||
| return col(s, 41) | ||
| } | ||
|
|
||
| func BgGreen(s string) string { | ||
| return col(s, 42) | ||
| } | ||
|
|
||
| // modifiers | ||
|
|
||
| func Bold(s string) string { | ||
| return col(s, 1) | ||
| } | ||
|
|
||
| func Underline(s string) string { | ||
| return col(s, 4) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "github.com/formancehq/numscript/internal/specs_format" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type testArgs struct { | ||
| paths []string | ||
| } | ||
|
|
||
| var opts = testArgs{} | ||
|
|
||
| func runTestCmd() { | ||
| files, err := specs_format.ReadSpecsFiles(opts.paths) | ||
| if err != nil { | ||
| _, _ = os.Stderr.Write([]byte(err.Error())) | ||
| os.Exit(1) | ||
| return | ||
| } | ||
|
|
||
| pass := specs_format.RunSpecs(os.Stdout, os.Stderr, files) | ||
| if !pass { | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func getTestCmd() *cobra.Command { | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "test folder...", | ||
| Short: "Test numscript file using the numscript specs format", | ||
| Long: `Searches for any <file>.num.specs files in the given directory (or directories), | ||
| and tests the corresponding <file>.num file (if any). | ||
| Defaults to "." if there are no given paths`, | ||
| Args: cobra.MatchAll(), | ||
| Run: func(cmd *cobra.Command, paths []string) { | ||
|
|
||
| if len(paths) == 0 { | ||
| paths = []string{"."} | ||
| } | ||
|
|
||
| opts.paths = paths | ||
| runTestCmd() | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
|
|
||
| [TestPrettyPrintBalance - 1] | ||
| | [36mAccount[0m | [36mAsset[0m | [36mBalance[0m | | ||
| | alice | EUR/2 | 1 | | ||
| | alice | USD/1234 | 999999 | | ||
| | bob | BTC | 3 | | ||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package interpreter | ||
|
|
||
| import ( | ||
| "github.com/formancehq/numscript/internal/utils" | ||
| ) | ||
|
|
||
| func (m AccountsMetadata) fetchAccountMetadata(account string) AccountMetadata { | ||
| return utils.MapGetOrPutDefault(m, account, func() AccountMetadata { | ||
| return AccountMetadata{} | ||
| }) | ||
| } | ||
|
|
||
| func (m AccountsMetadata) DeepClone() AccountsMetadata { | ||
| cloned := make(AccountsMetadata) | ||
| for account, accountBalances := range m { | ||
| for asset, metadataValue := range accountBalances { | ||
| clonedAccountBalances := cloned.fetchAccountMetadata(account) | ||
| utils.MapGetOrPutDefault(clonedAccountBalances, asset, func() string { | ||
| return metadataValue | ||
| }) | ||
| } | ||
| } | ||
| return cloned | ||
| } | ||
|
|
||
| func (m AccountsMetadata) Merge(update AccountsMetadata) { | ||
| for acc, accBalances := range update { | ||
| cachedAcc := utils.MapGetOrPutDefault(m, acc, func() AccountMetadata { | ||
| return AccountMetadata{} | ||
| }) | ||
|
|
||
| for curr, amt := range accBalances { | ||
| cachedAcc[curr] = amt | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (m AccountsMetadata) PrettyPrint() string { | ||
| header := []string{"Account", "Name", "Value"} | ||
|
|
||
| var rows [][]string | ||
| for account, accMetadata := range m { | ||
| for name, value := range accMetadata { | ||
| row := []string{account, name, value} | ||
| rows = append(rows, row) | ||
| } | ||
| } | ||
|
|
||
| return utils.CsvPretty(header, rows, true) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.