Skip to content

Commit

Permalink
Summarize vocabulary (google#183)
Browse files Browse the repository at this point in the history
* Started v3

* Added schemas and responses to schemas map

* Cleaned up the the v2 and v3 processing files

* Created plugin for the vocabulary app

* Finished vocabulary plug in

* Removed the app vocabulary because it has moved to plugins

* Created tests for vocabulary plugin

* Included changes suggested on PR

* Included additional suggestions made in the PR

* Added space to sampe file

* Deleted sample files, renamed parameter and changed names map to parameter map

* Filled parameter typo

* Created initial version of summarize-vocabulary

* Completed summarize-vocabulary

* Fixed header

* Added suggestions for renaming and providing an error message if no arguments are provided

* Fixed semantic error in grouping

* Fixed semantic errors

* Updated tests for new output
  • Loading branch information
gizzon committed Jun 6, 2020
1 parent d873db5 commit 899eab8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 61 deletions.
45 changes: 19 additions & 26 deletions plugins/gnostic-vocabulary/v2Processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
openapi_v2 "github.com/googleapis/gnostic/openapiv2"
)

func processOperationV2(operation *openapi_v2.Operation, operationId, parameters map[string]int) {
func processOperationV2(operation *openapi_v2.Operation, operationID, parameters map[string]int) {
if operation.OperationId != "" {
operationId[operation.OperationId] += 1
operationID[operation.OperationId]++
}
for _, item := range operation.Parameters {
switch t := item.Oneof.(type) {
case *openapi_v2.ParametersItem_Parameter:
switch t2 := t.Parameter.Oneof.(type) {
case *openapi_v2.Parameter_BodyParameter:
parameters[t2.BodyParameter.Name] += 1
parameters[t2.BodyParameter.Name]++
case *openapi_v2.Parameter_NonBodyParameter:
nonBodyParam := t2.NonBodyParameter
processOperationParametersV2(operation, parameters, nonBodyParam)
Expand All @@ -26,13 +26,13 @@ func processOperationV2(operation *openapi_v2.Operation, operationId, parameters
func processOperationParametersV2(operation *openapi_v2.Operation, parameters map[string]int, nonBodyParam *openapi_v2.NonBodyParameter) {
switch t3 := nonBodyParam.Oneof.(type) {
case *openapi_v2.NonBodyParameter_FormDataParameterSubSchema:
parameters[t3.FormDataParameterSubSchema.Name] += 1
parameters[t3.FormDataParameterSubSchema.Name]++
case *openapi_v2.NonBodyParameter_HeaderParameterSubSchema:
parameters[t3.HeaderParameterSubSchema.Name] += 1
parameters[t3.HeaderParameterSubSchema.Name]++
case *openapi_v2.NonBodyParameter_PathParameterSubSchema:
parameters[t3.PathParameterSubSchema.Name] += 1
parameters[t3.PathParameterSubSchema.Name]++
case *openapi_v2.NonBodyParameter_QueryParameterSubSchema:
parameters[t3.QueryParameterSubSchema.Name] += 1
parameters[t3.QueryParameterSubSchema.Name]++
}
}

Expand All @@ -41,51 +41,44 @@ func processSchemaV2(schema *openapi_v2.Schema, properties map[string]int) {
return
}
for _, pair := range schema.Properties.AdditionalProperties {
properties[pair.Name] += 1
properties[pair.Name]++
}
}

func processDocumentV2(document *openapi_v2.Document) *metrics.Vocabulary {
var schemas map[string]int
schemas = make(map[string]int)

var operationId map[string]int
operationId = make(map[string]int)

var parameters map[string]int
parameters = make(map[string]int)

var properties map[string]int
properties = make(map[string]int)
schemas := make(map[string]int)
operationID := make(map[string]int)
parameters := make(map[string]int)
properties := make(map[string]int)

if document.Definitions != nil {
for _, pair := range document.Definitions.AdditionalProperties {
schemas[pair.Name] += 1
schemas[pair.Name]++
processSchemaV2(pair.Value, properties)
}
}
for _, pair := range document.Paths.Path {
v := pair.Value
if v.Get != nil {
processOperationV2(v.Get, operationId, parameters)
processOperationV2(v.Get, operationID, parameters)
}
if v.Post != nil {
processOperationV2(v.Post, operationId, parameters)
processOperationV2(v.Post, operationID, parameters)
}
if v.Put != nil {
processOperationV2(v.Put, operationId, parameters)
processOperationV2(v.Put, operationID, parameters)
}
if v.Patch != nil {
processOperationV2(v.Patch, operationId, parameters)
processOperationV2(v.Patch, operationID, parameters)
}
if v.Delete != nil {
processOperationV2(v.Delete, operationId, parameters)
processOperationV2(v.Delete, operationID, parameters)
}
}

vocab := &metrics.Vocabulary{
Schemas: fillProtoStructures(schemas),
Operations: fillProtoStructures(operationId),
Operations: fillProtoStructures(operationID),
Parameters: fillProtoStructures(parameters),
Properties: fillProtoStructures(properties),
}
Expand Down
77 changes: 43 additions & 34 deletions plugins/gnostic-vocabulary/v3Processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
)

func fillProtoStructures(m map[string]int) []*metrics.WordCount {
key_names := make([]string, 0, len(m))
keyNames := make([]string, 0, len(m))
for key := range m {
key_names = append(key_names, key)
keyNames = append(keyNames, key)
}
sort.Strings(key_names)
sort.Strings(keyNames)

counts := make([]*metrics.WordCount, 0)
for _, k := range key_names {
for _, k := range keyNames {
temp := &metrics.WordCount{
Word: k,
Count: int32(m[k]),
Expand All @@ -25,43 +25,59 @@ func fillProtoStructures(m map[string]int) []*metrics.WordCount {
return counts
}

func processOperationV3(operation *openapi_v3.Operation, operationId, parameters map[string]int) {
func processOperationV3(operation *openapi_v3.Operation, operationID, parameters map[string]int) {
if operation.OperationId != "" {
operationId[operation.OperationId] += 1
operationID[operation.OperationId]++
}
for _, item := range operation.Parameters {
switch t := item.Oneof.(type) {
case *openapi_v3.ParameterOrReference_Parameter:
parameters[t.Parameter.Name] += 1
parameters[t.Parameter.Name]++
}
}
}

func processComponentsV3(components *openapi_v3.Components, schemas, properties map[string]int) {
processParametersV3(components, schemas, properties)
processSchemasV3(components, schemas)
func processComponentsV3(components *openapi_v3.Components, schemas, parameters, properties map[string]int) {
processParametersV3(components, schemas, parameters)
processSchemasV3(components, schemas, properties)
processResponsesV3(components, schemas)
}

func processParametersV3(components *openapi_v3.Components, schemas, properties map[string]int) {
func processParametersV3(components *openapi_v3.Components, schemas, parameters map[string]int) {
if components.Parameters == nil {
return
}
for _, pair := range components.Parameters.AdditionalProperties {
schemas[pair.Name] += 1
switch t := pair.Value.Oneof.(type) {
case *openapi_v3.ParameterOrReference_Parameter:
properties[t.Parameter.Name] += 1
parameters[t.Parameter.Name]++
}
}
}

func processSchemasV3(components *openapi_v3.Components, schemas map[string]int) {
func processSchemasV3(components *openapi_v3.Components, schemas, properties map[string]int) {
if components.Schemas == nil {
return
}
for _, pair := range components.Schemas.AdditionalProperties {
schemas[pair.Name] += 1
schemas[pair.Name]++
processSchemaV3(pair.Value, properties)
}
}

func processSchemaV3(schema *openapi_v3.SchemaOrReference, properties map[string]int) {
if schema == nil {
return
}
switch t := schema.Oneof.(type) {
case *openapi_v3.SchemaOrReference_Reference:
return
case *openapi_v3.SchemaOrReference_Schema:
if t.Schema.Properties != nil {
for _, pair := range t.Schema.Properties.AdditionalProperties {
properties[pair.Name]++
}
}
}
}

Expand All @@ -70,50 +86,43 @@ func processResponsesV3(components *openapi_v3.Components, schemas map[string]in
return
}
for _, pair := range components.Responses.AdditionalProperties {
schemas[pair.Name] += 1
schemas[pair.Name]++
}
}

func processDocumentV3(document *openapi_v3.Document) *metrics.Vocabulary {
var schemas map[string]int
schemas = make(map[string]int)

var operationId map[string]int
operationId = make(map[string]int)

var parameters map[string]int
parameters = make(map[string]int)

var properties map[string]int
properties = make(map[string]int)
schemas := make(map[string]int)
operationID := make(map[string]int)
parameters := make(map[string]int)
properties := make(map[string]int)

if document.Components != nil {
processComponentsV3(document.Components, schemas, properties)
processComponentsV3(document.Components, schemas, parameters, properties)

}
for _, pair := range document.Paths.Path {
v := pair.Value
if v.Get != nil {
processOperationV3(v.Get, operationId, parameters)
processOperationV3(v.Get, operationID, parameters)
}
if v.Post != nil {
processOperationV3(v.Post, operationId, parameters)
processOperationV3(v.Post, operationID, parameters)
}
if v.Put != nil {
processOperationV3(v.Put, operationId, parameters)
processOperationV3(v.Put, operationID, parameters)
}
if v.Patch != nil {
processOperationV3(v.Patch, operationId, parameters)
processOperationV3(v.Patch, operationID, parameters)
}
if v.Delete != nil {
processOperationV3(v.Delete, operationId, parameters)
processOperationV3(v.Delete, operationID, parameters)
}
}

vocab := &metrics.Vocabulary{
Properties: fillProtoStructures(properties),
Schemas: fillProtoStructures(schemas),
Operations: fillProtoStructures(operationId),
Operations: fillProtoStructures(operationID),
Parameters: fillProtoStructures(parameters),
}
return vocab
Expand Down
29 changes: 28 additions & 1 deletion testdata/v3.0/yaml/vocabulary-petstore.out
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@
"count": 1
}
],
"properties": [
{
"word": "code",
"count": 1
},
{
"word": "id",
"count": 1
},
{
"word": "message",
"count": 1
},
{
"word": "name",
"count": 1
},
{
"word": "tag",
"count": 1
}
],
"operations": [
{
"word": "createPets",
Expand Down Expand Up @@ -50,7 +72,12 @@

Pet

Pets
Pets
code
id
message
name
tag

createPets
listPets
Expand Down

0 comments on commit 899eab8

Please sign in to comment.