|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "regexp" |
5 | | - "bytes" |
6 | | - "bufio" |
| 4 | + "strings" |
7 | 5 | ) |
8 | 6 |
|
9 | | - |
10 | 7 | type ApiIntermediate struct { |
11 | 8 | ApiVersion string |
12 | 9 | ApiTitle string |
13 | 10 | ApiDescription string |
14 | 11 | BasePath string |
15 | | - SubApis []SubApiIntermediate |
16 | 12 | } |
17 | 13 |
|
18 | | - |
19 | 14 | func intermediatateApi(commentBlocks []string) ApiIntermediate { |
20 | 15 |
|
21 | | - // @APIVersion 1.0.0 |
22 | | - // @APITitle REST API |
23 | | - // @APIDescription EMS Rest API |
24 | | - // @BasePath /api/v1 |
25 | | - // @SubApi HealthCheck [/health] |
26 | | - |
27 | | - var ( |
28 | | - // At the time of writing, IntelliJ erroneously warns on unnecessary |
29 | | - // escape sequences. Do not trust IntelliJ. |
30 | | - rxApiVersion *regexp.Regexp = regexp.MustCompile(`@APIVersion\s+([\d\.]+)`) |
31 | | - rxApiTitle *regexp.Regexp = regexp.MustCompile(`@APITitle\s+(.+)`) |
32 | | - rxApiDescription *regexp.Regexp = regexp.MustCompile(`@APIDescription\s+(.+)`) |
33 | | - rxBasePath *regexp.Regexp = regexp.MustCompile(`@BasePath\s+([/a-zA-Z0-9-]+)`) |
34 | | - rxSubApi *regexp.Regexp = regexp.MustCompile(`@SubApi\s+([0-9a-zA-Z]+)\s+\[([/a-zA-Z0-9-]+)\]`) |
35 | | - ) |
| 16 | + /* |
| 17 | + OpenAPI API Title: |
| 18 | + Agame Public API |
| 19 | + OpenAPI API Description: |
| 20 | + The Agame Public API is the API that is exposed to the world to facilitate |
| 21 | + gameplay. |
| 22 | + OpenAPI API Version: |
| 23 | + 1.0 |
| 24 | + OpenAPI Base Path: |
| 25 | + /api |
| 26 | + */ |
36 | 27 |
|
37 | | - var apiIntermediate ApiIntermediate = ApiIntermediate{ |
38 | | - SubApis: make([]SubApiIntermediate, 0), |
39 | | - } |
| 28 | + var apiIntermediate ApiIntermediate = ApiIntermediate{} |
40 | 29 |
|
41 | 30 | for _, commentBlock := range commentBlocks { |
42 | 31 |
|
43 | | - b := bytes.NewBufferString(commentBlock) |
44 | | - scanner := bufio.NewScanner(b) |
45 | | - for scanner.Scan() { |
46 | | - line := scanner.Text() |
| 32 | + sections := parseSections(commentBlock) |
| 33 | + |
| 34 | + for _, section := range sections { |
47 | 35 |
|
48 | | - switch { |
| 36 | + //log.Print(section) |
49 | 37 |
|
50 | | - case rxApiDescription.MatchString(line): |
51 | | - apiIntermediate.ApiDescription = rxApiDescription.FindStringSubmatch(line)[1] |
52 | | - case rxApiTitle.MatchString(line): |
53 | | - apiIntermediate.ApiTitle = rxApiTitle.FindStringSubmatch(line)[1] |
54 | | - case rxApiVersion.MatchString(line): |
55 | | - apiIntermediate.ApiVersion = rxApiVersion.FindStringSubmatch(line)[1] |
56 | | - case rxBasePath.MatchString(line): |
57 | | - apiIntermediate.BasePath = rxBasePath.FindStringSubmatch(line)[1] |
| 38 | + title := strings.TrimSpace(section.Title) |
| 39 | + title = strings.ToLower(title) |
58 | 40 |
|
59 | | - case rxSubApi.MatchString(line): |
60 | | - matches := rxSubApi.FindStringSubmatch(line) |
61 | | - subApi := SubApiIntermediate{ |
62 | | - Name: matches[1], |
63 | | - Path: matches[2], |
| 41 | + switch title { |
| 42 | + case "openapi api title": |
| 43 | + if l, ok := section.Line(0); ok { |
| 44 | + apiIntermediate.ApiTitle = l |
| 45 | + } |
| 46 | + case "openapi api description": |
| 47 | + apiIntermediate.ApiDescription = section.Body |
| 48 | + case "openapi api version": |
| 49 | + if l, ok := section.Line(0); ok { |
| 50 | + apiIntermediate.ApiVersion = l |
| 51 | + } |
| 52 | + case "openapi base path": |
| 53 | + if l, ok := section.Line(0); ok { |
| 54 | + apiIntermediate.BasePath = l |
64 | 55 | } |
65 | | - apiIntermediate.SubApis = append(apiIntermediate.SubApis, subApi) |
66 | 56 | } |
67 | 57 | } |
68 | 58 | } |
69 | 59 |
|
70 | 60 | return apiIntermediate |
71 | 61 | } |
| 62 | + |
| 63 | +func getFirstWord(s string) string { |
| 64 | + words := strings.Split(s, " ") |
| 65 | + for _, word := range words { |
| 66 | + word = strings.Trim(word, "\t") |
| 67 | + if word != "" { |
| 68 | + return word |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return "" |
| 73 | +} |
0 commit comments