Skip to content

Commit 09ee5c5

Browse files
authored
refactor (#21)
* refactor * update * update * update
1 parent 764afd3 commit 09ee5c5

File tree

6 files changed

+7
-25
lines changed

6 files changed

+7
-25
lines changed

field.go

-5
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@ package main
33
import (
44
"fmt"
55
"github.com/swuecho/sqlc-fs/internal/sdk"
6-
"strings"
76
)
87

98
type Field struct {
109
Name string // CamelCased name for Go
1110
DBName string // Name as used in the DB
1211
Type string
13-
TypecheckTemplate string
1412
Comment string
1513
}
1614

17-
func (f Field) Typecheck(i int) string {
18-
return strings.ReplaceAll(f.TypecheckTemplate, "%", fmt.Sprintf("row[%d]", i))
19-
}
2015

2116
func SetCaseStyle(name string, style string) string {
2217
switch style {

fs_type.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,6 @@ import (
88
"github.com/swuecho/sqlc-fs/internal/sdk"
99
)
1010

11-
func tsTypecheckTemplate(req *plugin.CodeGenRequest, col *plugin.Column) string {
12-
typ := postgresType(req, col)
13-
cond := fmt.Sprintf(`typeof %% !== "%s"`, typ)
14-
if !col.NotNull {
15-
cond = fmt.Sprintf(`%s && %% !== null`, cond)
16-
}
17-
18-
if col.IsArray {
19-
cond = fmt.Sprintf(`!Array.isArray(%%) || %%.some(e => %s)`, strings.ReplaceAll(cond, "%%", "e"))
20-
}
21-
22-
return cond
23-
}
2411

2512
func fsType(req *plugin.CodeGenRequest, col *plugin.Column) string {
2613
typ := postgresType(req, col)
@@ -256,5 +243,5 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
256243
}
257244
}
258245

259-
return "interface{}"
246+
return "string"
260247
}

gen.go

-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ func (t *tmplCtx) OutputQuery(sourceName string) bool {
3838
func generate(req *plugin.CodeGenRequest, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) {
3939
funcMap := template.FuncMap{
4040
"stem": sdk.Stem,
41-
"lowerTitle": sdk.LowerTitle,
42-
"snakeCase": sdk.ToSnakeCase,
4341
"pascalCase": sdk.ToPascalCase,
4442
"comment": sdk.DoubleSlashComment,
4543
"escape": sdk.EscapeBacktick,

result.go

-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ func columnsToStruct(req *plugin.CodeGenRequest, name string, columns []column,
229229
Name: fieldName,
230230
DBName: colName,
231231
Type: fsType(req, c.Column),
232-
TypecheckTemplate: tsTypecheckTemplate(req, c.Column),
233232
})
234233
if _, found := seen[baseFieldName]; !found {
235234
seen[baseFieldName] = []int{i}

testdata/gen/model.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"Table":{"schema":"public","name":"auth_user"},"Name":"AuthUser","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"password","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"last_login","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"is_superuser","DBName":"","Type":"bool","TypecheckTemplate":"","Comment":""},{"Name":"username","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"first_name","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"last_name","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"email","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"is_staff","DBName":"","Type":"bool","TypecheckTemplate":"","Comment":""},{"Name":"is_active","DBName":"","Type":"bool","TypecheckTemplate":"","Comment":""},{"Name":"date_joined","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"auth_user_management"},"Name":"AuthUserManagement","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"rate_limit","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"authors"},"Name":"Author","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"name","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"bio","DBName":"","Type":"string option","TypecheckTemplate":"","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_logs"},"Name":"ChatLog","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"session","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"question","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"answer","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_message"},"Name":"ChatMessage","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"uuid","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"role","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"content","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"score","DBName":"","Type":"float","TypecheckTemplate":"","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"is_deleted","DBName":"","Type":"bool","TypecheckTemplate":"","Comment":""},{"Name":"is_pin","DBName":"","Type":"bool","TypecheckTemplate":"","Comment":""},{"Name":"token_count","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"raw","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_model"},"Name":"ChatModel","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"name","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"label","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"is_default","DBName":"","Type":"bool","TypecheckTemplate":"","Comment":""},{"Name":"url","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"api_auth_header","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"api_auth_key","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"enable_per_mode_ratelimit","DBName":"","Type":"bool","TypecheckTemplate":"","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_prompt"},"Name":"ChatPrompt","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"uuid","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"role","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"content","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"score","DBName":"","Type":"float","TypecheckTemplate":"","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"is_deleted","DBName":"","Type":"bool","TypecheckTemplate":"","Comment":""},{"Name":"token_count","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_session"},"Name":"ChatSession","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"uuid","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"topic","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"active","DBName":"","Type":"bool","TypecheckTemplate":"","Comment":""},{"Name":"model","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"max_length","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"temperature","DBName":"","Type":"float","TypecheckTemplate":"","Comment":""},{"Name":"top_p","DBName":"","Type":"float","TypecheckTemplate":"","Comment":""},{"Name":"max_tokens","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"n","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"debug","DBName":"","Type":"bool","TypecheckTemplate":"","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_snapshot"},"Name":"ChatSnapshot","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"uuid","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"title","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"summary","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"model","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"tags","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"session","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"conversation","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"text","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"search_vector","DBName":"","Type":"string option","TypecheckTemplate":"","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"jwt_secrets"},"Name":"JwtSecret","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"name","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"secret","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"audience","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"user_active_chat_session"},"Name":"UserActiveChatSession","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","TypecheckTemplate":"","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"user_chat_model_privilege"},"Name":"UserChatModelPrivilege","Fields":[{"Name":"id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"chat_model_id","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"rate_limit","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","TypecheckTemplate":"","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","TypecheckTemplate":"","Comment":""}],"Comment":""}]
1+
[{"Table":{"schema":"public","name":"auth_user"},"Name":"AuthUser","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"password","DBName":"","Type":"string","Comment":""},{"Name":"last_login","DBName":"","Type":"DateTime","Comment":""},{"Name":"is_superuser","DBName":"","Type":"bool","Comment":""},{"Name":"username","DBName":"","Type":"string","Comment":""},{"Name":"first_name","DBName":"","Type":"string","Comment":""},{"Name":"last_name","DBName":"","Type":"string","Comment":""},{"Name":"email","DBName":"","Type":"string","Comment":""},{"Name":"is_staff","DBName":"","Type":"bool","Comment":""},{"Name":"is_active","DBName":"","Type":"bool","Comment":""},{"Name":"date_joined","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"auth_user_management"},"Name":"AuthUserManagement","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"rate_limit","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"authors"},"Name":"Author","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"name","DBName":"","Type":"string","Comment":""},{"Name":"bio","DBName":"","Type":"string option","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_logs"},"Name":"ChatLog","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"session","DBName":"","Type":"string","Comment":""},{"Name":"question","DBName":"","Type":"string","Comment":""},{"Name":"answer","DBName":"","Type":"string","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_message"},"Name":"ChatMessage","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","Comment":""},{"Name":"role","DBName":"","Type":"string","Comment":""},{"Name":"content","DBName":"","Type":"string","Comment":""},{"Name":"score","DBName":"","Type":"float","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","Comment":""},{"Name":"is_deleted","DBName":"","Type":"bool","Comment":""},{"Name":"is_pin","DBName":"","Type":"bool","Comment":""},{"Name":"token_count","DBName":"","Type":"int32","Comment":""},{"Name":"raw","DBName":"","Type":"string","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_model"},"Name":"ChatModel","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"name","DBName":"","Type":"string","Comment":""},{"Name":"label","DBName":"","Type":"string","Comment":""},{"Name":"is_default","DBName":"","Type":"bool","Comment":""},{"Name":"url","DBName":"","Type":"string","Comment":""},{"Name":"api_auth_header","DBName":"","Type":"string","Comment":""},{"Name":"api_auth_key","DBName":"","Type":"string","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"enable_per_mode_ratelimit","DBName":"","Type":"bool","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_prompt"},"Name":"ChatPrompt","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","Comment":""},{"Name":"role","DBName":"","Type":"string","Comment":""},{"Name":"content","DBName":"","Type":"string","Comment":""},{"Name":"score","DBName":"","Type":"float","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","Comment":""},{"Name":"is_deleted","DBName":"","Type":"bool","Comment":""},{"Name":"token_count","DBName":"","Type":"int32","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_session"},"Name":"ChatSession","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"topic","DBName":"","Type":"string","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"active","DBName":"","Type":"bool","Comment":""},{"Name":"model","DBName":"","Type":"string","Comment":""},{"Name":"max_length","DBName":"","Type":"int32","Comment":""},{"Name":"temperature","DBName":"","Type":"float","Comment":""},{"Name":"top_p","DBName":"","Type":"float","Comment":""},{"Name":"max_tokens","DBName":"","Type":"int32","Comment":""},{"Name":"n","DBName":"","Type":"int32","Comment":""},{"Name":"debug","DBName":"","Type":"bool","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_snapshot"},"Name":"ChatSnapshot","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"title","DBName":"","Type":"string","Comment":""},{"Name":"summary","DBName":"","Type":"string","Comment":""},{"Name":"model","DBName":"","Type":"string","Comment":""},{"Name":"tags","DBName":"","Type":"string","Comment":""},{"Name":"session","DBName":"","Type":"string","Comment":""},{"Name":"conversation","DBName":"","Type":"string","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"text","DBName":"","Type":"string","Comment":""},{"Name":"search_vector","DBName":"","Type":"string option","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"jwt_secrets"},"Name":"JwtSecret","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"name","DBName":"","Type":"string","Comment":""},{"Name":"secret","DBName":"","Type":"string","Comment":""},{"Name":"audience","DBName":"","Type":"string","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"user_active_chat_session"},"Name":"UserActiveChatSession","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"user_chat_model_privilege"},"Name":"UserChatModelPrivilege","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"chat_model_id","DBName":"","Type":"int32","Comment":""},{"Name":"rate_limit","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","Comment":""}],"Comment":""}]

testdata/sqlc.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
}
99
}
1010
],
11-
"sql": [
11+
"sql": [
1212
{
1313
"schema": "schema.sql",
1414
"queries": "query/",
1515
"engine": "postgresql",
1616
"codegen": [
1717
{
1818
"out": "gen",
19-
"plugin": "fs"
19+
"plugin": "fs",
20+
"options": {
21+
"async": true
22+
}
2023
}
2124
]
2225
}

0 commit comments

Comments
 (0)