Skip to content

Commit ab91d47

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

13 files changed

Lines changed: 580 additions & 762 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: 339 additions & 408 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/bookstore/v1/bookstore.pb.gw.go

Lines changed: 0 additions & 54 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: 13 additions & 30 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: "*"
@@ -179,7 +181,7 @@ service Bookstore {
179181
message Book {
180182
option (google.api.resource) = {
181183
type: "bookstore.example.com/book",
182-
pattern: [ "publishers/{publisher}/books/{book}" ],
184+
pattern: [ "" ],
183185
plural: "books",
184186
singular: "book"
185187
};
@@ -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;
@@ -216,9 +218,7 @@ message Book {
216218
message BookEdition {
217219
option (google.api.resource) = {
218220
type: "bookstore.example.com/book-edition",
219-
pattern: [
220-
"publishers/{publisher}/books/{book}/editions/{book-edition}"
221-
],
221+
pattern: [ "" ],
222222
plural: "book-editions",
223223
singular: "book-edition"
224224
};
@@ -234,20 +234,17 @@ message BookEdition {
234234
message Isbn {
235235
option (google.api.resource) = {
236236
type: "bookstore.example.com/isbn",
237-
pattern: [ "isbns/{isbn}" ],
237+
pattern: [ "" ],
238238
plural: "isbns",
239239
singular: "isbn"
240240
};
241-
242-
// Field for path.
243-
string path = 10018;
244241
}
245242

246243
// A Publisher.
247244
message Publisher {
248245
option (google.api.resource) = {
249246
type: "bookstore.example.com/publisher",
250-
pattern: [ "publishers/{publisher}" ],
247+
pattern: [ "" ],
251248
plural: "publishers",
252249
singular: "publisher"
253250
};
@@ -305,9 +302,6 @@ message DeleteBookRequest {
305302
(google.api.field_behavior) = REQUIRED,
306303
(google.api.resource_reference) = { type: "bookstore.example.com/book" }
307304
];
308-
309-
// If true, the resource will be deleted, even if children still exist.
310-
bool force = 10020 [(google.api.field_behavior) = OPTIONAL];
311305
}
312306

313307
// Request message for the Listbook method
@@ -360,8 +354,6 @@ message ArchiveBookRequest {
360354

361355
// Response message for the archive method
362356
message ArchiveBookResponse {
363-
// Field for success.
364-
bool success = 1;
365357
}
366358

367359
// A Create request for a book-edition resource.
@@ -372,9 +364,6 @@ message CreateBookEditionRequest {
372364
(google.api.resource_reference) = { }
373365
];
374366

375-
// An id that uniquely identifies the resource within the collection
376-
string id = 10014;
377-
378367
// The resource to perform the operation on.
379368
BookEdition book_edition = 10015 [(google.api.field_behavior) = REQUIRED];
380369
}
@@ -429,9 +418,6 @@ message CreateIsbnRequest {
429418
(google.api.resource_reference) = { }
430419
];
431420

432-
// An id that uniquely identifies the resource within the collection
433-
string id = 10014;
434-
435421
// The resource to perform the operation on.
436422
Isbn isbn = 10015 [(google.api.field_behavior) = REQUIRED];
437423
}
@@ -515,9 +501,6 @@ message DeletePublisherRequest {
515501
(google.api.field_behavior) = REQUIRED,
516502
(google.api.resource_reference) = { type: "bookstore.example.com/publisher" }
517503
];
518-
519-
// If true, the resource will be deleted, even if children still exist.
520-
bool force = 10020 [(google.api.field_behavior) = OPTIONAL];
521504
}
522505

523506
// Request message for the Listpublisher method

0 commit comments

Comments
 (0)