-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathoas2.go
70 lines (58 loc) · 1.39 KB
/
oas2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Code generated by kun; DO NOT EDIT.
// github.com/RussellLuo/kun
package helloworld
import (
"reflect"
"github.com/RussellLuo/kun/pkg/oas2"
)
var (
base = `swagger: "2.0"
info:
title: "No Title"
version: "0.0.0"
description: "Service is used for saying hello."
license:
name: "MIT"
host: "example.com"
basePath: "/"
schemes:
- "https"
consumes:
- "application/json"
produces:
- "application/json"
`
paths = `
paths:
/messages:
post:
description: "SayHello says hello to the given name."
operationId: "SayHello"
parameters:
- name: body
in: body
schema:
$ref: "#/definitions/SayHelloRequestBody"
%s
`
)
func getResponses(schema oas2.Schema) []oas2.OASResponses {
return []oas2.OASResponses{
oas2.GetOASResponses(schema, "SayHello", 200, &SayHelloResponse{}),
}
}
func getDefinitions(schema oas2.Schema) map[string]oas2.Definition {
defs := make(map[string]oas2.Definition)
oas2.AddDefinition(defs, "SayHelloRequestBody", reflect.ValueOf(&struct {
Name string `json:"name"`
}{}))
oas2.AddResponseDefinitions(defs, schema, "SayHello", 200, (&SayHelloResponse{}).Body())
return defs
}
func OASv2APIDoc(schema oas2.Schema) string {
resps := getResponses(schema)
paths := oas2.GenPaths(resps, paths)
defs := getDefinitions(schema)
definitions := oas2.GenDefinitions(defs)
return base + paths + definitions
}