Skip to content

Commit 08975d9

Browse files
committed
feat(core): add core config for parsing plugin options(main)
1 parent 0eddb9f commit 08975d9

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

internal/core/config.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package core
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/sqlc-dev/plugin-sdk-go/plugin"
7+
)
8+
9+
type Config struct {
10+
SqlDriver string `json:"sql_driver" yaml:"sql_driver"`
11+
ModelType string `json:"model_type" yaml:"model_type"`
12+
13+
Async bool
14+
}
15+
16+
func ParseConfig(req *plugin.GenerateRequest) (*Config, error) {
17+
var config Config
18+
if len(req.PluginOptions) == 0 {
19+
return &config, nil
20+
}
21+
if err := json.Unmarshal(req.PluginOptions, &config); err != nil {
22+
return nil, fmt.Errorf("unmarshalling plugin options: %w", err)
23+
}
24+
if config.SqlDriver == "" {
25+
config.SqlDriver = SQLDriverAioSQLite
26+
}
27+
val, err := isDriverAsync(config.SqlDriver)
28+
if err != nil {
29+
return nil, fmt.Errorf("invalid options: %s", err)
30+
}
31+
config.Async = val
32+
if config.ModelType == "" {
33+
config.ModelType = ModelTypeDataclass
34+
}
35+
if err := isModelTypeValid(config.ModelType); err != nil {
36+
return nil, fmt.Errorf("invalid options: %s", err)
37+
}
38+
return &config, nil
39+
}

0 commit comments

Comments
 (0)