Skip to content

Commit f7a1d9d

Browse files
committed
✨ Nicer plugin development UX.
- There is less boilerplate for plugin development (insert/update/delete methods aren't required anymore) - The plugin scafolding tool download all dependencies now
1 parent 766bb11 commit f7a1d9d

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

controller/dev.go

+27-11
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,20 @@ import "github.com/julien040/anyquery/rpc"
3939
//
4040
// It should return a new table instance, the database schema and if there is an error
4141
func {{.TableName}}Creator(args rpc.TableCreatorArgs) (rpc.Table, *rpc.DatabaseSchema, error) {
42-
// Example: get a token from the user configuration
42+
// Get a token from the user configuration
4343
// token := args.UserConfig.GetString("token")
4444
// if token == "" {
4545
// return nil, nil, fmt.Errorf("token must be set in the plugin configuration")
4646
// }
4747
48-
// Example: open a cache connection
48+
// Open a cache connection
4949
/* cache, err := helper.NewCache(helper.NewCacheArgs{
5050
Paths: []string{"{{.TableName}}", "{{.TableName}}" + "_cache"},
5151
EncryptionKey: []byte("my_secret_key"),
5252
})*/
5353
5454
return &{{.TableName}}Table{}, &rpc.DatabaseSchema{
55-
HandlesInsert: false,
56-
HandlesUpdate: false,
57-
HandlesDelete: false,
58-
HandleOffset: false,
5955
Columns: []rpc.DatabaseSchemaColumn{
60-
{
61-
Name: "id",
62-
Type: rpc.ColumnTypeString,
63-
},
6456
{
6557
// This column is a parameter
6658
// Therefore, it'll be hidden in SELECT * but will be used in WHERE clauses
@@ -70,6 +62,11 @@ func {{.TableName}}Creator(args rpc.TableCreatorArgs) (rpc.Table, *rpc.DatabaseS
7062
IsParameter: true,
7163
IsRequired: false,
7264
},
65+
{
66+
Name: "id",
67+
Type: rpc.ColumnTypeString,
68+
},
69+
7370
},
7471
}, nil
7572
}
@@ -106,6 +103,8 @@ func (t *{{.TableName}}Cursor) Query(constraints rpc.QueryConstraint) ([][]inter
106103
}
107104
108105
// A slice of rows to insert
106+
// Uncomment the code to add support for inserting rows
107+
/*
109108
func (t *{{.TableName}}Table) Insert(rows [][]interface{}) error {
110109
// Example: insert the rows in a database
111110
// for _, row := range rows {
@@ -115,19 +114,25 @@ func (t *{{.TableName}}Table) Insert(rows [][]interface{}) error {
115114
// }
116115
return nil
117116
}
117+
*/
118118
119119
// A slice of rows to update
120120
// The first element of each row is the primary key
121121
// while the rest are the values to update
122122
// The primary key is therefore present twice
123+
// Uncomment the code to add support for updating rows
124+
/*
123125
func (t *{{.TableName}}Table) Update(rows [][]interface{}) error {
124126
return nil
125-
}
127+
}*/
126128
127129
// A slice of primary keys to delete
130+
// Uncomment the code to add support for deleting rows
131+
/*
128132
func (t *{{.TableName}}Table) Delete(primaryKeys []interface{}) error {
129133
return nil
130134
}
135+
*/
131136
132137
// A destructor to clean up resources
133138
func (t *{{.TableName}}Table) Close() error {
@@ -144,6 +149,9 @@ all: $(files)
144149
prod: $(files)
145150
go build -o {{.ModuleName}}.out -ldflags "-s -w" $(files)
146151
152+
release: prod
153+
goreleaser build -f .goreleaser.yaml --clean --snapshot
154+
147155
clean:
148156
rm -f {{.ModuleName}}.out
149157
@@ -352,6 +360,14 @@ func DevInit(cmd *cobra.Command, args []string) error {
352360
return fmt.Errorf("could not import rpc package: %w", err)
353361
}
354362

363+
goCmd = exec.Command("go", "get", "-u", "github.com/julien040/anyquery/rpc/helper")
364+
goCmd.Stdout = os.Stdout
365+
goCmd.Stderr = os.Stderr
366+
err = goCmd.Run()
367+
if err != nil {
368+
return fmt.Errorf("could not import rpc package: %w", err)
369+
}
370+
355371
// Create the main.go file
356372
mainFile, err := os.Create("main.go")
357373
if err != nil {

rpc/plugin.go

+13
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,19 @@ func (i *internalInterface) Initialize(connectionIndex int, tableIndex int, conf
272272
}
273273
i.plugin.tableConnection[tableKey{connectionIndex: connectionIndex, tableIndex: tableIndex}] = table
274274

275+
// Set to true each flag (insert, update, delete) if the table implements the corresponding interface
276+
if _, ok := table.(TableInsert); ok {
277+
schemaA.HandlesInsert = true
278+
}
279+
280+
if _, ok := table.(TableUpdate); ok {
281+
schemaA.HandlesUpdate = true
282+
}
283+
284+
if _, ok := table.(TableDelete); ok {
285+
schemaA.HandlesDelete = true
286+
}
287+
275288
return *schemaA, nil
276289

277290
}

0 commit comments

Comments
 (0)