Swagger generator now filters according to passed verbs.

Olivier Tremblay 4 years ago
parent
commit
53a13c92e2
2 changed files with 58 additions and 47 deletions
  1. 1 1
      lib/libautoapi.go
  2. 57 46
      lib/swaggergenerator.go

+ 1 - 1
lib/libautoapi.go

@@ -123,7 +123,7 @@ func Generate(db *sql.DB, dbName string, verbs string) error {
123 123
 		fmt.Println("failed generating checksumcode")
124 124
 		return err
125 125
 	}
126
-	err = (&swaggerGenerator{dbName}).Generate(tables)
126
+	err = (&swaggerGenerator{dbname: dbName, verbs: verbs}).Generate(tables)
127 127
 	if err != nil {
128 128
 		fmt.Println("failed generating swagger json")
129 129
 		return err

+ 57 - 46
lib/swaggergenerator.go

@@ -5,10 +5,14 @@ import (
5 5
 	"encoding/json"
6 6
 	"fmt"
7 7
 	"os"
8
+	"strings"
8 9
 	"text/template"
9 10
 )
10 11
 
11
-type swaggerGenerator struct{ dbname string }
12
+type swaggerGenerator struct {
13
+	dbname string
14
+	verbs  string
15
+}
12 16
 
13 17
 type msi map[string]interface{}
14 18
 
@@ -30,7 +34,7 @@ func (sg *swaggerGenerator) Generate(tables map[string]tableInfo) error {
30 34
 
31 35
 	consumes := []string{"application/json"}
32 36
 	produces := consumes
33
-
37
+	verbs := strings.Split(sg.verbs, ",")
34 38
 	for _, t := range tables {
35 39
 
36 40
 		refdef := fmt.Sprintf("#/definitions/%s", t.NormalizedTableName())
@@ -61,60 +65,67 @@ func (sg *swaggerGenerator) Generate(tables map[string]tableInfo) error {
61 65
 
62 66
 		definitions[t.TableName] = def
63 67
 		request := msi{}
64
-		request["get"] = msi{
65
-			"produces": produces,
66
-			"responses": msi{
67
-				"200": msi{
68
-					"schema": msi{
69
-						"$ref": refdef,
68
+		if stringInSlice("get", verbs) {
69
+			request["get"] = msi{
70
+				"produces": produces,
71
+				"responses": msi{
72
+					"200": msi{
73
+						"schema": msi{
74
+							"$ref": refdef,
75
+						},
76
+						"type": "array",
70 77
 					},
71
-					"type": "array",
78
+					"400": msi{"description": "Empty collection"},
72 79
 				},
73
-				"400": msi{"description": "Empty collection"},
74
-			},
80
+			}
75 81
 		}
76
-
77
-		request["post"] = msi{
78
-			"consumes":   consumes,
79
-			"produces":   produces,
80
-			"responses":  msi{"405": msi{"description": "Invalid input"}},
81
-			"parameters": []msi{bodydef},
82
+		if stringInSlice("post", verbs) {
83
+			request["post"] = msi{
84
+				"consumes":   consumes,
85
+				"produces":   produces,
86
+				"responses":  msi{"405": msi{"description": "Invalid input"}},
87
+				"parameters": []msi{bodydef},
88
+			}
82 89
 		}
83 90
 
84 91
 		paths[fmt.Sprintf("/%s", t.CamelCaseTableName())] = request
85 92
 		if len(t.PrimaryColumns()) > 0 {
86 93
 			request = msi{}
87
-			request["get"] = msi{
88
-				"produces": produces,
89
-				"responses": msi{
90
-					"404": msi{"description": "Not Found"},
91
-					"200": msi{"$ref": refdef},
92
-				},
93
-				"parameters": []msi{idparam},
94
+			if stringInSlice("get", verbs) {
95
+				request["get"] = msi{
96
+					"produces": produces,
97
+					"responses": msi{
98
+						"404": msi{"description": "Not Found"},
99
+						"200": msi{"$ref": refdef},
100
+					},
101
+					"parameters": []msi{idparam},
102
+				}
94 103
 			}
95
-
96
-			request["put"] = msi{
97
-				"consumes": consumes,
98
-				"produces": produces,
99
-				"responses": msi{
100
-					"404": msi{"description": "Not Found"},
101
-					"405": msi{"description": "Invalid input"},
102
-				},
103
-				"parameters": []msi{
104
-					idparam,
105
-					bodydef,
106
-				},
104
+			if stringInSlice("put", verbs) {
105
+				request["put"] = msi{
106
+					"consumes": consumes,
107
+					"produces": produces,
108
+					"responses": msi{
109
+						"404": msi{"description": "Not Found"},
110
+						"405": msi{"description": "Invalid input"},
111
+					},
112
+					"parameters": []msi{
113
+						idparam,
114
+						bodydef,
115
+					},
116
+				}
107 117
 			}
108
-
109
-			request["delete"] = msi{
110
-				"consumes": consumes,
111
-				"produces": produces,
112
-				"responses": msi{
113
-					"404": msi{"description": "Not Found"},
114
-				},
115
-				"parameters": []msi{
116
-					idparam,
117
-				},
118
+			if stringInSlice("delete", verbs) {
119
+				request["delete"] = msi{
120
+					"consumes": consumes,
121
+					"produces": produces,
122
+					"responses": msi{
123
+						"404": msi{"description": "Not Found"},
124
+					},
125
+					"parameters": []msi{
126
+						idparam,
127
+					},
128
+				}
118 129
 			}
119 130
 			paths[fmt.Sprintf("/%s/{%s}", t.CamelCaseTableName(), t.PrimaryColumns()[0].LowercaseColumnName())] = request
120 131
 		}