Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required and Repeated fields #23

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
369 changes: 156 additions & 213 deletions example/bookstore/v1/bookstore.pb.go

Large diffs are not rendered by default.

21 changes: 9 additions & 12 deletions example/bookstore/v1/bookstore.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ service Bookstore {
// A Book resource.
message Book {
// Field for isbn.
string isbn = 1;
repeated string isbn = 1 [(google.api.field_behavior) = REQUIRED];

// Field for details.
Details details = 2;
// Field for price.
float price = 2 [(google.api.field_behavior) = REQUIRED];

// Field for published.
bool published = 3 [(google.api.field_behavior) = REQUIRED];

// Field for edition.
int32 edition = 4;

// Field for path.
string path = 10000;
Expand Down Expand Up @@ -157,12 +163,3 @@ message ApplyBookRequest {
// The resource to perform the operation on.
Book book = 10015 [(google.api.field_behavior) = REQUIRED];
}

// A Details resource.
message Details {
// Field for edition.
string edition = 1;

// Field for supported_ereaders.
string supported_ereaders = 2;
}
42 changes: 23 additions & 19 deletions example/bookstore/v1/bookstore.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,25 @@
"type": "object",
"properties": {
"isbn": {
"type": "string",
"type": "array",
"items": {
"type": "string"
},
"description": "Field for isbn."
},
"details": {
"$ref": "#/definitions/v1Details",
"description": "Field for details."
"price": {
"type": "number",
"format": "float",
"description": "Field for price."
},
"published": {
"type": "boolean",
"description": "Field for published."
},
"edition": {
"type": "integer",
"format": "int32",
"description": "Field for edition."
},
"path": {
"type": "string",
Expand All @@ -315,21 +328,12 @@
"description": "Field for id."
}
},
"description": "A Book resource."
},
"v1Details": {
"type": "object",
"properties": {
"edition": {
"type": "string",
"description": "Field for edition."
},
"supportedEreaders": {
"type": "string",
"description": "Field for supported_ereaders."
}
},
"description": "A Details resource."
"description": "A Book resource.",
"required": [
"isbn",
"price",
"published"
]
},
"v1ListBookResponse": {
"type": "object",
Expand Down
25 changes: 12 additions & 13 deletions example/bookstore/v1/bookstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ resources:
plural: "books"
properties:
isbn:
type: STRING
type: ARRAY
array_primitive_type: STRING
number: 1
required: true
details:
type: OBJECT
price:
type: FLOAT
number: 2
required: true
object_type: Details
published:
type: BOOLEAN
number: 3
required: true
edition:
type: INT32
number: 4
required: false
# parents:
# - "bookstore.example.com/Publisher"
methods:
Expand All @@ -37,12 +45,3 @@ resources:
# - "Publisher"
# methods:
# read: {}
objects:
- kind: "Details"
properties:
edition:
type: STRING
number: 1
supported_ereaders:
type: STRING
number: 2
29 changes: 15 additions & 14 deletions example/bookstore/v1/bookstore_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,35 @@
"type": "object",
"required": [
"isbn",
"details"
"price",
"published"
],
"properties": {
"details": {
"$ref": "#/components/schemas/Details"
"edition": {
"type": "integer",
"format": "int32"
},
"id": {
"type": "string",
"readOnly": true,
"x-terraform-id": true
},
"isbn": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
},
"path": {
"type": "string",
"readOnly": true
}
}
},
"Details": {
"type": "object",
"properties": {
"edition": {
"type": "string"
},
"supported_ereaders": {
"type": "string"
"price": {
"type": "number",
"format": "float"
},
"published": {
"type": "boolean"
}
}
}
Expand Down
92 changes: 75 additions & 17 deletions schema/resourcedefinition.pb.go

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

6 changes: 6 additions & 0 deletions schema/resourcedefinition.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ enum Type {
FLOAT = 5;
BOOLEAN = 6;
OBJECT = 7;
ARRAY = 8;
}

message Property {
Expand All @@ -73,4 +74,9 @@ message Property {

// If type is OBJECT, this is the name of the object in `messages`.
string object_type = 5;

oneof array_type {
string array_object_type = 6;
Copy link
Member

@toumorokoshi toumorokoshi Sep 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this field still needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular field is necessary to define arrays of objects. It'll look way cleaner with the new definition we have above.

Type array_primitive_type = 7;
}
}
10 changes: 9 additions & 1 deletion writer/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/aep-dev/aepc/constants"
"github.com/aep-dev/aepc/parser"
"github.com/aep-dev/aepc/schema"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -174,7 +175,7 @@ func resourceToSchema(r *parser.ParsedResource) (Schema, error) {
if err != nil {
return Schema{}, err
}
properties[f.Name] = Schema{
s := Schema{
Type: t.openapi_type,
Format: t.openapi_format,
Ref: t.openapi_ref,
Expand All @@ -184,6 +185,13 @@ func resourceToSchema(r *parser.ParsedResource) (Schema, error) {
if f.Required {
required = append(required, f.Name)
}
if f.Type == schema.Type_ARRAY {
s.Items = &Schema{
Type: t.array_type.openapi_type,
Format: t.array_type.openapi_format,
}
}
properties[f.Name] = s
}
return Schema{
Type: "object",
Expand Down
Loading
Loading