Skip to content

Commit

Permalink
Add the rest of the primitive types (#17)
Browse files Browse the repository at this point in the history
This adds the rest of the primitive types in the schema (+ OpenAPI / proto writing)
  • Loading branch information
rambleraptor authored Sep 8, 2024
1 parent 80bb3d9 commit 6b85db5
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 131 deletions.
248 changes: 140 additions & 108 deletions example/bookstore/v1/bookstore.pb.go

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions example/bookstore/v1/bookstore.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,23 @@ service Bookstore {

// A Book resource.
message Book {
// Field for id.
string id = 10001;

// Field for edition.
int32 edition = 4;

// Field for isbn.
string isbn = 1;

// Field for price.
float price = 2;

// Field for published.
bool published = 3;

// Field for path.
string path = 10000;

// Field for id.
string id = 10001;
}

// A Create request for a Book resource.
Expand Down
42 changes: 35 additions & 7 deletions example/bookstore/v1/bookstore.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,27 @@
"schema": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "Field for id."
},
"edition": {
"type": "integer",
"format": "int32",
"description": "Field for edition."
},
"isbn": {
"type": "string",
"description": "Field for isbn."
},
"id": {
"type": "string",
"description": "Field for id."
"price": {
"type": "number",
"format": "float",
"description": "Field for price."
},
"published": {
"type": "boolean",
"description": "Field for published."
}
},
"title": "The resource to perform the operation on."
Expand Down Expand Up @@ -315,17 +329,31 @@
"v1Book": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "Field for id."
},
"edition": {
"type": "integer",
"format": "int32",
"description": "Field for edition."
},
"isbn": {
"type": "string",
"description": "Field for isbn."
},
"price": {
"type": "number",
"format": "float",
"description": "Field for price."
},
"published": {
"type": "boolean",
"description": "Field for published."
},
"path": {
"type": "string",
"description": "Field for path."
},
"id": {
"type": "string",
"description": "Field for id."
}
},
"description": "A Book resource."
Expand Down
12 changes: 12 additions & 0 deletions example/bookstore/v1/bookstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ resources:
type: STRING
number: 1
required: true
price:
type: FLOAT
number: 2
required: true
published:
type: BOOLEAN
number: 3
required: true
edition:
type: INT32
number: 4
required: false
# parents:
# - "bookstore.example.com/Publisher"
methods:
Expand Down
15 changes: 14 additions & 1 deletion example/bookstore/v1/bookstore_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,15 @@
"Book": {
"type": "object",
"required": [
"isbn"
"isbn",
"price",
"published"
],
"properties": {
"edition": {
"type": "integer",
"format": "int32"
},
"id": {
"type": "string",
"readOnly": true,
Expand All @@ -124,6 +130,13 @@
"path": {
"type": "string",
"readOnly": true
},
"price": {
"type": "number",
"format": "float"
},
"published": {
"type": "boolean"
}
}
}
Expand Down
24 changes: 21 additions & 3 deletions schema/resourcedefinition.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions schema/resourcedefinition.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ message Methods {
enum Type {
UNSPECIFIED = 0;
STRING = 1;
INT32 = 2;
INT64 = 3;
DOUBLE = 4;
FLOAT = 5;
BOOLEAN = 6;
}

message Property {
Expand Down
30 changes: 21 additions & 9 deletions writer/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,27 @@ func init() {
}

func WriteServiceToOpenAPI(ps *parser.ParsedService) ([]byte, error) {
openAPI := convertToOpenAPI(ps)
openAPI, err := convertToOpenAPI(ps)
if(err != nil) {
return nil, err
}

jsonData, err := json.MarshalIndent(openAPI, "", " ")
if err != nil {
return nil, err
}
return jsonData, nil
}

func convertToOpenAPI(service *parser.ParsedService) *OpenAPI {
func convertToOpenAPI(service *parser.ParsedService) (*OpenAPI, error) {
paths := Paths{}
definitions := Definitions{}
for _, r := range service.ResourceByType {
definitions[r.Kind] = resourceToSchema(r)
d, err := resourceToSchema(r)
if(err != nil) {
return nil, err
}
definitions[r.Kind] = d;
schemaRef := fmt.Sprintf("#/definitions/%v", r.Kind)
if r.Methods.List != nil {
log.Printf("resource plural: %s", r.Plural)
Expand Down Expand Up @@ -152,17 +160,20 @@ func convertToOpenAPI(service *parser.ParsedService) *OpenAPI {
Paths: paths,
Definitions: definitions,
}
return openAPI
return openAPI, nil
}

func resourceToSchema(r *parser.ParsedResource) Schema {
func resourceToSchema(r *parser.ParsedResource) (Schema, error) {
properties := Properties{}
required := []string{}
for name, p := range r.Properties {
// TODO(YFT): add more handling of types here
t := "string"
t, err := openAPIType(p)
if(err != nil ) {
return Schema{}, err
}
properties[name] = Schema{
Type: t,
Type: t.openapi_type,
Format: t.openapi_format,
XTerraformID: name == constants.FIELD_ID_NAME,
ReadOnly: p.ReadOnly,
}
Expand All @@ -174,7 +185,7 @@ func resourceToSchema(r *parser.ParsedResource) Schema {
Type: "object",
Properties: &properties,
Required: required,
}
}, nil
}

func addMethodToPath(paths Paths, path, method string, methodInfo MethodInfo) {
Expand Down Expand Up @@ -229,6 +240,7 @@ type ParameterInfo struct {
type Schema struct {
Ref string `json:"$ref,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Required []string `json:"required,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
Items *Schema `json:"items,omitempty"`
Expand Down
43 changes: 43 additions & 0 deletions writer/openapi/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package openapi

import (
"fmt"

"github.com/aep-dev/aepc/schema"
)

type TypeInfo struct {
openapi_type string
openapi_format string
}

func openAPIType(p *schema.Property) (TypeInfo, error) {
t := "";
f := "";

switch(p.Type) {
case schema.Type_STRING:
t = "string"
case schema.Type_DOUBLE:
t = "number"
f = "double"
case schema.Type_FLOAT:
t = "number"
f = "float"
case schema.Type_INT32:
t = "integer"
f = "int32"
case schema.Type_INT64:
t = "integer"
f = "int64"
case schema.Type_BOOLEAN:
t = "boolean"
default:
return TypeInfo{}, fmt.Errorf("%s does not have openapi type support", p.Type)
}

return TypeInfo{
openapi_type: t,
openapi_format: f,
}, nil
}
12 changes: 12 additions & 0 deletions writer/proto/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ func GeneratedResourceMessage(r *parser.ParsedResource) (*builder.MessageBuilder
switch p.Type {
case schema.Type_STRING:
typ = builder.FieldTypeString()
case schema.Type_INT32:
typ = builder.FieldTypeInt32()
case schema.Type_INT64:
typ = builder.FieldTypeInt64()
case schema.Type_BOOLEAN:
typ = builder.FieldTypeBool()
case schema.Type_DOUBLE:
typ = builder.FieldTypeDouble()
case schema.Type_FLOAT:
typ = builder.FieldTypeFloat()
default:
return nil, fmt.Errorf("proto mapping for type %s not found", p.Type)
}
mb.AddField(builder.NewField(p.Name, typ).SetNumber(p.Number).SetComments(
builder.Comments{
Expand Down

0 comments on commit 6b85db5

Please sign in to comment.