Skip to content

Commit

Permalink
Fix crash while parsing args
Browse files Browse the repository at this point in the history
  • Loading branch information
dz0ny committed Nov 11, 2024
1 parent ce49269 commit 4fbdd48
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
18 changes: 12 additions & 6 deletions cli/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ import (

type arrayExtra []subsetter.Rule

func (i *arrayExtra) String() string {
return fmt.Sprintf("%v", *i)
func (ae *arrayExtra) String() string {
return fmt.Sprintf("%v", *ae)
}

func (i *arrayExtra) Set(value string) error {
func (ae *arrayExtra) Set(value string) error {
q := strings.SplitN(strings.TrimSpace(value), ":", 2)

*i = append(*i, subsetter.Rule{
Table: strings.TrimSpace(q[0]),
Where: maybeAll(strings.TrimSpace(q[1])),
table := strings.TrimSpace(q[0])
where := ""
if len(q) > 1 {
where = strings.TrimSpace(q[1])
}

*ae = append(*ae, subsetter.Rule{
Table: table,
Where: maybeAll(where),
})
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions cli/extra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func Test_arrayExtra_Set(t *testing.T) {
wantErr bool
}{
{"With tables", "simple: id < 10", arrayExtra{{Table: "simple", Where: "id < 10"}}, false},
{"With tables and all", "simple: all", arrayExtra{{Table: "simple", Where: "1=1"}}, false},
{"With tables only", "simple", arrayExtra{{Table: "simple"}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
22 changes: 22 additions & 0 deletions subsetter/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import (
"github.com/stevenle/topsort"
)

// TableGraph generates a topologically sorted list of table names based on their relations.
// It takes a primary table name and a slice of Relation objects as input.
// The function returns a slice of strings representing the sorted table names and an error if any.
//
// Parameters:
// - primary: The name of the primary table to start the topological sort from.
// - relations: A slice of Relation objects representing the relationships between tables.
//
// Returns:
// - l: A slice of strings representing the topologically sorted table names.
// - err: An error if the topological sort fails or if there is an issue adding edges to the graph.
func TableGraph(primary string, relations []Relation) (l []string, err error) {
graph := topsort.NewGraph() // Create a new graph

Expand All @@ -25,6 +36,17 @@ func TableGraph(primary string, relations []Relation) (l []string, err error) {
return
}

// RequiredTableGraph generates a list of required tables in topological order
// starting from the primary table. It uses the provided relations to build a
// directed graph and performs a topological sort.
//
// Parameters:
// - primary: The name of the primary table to start the topological sort from.
// - relations: A slice of Relation structs that define the relationships between tables.
//
// Returns:
// - l: A slice of strings representing the tables in topological order.
// - err: An error if the graph construction or topological sort fails.
func RequiredTableGraph(primary string, relations []Relation) (l []string, err error) {
graph := topsort.NewGraph() // Create a new graph

Expand Down

0 comments on commit 4fbdd48

Please sign in to comment.