More work on #3, I should have a mostly parametrized http handler generated.

Olivier Tremblay 3 years ago
parent
commit
1a9e4d4d28
2 changed files with 27 additions and 9 deletions
  1. 4 9
      lib/httpcodegenerator.go
  2. 23 0
      lib/tableinfo.go

+ 4 - 9
lib/httpcodegenerator.go

@@ -2,7 +2,6 @@ package lib
2 2
 
3 3
 import (
4 4
 	"bytes"
5
-	"encoding/json"
6 5
 	"fmt"
7 6
 	"go/format"
8 7
 	"io"
@@ -51,7 +50,8 @@ func Get(res http.ResponseWriter, req *http.Request){
51 50
     {{if gt $l 1}}
52 51
         id_slice := strings.Split(vars["id"])
53 52
     {{else}}
54
-        vars["id"]
53
+        param := vars["id"]
54
+        {{.FirstPrimaryColumnTypeConverter}}
55 55
     {{end}}
56 56
     row, _ := {{.Table.TableName}}.GetBy{{.Table.PrimaryColumnsJoinedByAnd}}(id)
57 57
     enc.Encode(row)
@@ -76,18 +76,13 @@ func Delete(res http.ResponseWriter, req *http.Request){
76 76
 }
77 77
 
78 78
 `))
79
-	f, _ := os.Open("all.json")
80
-	schema := &jsonld{}
81
-	dec := json.NewDecoder(f)
82
-	dec.Decode(&schema)
79
+
83 80
 	path, err := GetRootPath()
84 81
 	if err != nil {
85 82
 		return err
86 83
 	}
87 84
 	for table, tinfo := range tables {
88
-		if m, ok := schema.Types[tinfo.NormalizedTableName()]; ok {
89
-			fmt.Println(m)
90
-		}
85
+
91 86
 		os.Mkdir("http/"+table, 0755)
92 87
 		f, err := os.Create("http/" + table + "/" + table + ".go")
93 88
 		if err != nil {

+ 23 - 0
lib/tableinfo.go

@@ -35,6 +35,29 @@ func (t tableInfo) NormalizedTableName() string {
35 35
 	return result
36 36
 }
37 37
 
38
+func (t tableInfo) FirstPrimaryColumnTypeConverter() string {
39
+	pcs := t.PrimaryColumns()
40
+	if len(pcs) > 0 {
41
+		preamble := `
42
+i, err := strconv.Atoi(param)
43
+if err != nil {
44
+fmt.Println(err)
45
+return
46
+}
47
+id := %s(i)
48
+`
49
+		x := pcs[0].MappedColumnType()
50
+		switch x {
51
+		case "int", "uint8", "int8", "uint16", "int16", "uint32", "int32", "uint64", "int64":
52
+			return fmt.Sprintf(preamble, x)
53
+		case "[]byte":
54
+			return "id := []byte(param)"
55
+		default:
56
+			return "id := param"
57
+		}
58
+	}
59
+}
60
+
38 61
 func (t tableInfo) PrimaryColumns() []tableColumn {
39 62
 	result := make([]tableColumn, 0, len(t.ColOrder))
40 63
 	for _, col := range t.ColOrder {