1- package dinosql
1+ package kotlin
22
33import (
44 "bufio"
@@ -10,6 +10,7 @@ import (
1010 "strings"
1111 "text/template"
1212
13+ "github.com/kyleconroy/sqlc/internal/dinosql"
1314 core "github.com/kyleconroy/sqlc/internal/pg"
1415
1516 "github.com/jinzhu/inflection"
@@ -137,12 +138,12 @@ type KtQuery struct {
137138}
138139
139140type KtGenerateable interface {
140- KtDataClasses (settings CombinedSettings ) []KtStruct
141- KtQueries (settings CombinedSettings ) []KtQuery
142- KtEnums (settings CombinedSettings ) []KtEnum
141+ KtDataClasses (settings dinosql. CombinedSettings ) []KtStruct
142+ KtQueries (settings dinosql. CombinedSettings ) []KtQuery
143+ KtEnums (settings dinosql. CombinedSettings ) []KtEnum
143144}
144145
145- func KtUsesType (r KtGenerateable , typ string , settings CombinedSettings ) bool {
146+ func KtUsesType (r KtGenerateable , typ string , settings dinosql. CombinedSettings ) bool {
146147 for _ , strct := range r .KtDataClasses (settings ) {
147148 for _ , f := range strct .Fields {
148149 if f .Type .Name == typ {
@@ -153,7 +154,7 @@ func KtUsesType(r KtGenerateable, typ string, settings CombinedSettings) bool {
153154 return false
154155}
155156
156- func KtImports (r KtGenerateable , settings CombinedSettings ) func (string ) [][]string {
157+ func KtImports (r KtGenerateable , settings dinosql. CombinedSettings ) func (string ) [][]string {
157158 return func (filename string ) [][]string {
158159 if filename == "Models.kt" {
159160 return ModelKtImports (r , settings )
@@ -167,7 +168,7 @@ func KtImports(r KtGenerateable, settings CombinedSettings) func(string) [][]str
167168 }
168169}
169170
170- func InterfaceKtImports (r KtGenerateable , settings CombinedSettings ) [][]string {
171+ func InterfaceKtImports (r KtGenerateable , settings dinosql. CombinedSettings ) [][]string {
171172 gq := r .KtQueries (settings )
172173 uses := func (name string ) bool {
173174 for _ , q := range gq {
@@ -211,7 +212,7 @@ func InterfaceKtImports(r KtGenerateable, settings CombinedSettings) [][]string
211212 return [][]string {stds }
212213}
213214
214- func ModelKtImports (r KtGenerateable , settings CombinedSettings ) [][]string {
215+ func ModelKtImports (r KtGenerateable , settings dinosql. CombinedSettings ) [][]string {
215216 std := make (map [string ]struct {})
216217 if KtUsesType (r , "LocalDate" , settings ) {
217218 std ["java.time.LocalDate" ] = struct {}{}
@@ -235,7 +236,7 @@ func ModelKtImports(r KtGenerateable, settings CombinedSettings) [][]string {
235236 return [][]string {stds }
236237}
237238
238- func QueryKtImports (r KtGenerateable , settings CombinedSettings , filename string ) [][]string {
239+ func QueryKtImports (r KtGenerateable , settings dinosql. CombinedSettings , filename string ) [][]string {
239240 // for _, strct := range r.KtDataClasses() {
240241 // for _, f := range strct.Fields {
241242 // if strings.HasPrefix(f.Type, "[]") {
@@ -320,7 +321,15 @@ func ktEnumValueName(value string) string {
320321 return strings .ToUpper (id )
321322}
322323
323- func (r Result ) KtEnums (settings CombinedSettings ) []KtEnum {
324+ // Result is a wrapper around *dinosql.Result that extends it with Kotlin support.
325+ // It can be used to generate both Go and Kotlin code.
326+ // TODO: This is a temporary hack to ensure minimal chance of merge conflicts while Kotlin support is forked.
327+ // Once it is merged upstream, we can factor split out Go support from the core dinosql.Result.
328+ type Result struct {
329+ * dinosql.Result
330+ }
331+
332+ func (r Result ) KtEnums (settings dinosql.CombinedSettings ) []KtEnum {
324333 var enums []KtEnum
325334 for name , schema := range r .Catalog .Schemas {
326335 if name == "pg_catalog" {
@@ -353,7 +362,7 @@ func (r Result) KtEnums(settings CombinedSettings) []KtEnum {
353362 return enums
354363}
355364
356- func KtDataClassName (name string , settings CombinedSettings ) string {
365+ func KtDataClassName (name string , settings dinosql. CombinedSettings ) string {
357366 if rename := settings .Global .Rename [name ]; rename != "" {
358367 return rename
359368 }
@@ -364,11 +373,11 @@ func KtDataClassName(name string, settings CombinedSettings) string {
364373 return out
365374}
366375
367- func KtMemberName (name string , settings CombinedSettings ) string {
368- return LowerTitle (KtDataClassName (name , settings ))
376+ func KtMemberName (name string , settings dinosql. CombinedSettings ) string {
377+ return dinosql . LowerTitle (KtDataClassName (name , settings ))
369378}
370379
371- func (r Result ) KtDataClasses (settings CombinedSettings ) []KtStruct {
380+ func (r Result ) KtDataClasses (settings dinosql. CombinedSettings ) []KtStruct {
372381 var structs []KtStruct
373382 for name , schema := range r .Catalog .Schemas {
374383 if name == "pg_catalog" {
@@ -475,7 +484,7 @@ func (t ktType) fromJDBCValue(expr string) string {
475484 return expr
476485}
477486
478- func (r Result ) ktType (col core.Column , settings CombinedSettings ) ktType {
487+ func (r Result ) ktType (col core.Column , settings dinosql. CombinedSettings ) ktType {
479488 typ , isEnum := r .ktInnerType (col , settings )
480489 return ktType {
481490 Name : typ ,
@@ -486,7 +495,7 @@ func (r Result) ktType(col core.Column, settings CombinedSettings) ktType {
486495 }
487496}
488497
489- func (r Result ) ktInnerType (col core.Column , settings CombinedSettings ) (string , bool ) {
498+ func (r Result ) ktInnerType (col core.Column , settings dinosql. CombinedSettings ) (string , bool ) {
490499 columnType := col .DataType
491500
492501 switch columnType {
@@ -582,7 +591,7 @@ func (r Result) ktInnerType(col core.Column, settings CombinedSettings) (string,
582591 }
583592}
584593
585- func (r Result ) ktColumnsToStruct (name string , columns []core.Column , settings CombinedSettings ) * KtStruct {
594+ func (r Result ) ktColumnsToStruct (name string , columns []core.Column , settings dinosql. CombinedSettings ) * KtStruct {
586595 gs := KtStruct {
587596 Name : name ,
588597 }
@@ -613,7 +622,7 @@ func ktArgName(name string) string {
613622 return out
614623}
615624
616- func ktParamName (p Parameter ) string {
625+ func ktParamName (p dinosql. Parameter ) string {
617626 if p .Column .Name != "" {
618627 return ktArgName (p .Column .Name )
619628 }
@@ -636,7 +645,7 @@ func jdbcSQL(s string) string {
636645 return jdbcSQLRe .ReplaceAllString (s , "?" )
637646}
638647
639- func (r Result ) KtQueries (settings CombinedSettings ) []KtQuery {
648+ func (r Result ) KtQueries (settings dinosql. CombinedSettings ) []KtQuery {
640649 structs := r .KtDataClasses (settings )
641650
642651 qs := make ([]KtQuery , 0 , len (r .Queries ))
@@ -651,9 +660,9 @@ func (r Result) KtQueries(settings CombinedSettings) []KtQuery {
651660 gq := KtQuery {
652661 Cmd : query .Cmd ,
653662 ClassName : strings .Title (query .Name ),
654- ConstantName : LowerTitle (query .Name ),
655- FieldName : LowerTitle (query .Name ) + "Stmt" ,
656- MethodName : LowerTitle (query .Name ),
663+ ConstantName : dinosql . LowerTitle (query .Name ),
664+ FieldName : dinosql . LowerTitle (query .Name ) + "Stmt" ,
665+ MethodName : dinosql . LowerTitle (query .Name ),
657666 SourceName : query .Filename ,
658667 SQL : jdbcSQL (query .SQL ),
659668 Comments : query .Comments ,
@@ -898,7 +907,7 @@ type ktTmplCtx struct {
898907 Enums []KtEnum
899908 KtDataClasses []KtStruct
900909 KtQueries []KtQuery
901- Settings GenerateSettings
910+ Settings dinosql. GenerateSettings
902911
903912 // TODO: Race conditions
904913 SourceName string
@@ -928,9 +937,9 @@ func ktFormat(s string) string {
928937 return o
929938}
930939
931- func KtGenerate (r KtGenerateable , settings CombinedSettings ) (map [string ]string , error ) {
940+ func KtGenerate (r KtGenerateable , settings dinosql. CombinedSettings ) (map [string ]string , error ) {
932941 funcMap := template.FuncMap {
933- "lowerTitle" : LowerTitle ,
942+ "lowerTitle" : dinosql . LowerTitle ,
934943 "imports" : KtImports (r , settings ),
935944 "offset" : Offset ,
936945 }
0 commit comments