Skip to content
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
18 changes: 13 additions & 5 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
version: v1
version: v2
managed:
enabled: true
override:
# temporary workaround until https://github.com/aep-dev/aep-components/pull/22
# is merged and released
- module: buf.build/aep/api
file_option: go_package_prefix
value: buf.build/gen/go/aep/api/protocolbuffers/go
plugins:
- plugin: buf.build/protocolbuffers/go:v1.31.0
- remote: buf.build/protocolbuffers/go:v1.31.0
out: .
opt: paths=source_relative
- plugin: buf.build/grpc-ecosystem/gateway:v2.18.0
- remote: buf.build/grpc-ecosystem/gateway:v2.18.0
out: .
opt: paths=source_relative
- plugin: buf.build/grpc-ecosystem/openapiv2:v2.18.0
- remote: buf.build/grpc-ecosystem/openapiv2:v2.18.0
out: .
- plugin: buf.build/grpc/go:v1.3.0
- remote: buf.build/grpc/go:v1.3.0
out: .
opt:
- paths=source_relative
6 changes: 6 additions & 0 deletions buf.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Generated by buf. DO NOT EDIT.
version: v2
deps:
- name: buf.build/aep/api
commit: 2fc44491c62940f1b59218fb450701b5
digest: b5:268b5152935e5a9b48c2606944b33cdbb141152e914a364a3bbce0790f657c8adfb4635664063e89cbaa557aa9294e37c3874613d921e2343a7a709a7924f5c3
- name: buf.build/bufbuild/protovalidate
commit: f05a6f4403ce4327bae4f50f281c3ed0
digest: b5:f1d76430ee97c89cd2044e9ae1c510887b701ee7bca60564ebf82e3919e53cacefc830a0eb803277c2d98c5f313b4167e8914afc9f214332717a50b5e170e6f4
- name: buf.build/googleapis/googleapis
commit: 28151c0d0a1641bf938a7672c500e01d
digest: b5:93b70089baa4fc05a92d3e52db91a4b7812db3b57b9664f6cb301733938cb630e377a938e8a56779388171c749c1d42a2e9a6c6230f2ff45f127a8102a6a27d0
1 change: 1 addition & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ plugins:
- plugin: buf-plugin-aep
deps:
- buf.build/googleapis/googleapis:28151c0d0a1641bf938a7672c500e01d
- buf.build/aep/api:2fc44491c62940f1b59218fb450701b5
43 changes: 17 additions & 26 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ import (

"github.com/ghodss/yaml"

"github.com/aep-dev/aepc/loader"
"github.com/aep-dev/aepc/parser"
"github.com/aep-dev/aepc/schema"
"github.com/aep-dev/aep-lib-go/pkg/api"
"github.com/aep-dev/aep-lib-go/pkg/proto"
"github.com/aep-dev/aepc/validator"
"github.com/aep-dev/aepc/writer/proto"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
)

func NewCommand() *cobra.Command {
Expand All @@ -53,26 +50,21 @@ func NewCommand() *cobra.Command {

func ProcessInput(inputFile, outputFilePrefix string) error {
outputDir := filepath.Dir(outputFilePrefix)
s := &schema.Service{}
input, err := ReadFile(inputFile)
fmt.Printf("input: %s\n", string(input))
if err != nil {
return fmt.Errorf("unable to read file: %w", err)
}
ext := filepath.Ext(inputFile)
err = unmarshal(ext, input, s)
a, err := deserializeAPI(ext, input)
if err != nil {
return fmt.Errorf("unable to unmarshal file: %w", err)
}
errors := validator.ValidateService(s)
errors := validator.ValidateAPI(a)
if len(errors) > 0 {
return fmt.Errorf("error validating service: %v", errors)
}
api, err := parser.ToAPI(s)
if err != nil {
return fmt.Errorf("error building api: %w", err)
}
proto, err := proto.WriteServiceToProto(api, outputDir)
proto, err := proto.APIToProtoString(a, outputDir)
if err != nil {
return fmt.Errorf("error writing service proto: %w", err)
}
Expand All @@ -82,7 +74,7 @@ func ProcessInput(inputFile, outputFilePrefix string) error {
return fmt.Errorf("error writing file: %w", err)
}
fmt.Printf("output proto file: %s\n", protoFile)
openapi, err := api.ConvertToOpenAPIBytes()
openapi, err := a.ConvertToOpenAPIBytes()
if err != nil {
return fmt.Errorf("error building openapi: %w", err)
}
Expand All @@ -105,28 +97,27 @@ func ProcessInput(inputFile, outputFilePrefix string) error {
return nil
}

func unmarshal(ext string, b []byte, s *schema.Service) error {
func deserializeAPI(ext string, b []byte) (*api.API, error) {
switch ext {
case ".proto":
if err := loader.ReadServiceFromProto(b, s); err != nil {
return fmt.Errorf("unable to decode proto %q: %w", string(b), err)
}
case ".yaml":
asJson, err := yaml.YAMLToJSON(b)
if err != nil {
return fmt.Errorf("unable to decode yaml to JSON %q: %w", string(b), err)
return nil, fmt.Errorf("unable to decode yaml to JSON %q: %w", string(b), err)
}
if err := protojson.Unmarshal(asJson, s); err != nil {
log.Fatal(fmt.Errorf("unable to decode proto %q: %w", string(b), err))
api, err := api.LoadAPIFromJson(asJson)
if err != nil {
log.Fatal(fmt.Errorf("unable to unmarshal json %q: %w", string(b), err))
}
return api, nil
case ".json":
if err := protojson.Unmarshal(b, s); err != nil {
return fmt.Errorf("unable to decode json %q: %w", string(b), err)
api, err := api.LoadAPIFromJson(b)
if err != nil {
log.Fatal(fmt.Errorf("unable to unmarshal json %q: %w", string(b), err))
}
return api, nil
default:
return fmt.Errorf("extension %v is unsupported", ext)
return nil, fmt.Errorf("extension %v is unsupported", ext)
}
return nil
}

func ReadFile(fileName string) ([]byte, error) {
Expand Down
Loading