Skip to content

Commit

Permalink
Refactoring (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibukazu authored Oct 16, 2024
1 parent 8d556a2 commit eedfbe6
Show file tree
Hide file tree
Showing 28 changed files with 361 additions and 287 deletions.
4 changes: 2 additions & 2 deletions go/cmd/open-ve/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func gen(cmd *cobra.Command, args []string) {
filePath := args[1]
outputDir := args[2]

logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
logger.Info("🏭 generating open-ve schema", slog.String("fileType", fileType), slog.String("filePath", filePath), slog.String("outputDir", outputDir))

var serialized []byte
Expand All @@ -71,5 +71,5 @@ func gen(cmd *cobra.Command, args []string) {
panic(fmt.Errorf("failed to write file: %w", err))
}

logger.Info("🎉 generated open-ve schema", slog.String("outputPath", outputPath))
logger.Info(" generated open-ve schema", slog.String("outputPath", outputPath))
}
6 changes: 3 additions & 3 deletions go/cmd/open-ve/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func NewRunCommand() *cobra.Command {
if mode == "slave" {
id := viper.GetString("slave.id")
if id == "" {
return failure.New(appError.ErrConfigFileSyntaxError, failure.Message("ID of the slave server is required"))
return failure.New(appError.ErrConfigError, failure.Message("ID of the slave server is required"))
}
slaveHTTPAddr := viper.GetString("slave.slaveHTTPAddr")
if slaveHTTPAddr == "" {
return failure.New(appError.ErrConfigFileSyntaxError, failure.Message("HTTP address of the slave server is required"))
return failure.New(appError.ErrConfigError, failure.Message("HTTP address of the slave server is required"))
}
masterHTTPAddr := viper.GetString("slave.masterHTTPAddr")
if masterHTTPAddr == "" {
return failure.New(appError.ErrConfigFileSyntaxError, failure.Message("HTTP address of the master server is required"))
return failure.New(appError.ErrConfigError, failure.Message("HTTP address of the master server is required"))
}
}
return nil
Expand Down
29 changes: 21 additions & 8 deletions go/cmd/open-ve/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"log/slog"
"os"

"github.com/shibukazu/open-ve/go/pkg/dsl"
"github.com/shibukazu/open-ve/go/pkg/dsl/tester"
"github.com/shibukazu/open-ve/go/pkg/dsl/util"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -35,22 +36,34 @@ func validateArgs(cmd *cobra.Command, args []string) error {
func test(cmd *cobra.Command, args []string) {
filePath := args[0]

logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
logger.Info("🧪 test open-ve schema", slog.String("filePath", filePath))
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
logger.Info("🧪 testing open-ve schema", slog.String("filePath", filePath))

dsl, err := dsl.ParseYAML(filePath)
dsl, err := util.ParseDSLYAML(filePath)
if err != nil {
panic(fmt.Errorf("failed to parse schema: %w", err))
}
result, err := dsl.Test()
result, err := tester.TestDSL(dsl)
if err != nil {
panic(fmt.Errorf("failed to test schema: %w", err))
}
numPassed := 0
numFailed := 0
numNotFound := 0
for _, validationResult := range result.ValidationResults {
if len(validationResult.FailedTestCases) > 0 {
logger.Info("❌ test failed", slog.String("id", validationResult.ID), slog.String("failedTestCases", fmt.Sprintf("%v", validationResult.FailedTestCases)))
if validationResult.TestCaseNotFound {
numNotFound++
logger.Info(fmt.Sprintf("✅ NoutFound: %s", validationResult.ID))
} else if len(validationResult.FailedTestCases) > 0 {
numFailed++
logger.Info(fmt.Sprintf("❌ FAILED : %s", validationResult.ID))
for _, failedTestCase := range validationResult.FailedTestCases {
logger.Info(fmt.Sprintf(" - %s", failedTestCase))
}
} else {
logger.Info("✅ test passed", slog.String("id", validationResult.ID))
numPassed++
logger.Info(fmt.Sprintf("✅ PASS : %s", validationResult.ID))
}
}
logger.Info(fmt.Sprintf("📊 Results: %d passed, %d failed, %d not found", numPassed, numFailed, numNotFound))
}
54 changes: 54 additions & 0 deletions go/pkg/appError/appError.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package appError

import (
"fmt"

"github.com/morikuni/failure/v2"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

const (
ErrConfigError = "ConfigError"
ErrDSLSyntaxError = "DSLSyntaxError"
ErrDSLGenerationFailed = "DSLGenerationFailed"
ErrStoreOperationFailed = "StoreOperationFailed"
ErrRequestParameterInvalid = "RequestParameterInvalid"
ErrServerError = "ServerError"
ErrRequestForwardFailed = "RequestForwardError"
ErrAuthenticationFailed = "AuthenticationFailed"
)

func ToGRPCError(err error) error {
var code codes.Code
switch failure.CodeOf(err) {
case ErrConfigError:
code = codes.InvalidArgument
case ErrDSLSyntaxError:
code = codes.InvalidArgument
case ErrDSLGenerationFailed:
code = codes.Internal
case ErrStoreOperationFailed:
code = codes.Internal
case ErrRequestParameterInvalid:
code = codes.InvalidArgument
case ErrAuthenticationFailed:
code = codes.Unauthenticated
case ErrServerError:
code = codes.Internal
case ErrRequestForwardFailed:
code = codes.Internal
default:
code = codes.Unknown
}
return status.Error(code, getGRPCErrorMessage(err))
}

func getGRPCErrorMessage(err error) string {
code := failure.CodeOf(err)
message := failure.MessageOf(err)
cause := failure.CauseOf(err)
ret := fmt.Sprintf("code: %s, message: %s, cause: %s", code, message, cause)

return ret
}
10 changes: 0 additions & 10 deletions go/pkg/appError/serverError.go

This file was deleted.

67 changes: 0 additions & 67 deletions go/pkg/appError/serviceError.go

This file was deleted.

4 changes: 2 additions & 2 deletions go/pkg/authn/presharedkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func NewPresharedKeyAuthenticator(key string) *PresharedKeyAuthenticator {
func (a *PresharedKeyAuthenticator) Authenticate(ctx context.Context) (string, error) {
authHeader, err := grpcauth.AuthFromMD(ctx, "Bearer")
if err != nil {
return "", failure.Translate(err, appError.ErrAuthMissingToken)
return "", failure.Translate(err, appError.ErrAuthenticationFailed, failure.Messagef("failed to get auth header"))
}

if authHeader != a.key {
return "", failure.New(appError.ErrAuthUnauthorized)
return "", failure.New(appError.ErrAuthenticationFailed, failure.Messagef("invalid key"))
}

return "", nil
Expand Down
63 changes: 0 additions & 63 deletions go/pkg/dsl/dsl.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,10 @@
package dsl

import (
"io"
"os"

"github.com/google/cel-go/cel"
"github.com/morikuni/failure/v2"
"github.com/shibukazu/open-ve/go/pkg/appError"
"gopkg.in/yaml.v3"
)

type Variable struct {
Name string `yaml:"name" json:"name"`
Type string `yaml:"type" json:"type"`
}

func (v *Variable) ParseVariable() (cel.EnvOption, error) {
switch v.Type {
case "int":
return cel.Variable(v.Name, cel.IntType), nil
case "uint":
return cel.Variable(v.Name, cel.UintType), nil
case "double":
return cel.Variable(v.Name, cel.DoubleType), nil
case "bool":
return cel.Variable(v.Name, cel.BoolType), nil
case "bytes":
return cel.Variable(v.Name, cel.BytesType), nil
case "string":
return cel.Variable(v.Name, cel.StringType), nil
// TODO: listとmap向けの再帰パースの実装
default:
return nil, failure.New(appError.ErrDSLSyntaxError, failure.Messagef("Unsupported variable type: %s\nPlease specify one of the following types: int, uint, double, bool, string, bytes", v.Type))
}
}

func ToCELVariables(vars []Variable) ([]cel.EnvOption, error) {
celVars := make([]cel.EnvOption, 0, len(vars))
for _, v := range vars {
v, err := v.ParseVariable()
if err != nil {
return nil, err
}
celVars = append(celVars, v)
}
return celVars, nil
}

type TestVeriable struct {
Name string `yaml:"name" json:"name"`
Value interface{} `yaml:"value" json:"value"`
Expand All @@ -68,24 +26,3 @@ type Validation struct {
type DSL struct {
Validations []Validation `yaml:"validations" json:"validations"`
}

func ParseYAML(yamlFilePath string) (*DSL, error) {
yamlFile, err := os.Open(yamlFilePath)
if err != nil {
return nil, err
}
defer yamlFile.Close()

yamlBytes, err := io.ReadAll(yamlFile)
if err != nil {
return nil, err
}

dsl := &DSL{}
err = yaml.Unmarshal(yamlBytes, &dsl)
if err != nil {
return nil, err
}

return dsl, nil
}
13 changes: 7 additions & 6 deletions go/pkg/dsl/generator/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/go-openapi/loads"
"github.com/go-openapi/spec"
"github.com/morikuni/failure/v2"
"github.com/shibukazu/open-ve/go/pkg/appError"
"github.com/shibukazu/open-ve/go/pkg/dsl"
)

Expand All @@ -16,12 +17,12 @@ func GenerateFromOpenAPI2(logger *slog.Logger, filePath string) (*dsl.DSL, error

swaggerDoc, err := loads.Spec(filePath)
if err != nil {
return nil, failure.Translate(err, failure.Messagef("failed to load OpenAPI schema file: %s", filePath))
return nil, failure.Translate(err, appError.ErrDSLGenerationFailed, failure.Messagef("failed to load OpenAPI schema file: %s", filePath))
}

paths := swaggerDoc.Spec().Paths
for path, pathItem := range paths.Paths {
logger.Info("🔍 parsing...", slog.String("path", path))
logger.Info("📈 parsing...", slog.String("path", path))
if pathItem.Post != nil {
for _, param := range pathItem.Post.Parameters {
if param.Schema != nil {
Expand Down Expand Up @@ -53,7 +54,7 @@ func resolveSchemaReference(doc *spec.Swagger, schema *spec.Schema) (*spec.Schem
if schema.Ref.String() != "" {
ref, err := spec.ResolveRef(doc, &schema.Ref)
if err != nil {
return nil, "", failure.Translate(err, failure.Messagef("failed to resolve schema reference"))
return nil, "", failure.Translate(err, appError.ErrDSLGenerationFailed, failure.Messagef("failed to resolve schema reference"))
}

refParts := strings.Split(schema.Ref.String(), "/")
Expand All @@ -68,7 +69,7 @@ func resolveSchemaReference(doc *spec.Swagger, schema *spec.Schema) (*spec.Schem

func parseParamSchema(doc *spec.Swagger, schema *spec.Schema, parentObjectName string, propName string, variables *[]dsl.Variable) error {
if schema == nil {
return failure.New(failure.Messagef("schema is nil"))
return failure.New(appError.ErrDSLGenerationFailed, failure.Messagef("schema is nil"))
}

if schema.Properties != nil {
Expand Down Expand Up @@ -104,12 +105,12 @@ func parseParamSchema(doc *spec.Swagger, schema *spec.Schema, parentObjectName s

} else if schema.Items != nil {
// TODO: Support Array
return failure.New(failure.Messagef("Array is not supported"))
return failure.New(appError.ErrDSLGenerationFailed, failure.Messagef("Array is not supported"))
} else {
// Primitive
if schema.Type != nil {
if len(schema.Type) != 1 {
return failure.New(failure.Messagef("schema.Type length is not 1"))
return failure.New(appError.ErrDSLGenerationFailed, failure.Messagef("schema.Type length is not 1"))
}
typeName := schema.Type[0]
celType := openAPITypeToCELType(typeName, schema.Format)
Expand Down
Loading

0 comments on commit eedfbe6

Please sign in to comment.