Skip to content

Commit 4bd4035

Browse files
committed
feat: Add initial parse functionality. Add stub test object.
1 parent d100dbe commit 4bd4035

File tree

14 files changed

+1430
-78
lines changed

14 files changed

+1430
-78
lines changed

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/rog-golang-buddies/api-hub_data-scraping-service
33
go 1.18
44

55
require (
6+
github.com/getkin/kin-openapi v0.98.0
67
github.com/golang/mock v1.6.0
78
github.com/kelseyhightower/envconfig v1.4.0
89
github.com/rabbitmq/amqp091-go v1.4.0
@@ -13,8 +14,13 @@ require (
1314

1415
require (
1516
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
18+
github.com/go-openapi/swag v0.19.5 // indirect
19+
github.com/invopop/yaml v0.1.0 // indirect
20+
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e // indirect
1621
github.com/pmezard/go-difflib v1.0.0 // indirect
1722
go.uber.org/atomic v1.7.0 // indirect
1823
go.uber.org/multierr v1.6.0 // indirect
24+
gopkg.in/yaml.v2 v2.4.0 // indirect
1925
gopkg.in/yaml.v3 v3.0.1 // indirect
2026
)

go.sum

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
33
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/getkin/kin-openapi v0.98.0 h1:lIACvCG9cxmFsEywz+LCoVhcZHFLUy+Nv5QSkb43eAE=
7+
github.com/getkin/kin-openapi v0.98.0/go.mod h1:w4lRPHiyOdwGbOkLIyk+P0qCwlu7TXPCHD/64nSXzgE=
8+
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
9+
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
10+
github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY=
11+
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
612
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
713
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
14+
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
15+
github.com/invopop/yaml v0.1.0 h1:YW3WGUoJEXYfzWBjn00zIlrw7brGVD0fUKRYDPAPhrc=
16+
github.com/invopop/yaml v0.1.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
817
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
918
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
1019
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1120
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1221
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1322
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1423
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
24+
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
25+
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8=
26+
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
1527
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
1628
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1729
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -21,6 +33,7 @@ github.com/rabbitmq/amqp091-go v1.4.0/go.mod h1:JsV0ofX5f1nwOGafb8L5rBItt9GyhfQf
2133
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2234
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
2335
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
36+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
2437
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2538
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2639
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
@@ -66,9 +79,12 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
6679
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6780
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
6881
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
69-
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
82+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7083
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
84+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
85+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
7186
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7287
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
88+
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7389
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7490
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/app.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/load"
88
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/logger"
99
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/parse"
10+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/parse/openapi"
1011
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/process"
1112
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue"
1213
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue/handler"
@@ -67,7 +68,7 @@ func Start() int {
6768

6869
func createDefaultProcessor() (process.UrlProcessor, error) {
6970
recognizer := recognize.NewRecognizer()
70-
parsers := []parse.Parser{parse.NewJsonOpenApiParser(), parse.NewYamlOpenApiParser()}
71+
parsers := []parse.Parser{openapi.NewJsonOpenApiParser(), openapi.NewYamlOpenApiParser()}
7172
converter := parse.NewConverter(parsers)
7273
loader := load.NewContentLoader()
7374

internal/dto/apiSpecDoc/apiSpecDoc.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package apiSpecDoc
22

3-
//ApiSpecDoc represents full API Specification document
4-
//with all required data to view and execute requests
5-
//Propose to not store something like components at the initial stage - leave it as update to simplify logic
6-
//So each ApiMethod need to keep all schema (some duplication, but we could update it faster with extending of the model)
3+
// ApiSpecDoc represents full API Specification document
4+
// with all required data to view and execute requests
5+
// Propose to not store something like components at the initial stage - leave it as update to simplify logic
6+
// So each ApiMethod need to keep all schema (some duplication, but we could update it faster with extending of the model)
77
type ApiSpecDoc struct {
88
//Type of API definition
99
//It's possible to be OpenApi, gRPC and others
@@ -13,15 +13,15 @@ type ApiSpecDoc struct {
1313
//It relates to:
1414
//Tags - Open API specs
1515
//Services - RPCs
16-
Groups []Group
16+
Groups []*Group
1717

1818
//Methods at the root level without groups
19-
Methods []ApiMethod
19+
Methods []*ApiMethod
2020
}
2121

22-
//Group represents some grouping rule
23-
//Tags for the open API
24-
//Services for the gRPC
22+
// Group represents some grouping rule
23+
// Tags for the open API
24+
// Services for the gRPC
2525
type Group struct {
2626
//Name of the group
2727
Name string
@@ -30,10 +30,10 @@ type Group struct {
3030
Description string
3131

3232
//Methods is a set of request methods related to the group
33-
Methods []ApiMethod
33+
Methods []*ApiMethod
3434
}
3535

36-
//ApiMethod represents particular API method to call
36+
// ApiMethod represents particular API method to call
3737
type ApiMethod struct {
3838
//Path to send request
3939
//For example /open/api/info/
@@ -52,7 +52,7 @@ type ApiMethod struct {
5252
Type MethodType
5353

5454
//RequestBody is a description of ApiMethod request body, nil if no request body
55-
RequestBody *MediaTypeObject
55+
RequestBody *RequestBody
5656

5757
//Servers represent available paths for requests with description.
5858
//In the case of swagger we need to calculate it taking the deepest nested servers definition
@@ -135,29 +135,29 @@ type Schema struct {
135135

136136
//Nested describe nested object/properties
137137
//If it's object and contain nested fields
138-
Fields []Schema
138+
Fields []*Schema
139139
}
140140

141-
//MediaTypeObject represents schema for the different media types
142-
//i.e. "application/json" and etc.
141+
// MediaTypeObject represents schema for the different media types
142+
// i.e. "application/json" and etc.
143143
type MediaTypeObject struct {
144-
Schema Schema
144+
Schema *Schema
145145
}
146146

147-
//RequestBody is a representation of request body
147+
// RequestBody is a representation of request body
148148
type RequestBody struct {
149149
Description string
150150

151151
//Content represents request object for the different media types
152-
Content map[string]MediaTypeObject
152+
Content map[string]*MediaTypeObject
153153

154154
//Required define is request body required
155155
Required bool
156156
}
157157

158-
//Server represents server description
159-
//To use in the address line of view
160-
//https://swagger.io/specification/#server-object
158+
// Server represents server description
159+
// To use in the address line of view
160+
// https://swagger.io/specification/#server-object
161161
type Server struct {
162162
//Url to access server (no templates, real url)
163163
Url string
@@ -166,8 +166,8 @@ type Server struct {
166166
Description string
167167
}
168168

169-
//Parameter is abstraction of additional data to request
170-
//It is headers for the REST API and metadata for the gRPC
169+
// Parameter is abstraction of additional data to request
170+
// It is headers for the REST API and metadata for the gRPC
171171
type Parameter struct {
172172
Name string
173173

@@ -183,9 +183,9 @@ type Parameter struct {
183183
Required bool
184184
}
185185

186-
//ExternalDoc may be available for the Open API
187-
//And contain link to description of request method
188-
//Maybe useful also for debugging - to find failed API description rapidly
186+
// ExternalDoc may be available for the Open API
187+
// And contain link to description of request method
188+
// Maybe useful also for debugging - to find failed API description rapidly
189189
type ExternalDoc struct {
190190
Description string
191191

internal/dto/apiSpecDoc/types.go

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,58 @@
11
package apiSpecDoc
22

3-
//Type represents type of parsed API
3+
// Type represents type of parsed API
44
type Type int
55

66
const (
77
TypeOpenApi Type = iota
88
)
99

10-
//SchemaType is the type of field.
10+
// SchemaType is the type of field.
1111
type SchemaType string
1212

1313
const (
14-
Integer SchemaType = "INTEGER"
15-
Number SchemaType = "NUMBER"
16-
String SchemaType = "STRING"
17-
Date SchemaType = "DATE"
18-
Array SchemaType = "ARRAY" //Array type may contain single nested field with type to define full array type
19-
Map SchemaType = "MAP" //Map i.e. { "map_name": {"key1":"value1", "key2":"value2"}}
20-
OneOf SchemaType = "ONE_OF" //OneOf is one of the different types (C union) (from nested fields)
21-
Object SchemaType = "OBJECT" //Object represent object and contains set of fields inside
14+
Unknown SchemaType = "UNKNOWN"
15+
NotDefined SchemaType = "NOT_DEFINED"
16+
Integer SchemaType = "INTEGER"
17+
Boolean SchemaType = "BOOLEAN"
18+
Number SchemaType = "NUMBER"
19+
String SchemaType = "STRING"
20+
Date SchemaType = "DATE"
21+
Array SchemaType = "ARRAY" //Array type may contain single nested field with type to define full array type
22+
Map SchemaType = "MAP" //Map i.e. { "map_name": {"key1":"value1", "key2":"value2"}}
23+
OneOf SchemaType = "ONE_OF" //OneOf is one of the different types (C union) (from nested fields)
24+
AnyOf SchemaType = "ANY_OF" //AnyOf defines that result object can contain any set of sub-schemes
25+
AllOf SchemaType = "ALL_OF" //AllOf defines that result object combines all listed objects/properties.
26+
Not SchemaType = "NOT" //Not represents type that can't be used. So it's possible to use any of types except "not"
27+
Object SchemaType = "OBJECT" //Object represent object and contains set of fields inside
2228
)
2329

24-
//MethodType define type of action of the method
30+
func ResolveSchemaType(strType string) SchemaType {
31+
switch strType {
32+
case "string":
33+
return String
34+
case "number":
35+
return Number
36+
case "integer":
37+
return Integer
38+
case "boolean":
39+
return Boolean
40+
case "array":
41+
return Array
42+
case "object":
43+
return Object
44+
case "":
45+
return NotDefined
46+
default:
47+
return Unknown
48+
}
49+
}
50+
51+
// MethodType define type of action of the method
2552
type MethodType string
2653

2754
const (
55+
MethodConnect MethodType = "CONNECT"
2856
MethodGet MethodType = "GET"
2957
MethodPut MethodType = "PUT"
3058
MethodPost MethodType = "POST"
@@ -36,7 +64,7 @@ const (
3664
//Unary/bidirectional stream etc. for the rpc
3765
)
3866

39-
//ParameterType represents to what part of request parameter relates
67+
// ParameterType represents to what part of request parameter relates
4068
type ParameterType string
4169

4270
const (

internal/parse/converter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (c *ConverterImpl) Convert(file *fileresource.FileResource) (*apiSpecDoc.Ap
2626
if !ok {
2727
return nil, errors.New("file type not supported")
2828
}
29-
apiSpec, err := parser.parse(file.Content)
29+
apiSpec, err := parser.Parse(file.Content)
3030
if err != nil {
3131
return nil, err
3232
}
@@ -37,7 +37,7 @@ func (c *ConverterImpl) Convert(file *fileresource.FileResource) (*apiSpecDoc.Ap
3737
func NewConverter(parsers []Parser) Converter {
3838
parsersMap := make(map[fileresource.AsdFileType]Parser)
3939
for _, parser := range parsers {
40-
parsersMap[parser.getType()] = parser
40+
parsersMap[parser.GetType()] = parser
4141
}
4242
return &ConverterImpl{
4343
parsers: parsersMap,

internal/parse/converter_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package parse
22

33
import (
4+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/parse/openapi"
45
"github.com/stretchr/testify/assert"
56
"testing"
67
)
78

89
func TestNewConverter(t *testing.T) {
9-
parsers := []Parser{NewYamlOpenApiParser(), NewJsonOpenApiParser()}
10+
parsers := []Parser{openapi.NewYamlOpenApiParser(), openapi.NewJsonOpenApiParser()}
1011
converter := NewConverter(parsers)
1112
assert.NotNil(t, converter)
1213
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
package parse
1+
package openapi
22

33
import (
44
"errors"
55
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/apiSpecDoc"
66
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/dto/fileresource"
7+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/parse"
78
)
89

9-
//JsonOpenApiParser implementation for parsing json open API files
10+
// JsonOpenApiParser implementation for parsing json open API files
1011
type JsonOpenApiParser struct {
1112
}
1213

13-
func (joap *JsonOpenApiParser) parse(content []byte) (*apiSpecDoc.ApiSpecDoc, error) {
14+
func (joap *JsonOpenApiParser) Parse(content []byte) (*apiSpecDoc.ApiSpecDoc, error) {
1415
return nil, errors.New("not implemented")
1516
}
1617

17-
func (joap *JsonOpenApiParser) getType() fileresource.AsdFileType {
18+
func (joap *JsonOpenApiParser) GetType() fileresource.AsdFileType {
1819
return fileresource.JsonOpenAPI
1920
}
2021

21-
func NewJsonOpenApiParser() Parser {
22+
func NewJsonOpenApiParser() parse.Parser {
2223
return &JsonOpenApiParser{}
2324
}

0 commit comments

Comments
 (0)