Skip to content

Commit

Permalink
Objects (#22)
Browse files Browse the repository at this point in the history
* more changes

* wip

* objects

* objects done

* my hack

* pr changes

* rebase

* rebase issue
  • Loading branch information
rambleraptor authored Sep 13, 2024
1 parent 2ef9391 commit 1e577cf
Show file tree
Hide file tree
Showing 13 changed files with 659 additions and 378 deletions.
10 changes: 8 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,20 @@ func ProcessInput(inputFile, outputFilePrefix string) error {
if err != nil {
return fmt.Errorf("error parsing service: %w", err)
}
proto, _ := proto.WriteServiceToProto(ps, outputDir)
proto, err := proto.WriteServiceToProto(ps, outputDir)
if(err != nil) {
return fmt.Errorf("error writing service proto %w", err)
}
protoFile := fmt.Sprintf("%s.proto", outputFilePrefix)
err = WriteFile(protoFile, proto)
if err != nil {
return fmt.Errorf("error writing file: %w", err)
}
fmt.Printf("output proto file: %s\n", protoFile)
openapi, _ := openapi.WriteServiceToOpenAPI(ps)
openapi, err := openapi.WriteServiceToOpenAPI(ps)
if err != nil {
return fmt.Errorf("error building openapi %s", err)
}
openapiFile := fmt.Sprintf("%s_openapi.json", outputFilePrefix)
err = WriteFile(openapiFile, openapi)
if err != nil {
Expand Down
372 changes: 215 additions & 157 deletions example/bookstore/v1/bookstore.pb.go

Large diffs are not rendered by default.

21 changes: 12 additions & 9 deletions example/bookstore/v1/bookstore.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,8 @@ message Book {
// Field for isbn.
string isbn = 1;

// Field for price.
float price = 2;

// Field for published.
bool published = 3;

// Field for edition.
int32 edition = 4;
// Field for details.
Details details = 2;

// Field for path.
string path = 10000;
Expand All @@ -88,7 +82,7 @@ message CreateBookRequest {
(google.api.resource_reference) = { }
];

// An id that uniquely identifies the resource within the collection%!(EXTRA string=Book)
// An id that uniquely identifies the resource within the collection
string id = 10014;

// The resource to perform the operation on.
Expand Down Expand Up @@ -163,3 +157,12 @@ 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;
}
32 changes: 18 additions & 14 deletions example/bookstore/v1/bookstore.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
},
{
"name": "id",
"description": "An id that uniquely identifies the resource within the collection%!(EXTRA string=Book)",
"description": "An id that uniquely identifies the resource within the collection",
"in": "query",
"required": false,
"type": "string"
Expand Down Expand Up @@ -302,19 +302,9 @@
"type": "string",
"description": "Field for isbn."
},
"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."
"details": {
"$ref": "#/definitions/v1Details",
"description": "Field for details."
},
"path": {
"type": "string",
Expand All @@ -327,6 +317,20 @@
},
"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."
},
"v1ListBookResponse": {
"type": "object",
"properties": {
Expand Down
22 changes: 12 additions & 10 deletions example/bookstore/v1/bookstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@ resources:
type: STRING
number: 1
required: true
price:
type: FLOAT
details:
type: OBJECT
number: 2
required: true
published:
type: BOOLEAN
number: 3
required: true
edition:
type: INT32
number: 4
required: false
object_type: Details
# parents:
# - "bookstore.example.com/Publisher"
methods:
Expand All @@ -44,3 +37,12 @@ resources:
# - "Publisher"
# methods:
# read: {}
objects:
- kind: "Details"
properties:
edition:
type: STRING
number: 1
supported_ereaders:
type: STRING
number: 2
24 changes: 13 additions & 11 deletions example/bookstore/v1/bookstore_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,11 @@
"type": "object",
"required": [
"isbn",
"price",
"published"
"details"
],
"properties": {
"edition": {
"type": "integer",
"format": "int32"
"details": {
"$ref": "#/components/schemas/Details"
},
"id": {
"type": "string",
Expand All @@ -130,13 +128,17 @@
"path": {
"type": "string",
"readOnly": true
}
}
},
"Details": {
"type": "object",
"properties": {
"edition": {
"type": "string"
},
"price": {
"type": "number",
"format": "float"
},
"published": {
"type": "boolean"
"supported_ereaders": {
"type": "string"
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type ParsedResource struct {
*schema.Resource
Type string
Parents []*ParsedResource

IsResource bool
}

type ParsedProperty struct {
Expand All @@ -41,13 +43,37 @@ func NewParsedService(s *schema.Service) (*ParsedService, error) {
if err != nil {
return nil, fmt.Errorf("unable to build service %q: %w", s, err)
}
err = loadObjectsByType(s.Objects, s, &resourceByType)
if err != nil {
return nil, fmt.Errorf("unable to build service objects %q: %w", s, err)
}
ps := ParsedService{
Service: s,
ResourceByType: resourceByType,
}
return &ps, nil
}

func ParsedResourceForObject(r *schema.Object, s *schema.Service) *ParsedResource {
t := fmt.Sprintf("%s/%s", s.Name, r.Kind)
return &ParsedResource{
Type: t,
Resource: &schema.Resource{
Kind: r.Kind,
Properties: r.Properties,
},
IsResource: false,
}
}

func loadObjectsByType(o []*schema.Object, s *schema.Service, m *map[string]*ParsedResource) (error) {
for _, r := range o {
t := fmt.Sprintf("%s/%s", s.Name, r.Kind)
(*m)[t] = ParsedResourceForObject(r, s)
}
return nil
}

func loadResourceByType(s *schema.Service) (map[string]*ParsedResource, error) {
resourceByType := map[string]*ParsedResource{}
for _, r := range s.Resources {
Expand All @@ -56,6 +82,7 @@ func loadResourceByType(s *schema.Service) (map[string]*ParsedResource, error) {
Resource: r,
Type: t,
Parents: []*ParsedResource{},
IsResource: true,
}
}
// populate resource parents
Expand Down
Loading

0 comments on commit 1e577cf

Please sign in to comment.