poet.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package poet
  2. import (
  3. "github.com/kyleconroy/sqlc/internal/python/ast"
  4. )
  5. type proto interface {
  6. ProtoMessage()
  7. }
  8. func Nodes(nodes ...proto) []*ast.Node {
  9. list := make([]*ast.Node, len(nodes))
  10. for i, _ := range nodes {
  11. list[i] = Node(nodes[i])
  12. }
  13. return list
  14. }
  15. func Node(node proto) *ast.Node {
  16. switch n := node.(type) {
  17. case *ast.Alias:
  18. return &ast.Node{
  19. Node: &ast.Node_Alias{
  20. Alias: n,
  21. },
  22. }
  23. case *ast.Await:
  24. return &ast.Node{
  25. Node: &ast.Node_Await{
  26. Await: n,
  27. },
  28. }
  29. case *ast.AnnAssign:
  30. return &ast.Node{
  31. Node: &ast.Node_AnnAssign{
  32. AnnAssign: n,
  33. },
  34. }
  35. case *ast.Assign:
  36. return &ast.Node{
  37. Node: &ast.Node_Assign{
  38. Assign: n,
  39. },
  40. }
  41. case *ast.AsyncFor:
  42. return &ast.Node{
  43. Node: &ast.Node_AsyncFor{
  44. AsyncFor: n,
  45. },
  46. }
  47. case *ast.AsyncFunctionDef:
  48. return &ast.Node{
  49. Node: &ast.Node_AsyncFunctionDef{
  50. AsyncFunctionDef: n,
  51. },
  52. }
  53. case *ast.Attribute:
  54. return &ast.Node{
  55. Node: &ast.Node_Attribute{
  56. Attribute: n,
  57. },
  58. }
  59. case *ast.Call:
  60. return &ast.Node{
  61. Node: &ast.Node_Call{
  62. Call: n,
  63. },
  64. }
  65. case *ast.ClassDef:
  66. return &ast.Node{
  67. Node: &ast.Node_ClassDef{
  68. ClassDef: n,
  69. },
  70. }
  71. case *ast.Comment:
  72. return &ast.Node{
  73. Node: &ast.Node_Comment{
  74. Comment: n,
  75. },
  76. }
  77. case *ast.Compare:
  78. return &ast.Node{
  79. Node: &ast.Node_Compare{
  80. Compare: n,
  81. },
  82. }
  83. // case *ast.Constant:
  84. // case *ast.Dict:
  85. case *ast.Expr:
  86. return &ast.Node{
  87. Node: &ast.Node_Expr{
  88. Expr: n,
  89. },
  90. }
  91. case *ast.For:
  92. return &ast.Node{
  93. Node: &ast.Node_For{
  94. For: n,
  95. },
  96. }
  97. case *ast.FunctionDef:
  98. return &ast.Node{
  99. Node: &ast.Node_FunctionDef{
  100. FunctionDef: n,
  101. },
  102. }
  103. case *ast.If:
  104. return &ast.Node{
  105. Node: &ast.Node_If{
  106. If: n,
  107. },
  108. }
  109. // case *ast.Node_Import:
  110. // w.printImport(n.Import, indent)
  111. // case *ast.Node_ImportFrom:
  112. // w.printImportFrom(n.ImportFrom, indent)
  113. // case *ast.Node_Is:
  114. // w.print("is")
  115. // case *ast.Node_Keyword:
  116. // w.printKeyword(n.Keyword, indent)
  117. case *ast.Module:
  118. return &ast.Node{
  119. Node: &ast.Node_Module{
  120. Module: n,
  121. },
  122. }
  123. // w.printModule(n.Module, indent)
  124. // case *ast.Node_Name:
  125. // w.print(n.Name.Id)
  126. // case *ast.Node_Pass:
  127. // w.print("pass")
  128. // case *ast.Node_Return:
  129. // w.printReturn(n.Return, indent)
  130. // case *ast.Node_Subscript:
  131. // w.printSubscript(n.Subscript, indent)
  132. case *ast.Yield:
  133. return &ast.Node{
  134. Node: &ast.Node_Yield{
  135. Yield: n,
  136. },
  137. }
  138. default:
  139. panic(n)
  140. }
  141. }
  142. func Constant(value interface{}) *ast.Node {
  143. switch n := value.(type) {
  144. case string:
  145. return &ast.Node{
  146. Node: &ast.Node_Constant{
  147. Constant: &ast.Constant{
  148. Value: &ast.Constant_Str{
  149. Str: n,
  150. },
  151. },
  152. },
  153. }
  154. case int:
  155. return &ast.Node{
  156. Node: &ast.Node_Constant{
  157. Constant: &ast.Constant{
  158. Value: &ast.Constant_Int{
  159. Int: int32(n),
  160. },
  161. },
  162. },
  163. }
  164. case nil:
  165. return &ast.Node{
  166. Node: &ast.Node_Constant{
  167. Constant: &ast.Constant{
  168. Value: &ast.Constant_None{},
  169. },
  170. },
  171. }
  172. default:
  173. panic("unknown type")
  174. }
  175. }