Skip to content

Commit 2dd6934

Browse files
committed
using aep-lib-go's generation logic
1 parent 23904f5 commit 2dd6934

12 files changed

Lines changed: 436 additions & 464 deletions

cmd/root.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222

2323
"github.com/ghodss/yaml"
2424

25-
"encoding/json"
26-
2725
"github.com/aep-dev/aep-lib-go/pkg/api"
2826
"github.com/aep-dev/aep-lib-go/pkg/proto"
2927
"github.com/aep-dev/aepc/validator"
@@ -52,22 +50,21 @@ func NewCommand() *cobra.Command {
5250

5351
func ProcessInput(inputFile, outputFilePrefix string) error {
5452
outputDir := filepath.Dir(outputFilePrefix)
55-
var a api.API
5653
input, err := ReadFile(inputFile)
5754
fmt.Printf("input: %s\n", string(input))
5855
if err != nil {
5956
return fmt.Errorf("unable to read file: %w", err)
6057
}
6158
ext := filepath.Ext(inputFile)
62-
err = unmarshal(ext, input, &a)
59+
a, err := deserializeAPI(ext, input)
6360
if err != nil {
6461
return fmt.Errorf("unable to unmarshal file: %w", err)
6562
}
66-
errors := validator.ValidateAPI(&a)
63+
errors := validator.ValidateAPI(a)
6764
if len(errors) > 0 {
6865
return fmt.Errorf("error validating service: %v", errors)
6966
}
70-
proto, err := proto.APIToProtoString(&a, outputDir)
67+
proto, err := proto.APIToProtoString(a, outputDir)
7168
if err != nil {
7269
return fmt.Errorf("error writing service proto: %w", err)
7370
}
@@ -100,24 +97,27 @@ func ProcessInput(inputFile, outputFilePrefix string) error {
10097
return nil
10198
}
10299

103-
func unmarshal(ext string, b []byte, a *api.API) error {
100+
func deserializeAPI(ext string, b []byte) (*api.API, error) {
104101
switch ext {
105102
case ".yaml":
106103
asJson, err := yaml.YAMLToJSON(b)
107104
if err != nil {
108-
return fmt.Errorf("unable to decode yaml to JSON %q: %w", string(b), err)
105+
return nil, fmt.Errorf("unable to decode yaml to JSON %q: %w", string(b), err)
109106
}
110-
if err := json.Unmarshal(asJson, a); err != nil {
111-
log.Fatal(fmt.Errorf("unable to decode proto %q: %w", string(b), err))
107+
api, err := api.LoadAPIFromJson(asJson)
108+
if err != nil {
109+
log.Fatal(fmt.Errorf("unable to unmarshal json %q: %w", string(b), err))
112110
}
111+
return api, nil
113112
case ".json":
114-
if err := json.Unmarshal(b, a); err != nil {
115-
return fmt.Errorf("unable to decode json %q: %w", string(b), err)
113+
api, err := api.LoadAPIFromJson(b)
114+
if err != nil {
115+
log.Fatal(fmt.Errorf("unable to unmarshal json %q: %w", string(b), err))
116116
}
117+
return api, nil
117118
default:
118-
return fmt.Errorf("extension %v is unsupported", ext)
119+
return nil, fmt.Errorf("extension %v is unsupported", ext)
119120
}
120-
return nil
121121
}
122122

123123
func ReadFile(fileName string) ([]byte, error) {

example/bookstore/v1/bookstore.pb.go

Lines changed: 262 additions & 270 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/bookstore/v1/bookstore.proto

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ service Bookstore {
6969
};
7070
}
7171

72-
// archive abook.
73-
rpc archiveBook ( ArchiveBookRequest ) returns ( aep.api.Operation ) {
72+
// archive a book.
73+
rpc ArchiveBook ( ArchiveBookRequest ) returns ( aep.api.Operation ) {
74+
option (aep.api.operation_info) = { response_type: "example.bookstore.v1.ArchiveBookResponse" };
75+
7476
option (google.api.http) = {
7577
post: "/{path=publishers/*/books/*}:archive",
7678
body: "*"
@@ -193,20 +195,20 @@ message Book {
193195
string lastName = 2;
194196
}
195197

196-
// Field for author.
197-
repeated Author author = 5;
198-
199198
// Field for isbn.
200199
repeated string isbn = 1 [(google.api.field_behavior) = REQUIRED];
201200

202201
// Field for price.
203-
float price = 2 [(google.api.field_behavior) = REQUIRED];
202+
int32 price = 2 [(google.api.field_behavior) = REQUIRED];
204203

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

208207
// Field for edition.
209-
int32 edition = 4;
208+
int32 edition = 4 [(google.api.field_behavior) = REQUIRED];
209+
210+
// Field for author.
211+
repeated Author author = 5 [(google.api.field_behavior) = REQUIRED];
210212

211213
// Field for path.
212214
string path = 10018;
@@ -360,8 +362,6 @@ message ArchiveBookRequest {
360362

361363
// Response message for the archive method
362364
message ArchiveBookResponse {
363-
// Field for success.
364-
bool success = 1;
365365
}
366366

367367
// A Create request for a book-edition resource.

example/bookstore/v1/bookstore.swagger.json

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@
836836
"/{path}:archive": {
837837
"post": {
838838
"summary": "archive a book.",
839-
"operationId": "Bookstore_archiveBook",
839+
"operationId": "Bookstore_ArchiveBook",
840840
"responses": {
841841
"200": {
842842
"description": "A successful response.",
@@ -981,14 +981,6 @@
981981
"v1Book": {
982982
"type": "object",
983983
"properties": {
984-
"author": {
985-
"type": "array",
986-
"items": {
987-
"type": "object",
988-
"$ref": "#/definitions/BookAuthor"
989-
},
990-
"description": "Field for author."
991-
},
992984
"isbn": {
993985
"type": "array",
994986
"items": {
@@ -997,8 +989,8 @@
997989
"description": "Field for isbn."
998990
},
999991
"price": {
1000-
"type": "number",
1001-
"format": "float",
992+
"type": "integer",
993+
"format": "int32",
1002994
"description": "Field for price."
1003995
},
1004996
"published": {
@@ -1010,6 +1002,14 @@
10101002
"format": "int32",
10111003
"description": "Field for edition."
10121004
},
1005+
"author": {
1006+
"type": "array",
1007+
"items": {
1008+
"type": "object",
1009+
"$ref": "#/definitions/BookAuthor"
1010+
},
1011+
"description": "Field for author."
1012+
},
10131013
"path": {
10141014
"type": "string",
10151015
"description": "Field for path."
@@ -1019,7 +1019,9 @@
10191019
"required": [
10201020
"isbn",
10211021
"price",
1022-
"published"
1022+
"published",
1023+
"edition",
1024+
"author"
10231025
]
10241026
},
10251027
"v1BookEdition": {
Lines changed: 47 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,58 @@
11
# normally this would be suffixed with the domain (.com)
22
name: "bookstore.example.com"
3-
url: "http://localhost:8081"
3+
server_url: "http://localhost:8081"
44
contact:
55
name: "API support"
66
email: "aepsupport@aep.dev"
77
resources:
8-
# example of a simple resource
9-
- kind: "publisher"
8+
publisher:
9+
singular: "publisher"
1010
plural: "publishers"
1111
schema:
12-
type: "object"
12+
type: object
1313
properties:
1414
description:
1515
type: string
1616
x-aep-field-number: 1
1717
methods:
18-
create: {}
19-
read: {}
18+
create:
19+
supports_user_settable_create: true
20+
get: {}
2021
update: {}
2122
delete: {}
2223
list:
2324
supports_filter: true
2425
supports_skip: true
2526
apply: {}
26-
# example of a child resource
27-
- kind: "book"
27+
book:
28+
singular: "book"
2829
plural: "books"
29-
# the parents should specify the parents of the resource. It takes in the
30-
# kind.
31-
parents:
32-
- "publisher"
30+
parents: ["publisher"]
3331
schema:
34-
type: "object"
35-
required: ["author", "isbn", "price", "published"]
32+
type: object
33+
required: ["author", "edition", "isbn", "price", "published"]
3634
properties:
3735
isbn:
36+
type: array
3837
items:
3938
type: string
4039
x-aep-field-number: 1
4140
price:
42-
type: float
41+
type: integer
42+
format: int32
4343
x-aep-field-number: 2
4444
published:
4545
type: boolean
4646
x-aep-field-number: 3
4747
edition:
4848
type: integer
49+
format: int32
4950
x-aep-field-number: 4
5051
author:
52+
type: array
5153
x-aep-field-number: 5
5254
items:
55+
type: object
5356
properties:
5457
firstName:
5558
type: string
@@ -58,73 +61,50 @@ resources:
5861
type: string
5962
x-aep-field-number: 2
6063
methods:
61-
create: {}
62-
read: {}
64+
create:
65+
supports_user_settable_create: true
66+
get: {}
6367
update: {}
6468
delete: {}
6569
list:
6670
has_unreachable_resources: true
6771
apply: {}
68-
custom:
69-
- name: "archive"
70-
method_type: POST
71-
request:
72-
type: "object"
73-
properties: {}
74-
response:
75-
type: "object"
76-
properties:
77-
success:
78-
type: boolean
79-
x-aep-field-number: 1
80-
# other example resources that might be interesting to add:
81-
# authors, which could be a reference for book
82-
# authors could have a reference to publishers too
83-
# example of a child resource, with a redudant type name.
84-
# aepc will remove the redundant component in the path pattern
85-
- kind: "book-edition"
72+
custom_methods:
73+
- name: "archive"
74+
method: "POST"
75+
is_long_running: true
76+
request:
77+
type: object
78+
properties: {}
79+
response:
80+
type: object
81+
properties:
82+
success:
83+
type: boolean
84+
book-edition:
85+
singular: "book-edition"
8686
plural: "book-editions"
87-
# the parents should specify the parents of the resource. It takes in the
88-
# kind.
89-
parents:
90-
- "book"
87+
parents: ["book"]
9188
schema:
89+
type: object
9290
required: ["displayname"]
93-
type: "object"
9491
properties:
9592
displayname:
96-
x-aep-field-number: 1
9793
type: string
94+
x-aep-field-number: 1
9895
methods:
99-
create: {}
100-
read: {}
96+
create:
97+
supports_user_settable_create: true
98+
get: {}
10199
list: {}
102100
delete: {}
103-
- kind: "isbn"
101+
isbn:
102+
singular: "isbn"
104103
plural: "isbns"
105104
schema:
106-
type: "object"
105+
type: object
107106
methods:
108-
read: {}
107+
get: {}
109108
list: {}
110-
create: {}
111-
# blocked on https://github.com/aep-dev/api-linter/issues/97
112-
# non_client_settable_id: true
113-
- kind: "operation"
114-
plural: "operations"
115-
schema:
116-
x-aep-proto-message-name: "aep.api.Operation",
117-
type: "object"
118-
properties:
119-
path:
120-
type: string
121-
x-aep-field-number: 1
122-
metadata:
123-
type: string
124-
x-aep-field-number: 2
125-
done:
126-
type: boolean
127-
x-aep-field-number: 3
128-
response:
129-
type: object
130-
x-aep-field-number: 5
109+
create:
110+
supports_user_settable_create: true

example/bookstore/v1/bookstore_grpc.pb.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)