Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.

Commit 644ef3d

Browse files
committed
refactor to new comment format (hopefully) complete. various optimizations.
1 parent 9d9be0b commit 644ef3d

13 files changed

+378
-235
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ OpenAPI Tags:
420420

421421
# Code Structure
422422

423-
This tool operates, at least conceptually, in three phases: parsing, extraction,
424-
and generation.
423+
This tool operates, at least conceptually, in three phases: detection,
424+
extraction, and generation.
425425

426426
During the parsing phase, the code project is scanned for comments blocks.
427427

intermediate_api.go

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,73 @@
11
package main
22

33
import (
4-
"regexp"
5-
"bytes"
6-
"bufio"
4+
"strings"
75
)
86

9-
107
type ApiIntermediate struct {
118
ApiVersion string
129
ApiTitle string
1310
ApiDescription string
1411
BasePath string
15-
SubApis []SubApiIntermediate
1612
}
1713

18-
1914
func intermediatateApi(commentBlocks []string) ApiIntermediate {
2015

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+
*/
3627

37-
var apiIntermediate ApiIntermediate = ApiIntermediate{
38-
SubApis: make([]SubApiIntermediate, 0),
39-
}
28+
var apiIntermediate ApiIntermediate = ApiIntermediate{}
4029

4130
for _, commentBlock := range commentBlocks {
4231

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 {
4735

48-
switch {
36+
//log.Print(section)
4937

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)
5840

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
6455
}
65-
apiIntermediate.SubApis = append(apiIntermediate.SubApis, subApi)
6656
}
6757
}
6858
}
6959

7060
return apiIntermediate
7161
}
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+
}

intermediate_definition.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"github.com/go-openapi/spec"
55
"github.com/jackmanlabs/errors"
6-
"strconv"
76
"strings"
87
)
98

@@ -13,10 +12,10 @@ type DefinitionIntermediate struct {
1312
EmbeddedTypes []string
1413
Members map[string]SchemerDefiner // map[name]schemer
1514
Name string
16-
PackageName string // The actual package name of this type.
17-
PackagePath string // The actual package path of this type.
18-
UnderlyingType string // This isn't used right now. In our test codebase, non-struct types were never used.
19-
Enums []string // If the underlying type is a primitive type, it's assumed it's an enum type, these being the values.
15+
PackageName string // The actual package name of this type.
16+
PackagePath string // The actual package path of this type.
17+
UnderlyingType string // This isn't used right now. In our test codebase, non-struct types were never used.
18+
Enums []interface{} // If the underlying type is a primitive type, it's assumed it's an enum type, these being the values.
2019

2120
// While it may not strictly be equivalent from a language specification
2221
// perspective, we're going to call a non-struct type with an underlying
@@ -55,21 +54,7 @@ func (this *DefinitionIntermediate) Schema() spec.Schema {
5554

5655
if isPrimitive, t, f := IsPrimitive(this.UnderlyingType); isPrimitive {
5756
schema.Typed(t, f)
58-
schema.Enum = make([]interface{}, 0)
59-
60-
for _, enum := range this.Enums {
61-
if strings.HasPrefix(enum, "\"") {
62-
schema.Enum = append(schema.Enum, strings.Trim(enum, "\""))
63-
} else {
64-
// store numerical enums as numbers, otherwise strings.
65-
if f, err := strconv.ParseFloat(enum, 64); err == nil {
66-
schema.Enum = append(schema.Enum, f)
67-
} else {
68-
enum = strings.Trim(enum, "\"")
69-
schema.Enum = append(schema.Enum, enum)
70-
}
71-
}
72-
}
57+
schema.Enum = this.Enums
7358
} else {
7459
schema.Typed("object", "")
7560
schema.Required = make([]string, 0)
@@ -126,8 +111,6 @@ func (this *DefinitionIntermediate) DefineDefinitions() error {
126111
return nil
127112
}
128113

129-
130-
131114
func mergeDefinitions(dst, src *DefinitionIntermediate) {
132115
for srcName, srcMember := range src.Members {
133116
_, exists := dst.Members[srcName]

intermediate_member.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ func (this *MemberIntermediate) CanonicalName() string {
7171
}
7272

7373
func (this *MemberIntermediate) Schema() *spec.Schema {
74+
75+
// This addresses a response not having a type (no body).
76+
if this.Type == "nil" {
77+
return nil
78+
}
79+
7480
schema := new(spec.Schema)
7581

7682
name := this.Name
@@ -81,6 +87,9 @@ func (this *MemberIntermediate) Schema() *spec.Schema {
8187
schema.Description = this.Description
8288

8389
if isPrimitive, t, f := IsPrimitive(this.Type); isPrimitive {
90+
91+
92+
8493
schema.Typed(t, f)
8594

8695
if t == "string" {
@@ -160,7 +169,7 @@ func (this *MemberIntermediate) DefineDefinitions(referringPackage string) error
160169

161170
goType := this.Type
162171

163-
if goType == "nil"{
172+
if goType == "nil" {
164173
return nil
165174
}
166175

0 commit comments

Comments
 (0)