Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
feat: add a prefix to generated function and procedure names (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Sep 3, 2024
1 parent f34e81c commit 3112447
Show file tree
Hide file tree
Showing 17 changed files with 2,982 additions and 76 deletions.
41 changes: 30 additions & 11 deletions command/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ConvertCommandArguments struct {
Format string `help:"The output format, is one of json, yaml. If the output is set, automatically detect the format in the output file extension" default:"json"`
Strict bool `help:"Require strict validation" default:"false"`
Pure bool `help:"Return the pure NDC schema only" default:"false"`
Prefix string `help:"Add a prefix to the function and procedure names"`
TrimPrefix string `help:"Trim the prefix in URL, e.g. /v1"`
EnvPrefix string `help:"The environment variable prefix for security values, e.g. PET_STORE"`
MethodAlias map[string]string `help:"Alias names for HTTP method. Used for prefix renaming, e.g. getUsers, postUser"`
Expand All @@ -40,6 +41,7 @@ func CommandConvertToNDCSchema(args *ConvertCommandArguments, logger *slog.Logge
slog.String("output", args.Output),
slog.String("spec", args.Spec),
slog.String("format", args.Format),
slog.String("prefix", args.Prefix),
slog.String("trim_prefix", args.TrimPrefix),
slog.String("env_prefix", args.EnvPrefix),
slog.Any("patch_before", args.PatchBefore),
Expand Down Expand Up @@ -125,17 +127,30 @@ func CommandConvertToNDCSchema(args *ConvertCommandArguments, logger *slog.Logge

// ConvertConfig represents the content of convert config file
type ConvertConfig struct {
File string `json:"file" yaml:"file"`
Spec schema.SchemaSpecType `json:"spec,omitempty" yaml:"spec"`
MethodAlias map[string]string `json:"methodAlias,omitempty" yaml:"methodAlias"`
TrimPrefix string `json:"trimPrefix,omitempty" yaml:"trimPrefix"`
EnvPrefix string `json:"envPrefix,omitempty" yaml:"envPrefix"`
Pure bool `json:"pure,omitempty" yaml:"pure"`
Strict bool `json:"strict,omitempty" yaml:"strict"`
PatchBefore []utils.PatchConfig `json:"patchBefore,omitempty" yaml:"patchBefore"`
PatchAfter []utils.PatchConfig `json:"patchAfter,omitempty" yaml:"patchAfter"`
AllowedContentTypes []string `json:"allowedContentTypes,omitempty" yaml:"allowedContentTypes"`
Output string `json:"output,omitempty" yaml:"output"`
// File path needs to be converted
File string `json:"file" yaml:"file" jsonschema:"required"`
// The API specification of the file, is one of oas3 (openapi3), oas2 (openapi2)
Spec schema.SchemaSpecType `json:"spec,omitempty" yaml:"spec" jsonschema:"default=oas3"`
// Alias names for HTTP method. Used for prefix renaming, e.g. getUsers, postUser
MethodAlias map[string]string `json:"methodAlias,omitempty" yaml:"methodAlias"`
// Add a prefix to the function and procedure names
Prefix string `json:"prefix,omitempty" yaml:"prefix"`
// Trim the prefix in URL, e.g. /v1
TrimPrefix string `json:"trimPrefix,omitempty" yaml:"trimPrefix"`
// The environment variable prefix for security values, e.g. PET_STORE
EnvPrefix string `json:"envPrefix,omitempty" yaml:"envPrefix"`
// Return the pure NDC schema only
Pure bool `json:"pure,omitempty" yaml:"pure"`
// Require strict validation
Strict bool `json:"strict,omitempty" yaml:"strict"`
// Patch files to be applied into the input file before converting
PatchBefore []utils.PatchConfig `json:"patchBefore,omitempty" yaml:"patchBefore"`
// Patch files to be applied into the input file after converting
PatchAfter []utils.PatchConfig `json:"patchAfter,omitempty" yaml:"patchAfter"`
// Allowed content types. All content types are allowed by default
AllowedContentTypes []string `json:"allowedContentTypes,omitempty" yaml:"allowedContentTypes"`
// The location where the ndc schema file will be generated. Print to stdout if not set
Output string `json:"output,omitempty" yaml:"output"`
}

// ConvertToNDCSchema converts to NDC REST schema from config
Expand All @@ -155,6 +170,7 @@ func ConvertToNDCSchema(config *ConvertConfig, logger *slog.Logger) (*schema.NDC
var errs []error
options := openapi.ConvertOptions{
MethodAlias: config.MethodAlias,
Prefix: config.Prefix,
TrimPrefix: config.TrimPrefix,
EnvPrefix: config.EnvPrefix,
AllowedContentTypes: config.AllowedContentTypes,
Expand Down Expand Up @@ -192,6 +208,9 @@ func ResolveConvertConfigArguments(config *ConvertConfig, configDir string, args
if len(args.MethodAlias) > 0 {
config.MethodAlias = args.MethodAlias
}
if args.Prefix != "" {
config.Prefix = args.Prefix
}
if args.TrimPrefix != "" {
config.TrimPrefix = args.TrimPrefix
}
Expand Down
3 changes: 3 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ output: ""
# @enum: oas2, oas3
spec: oas3

# -- Add a prefix to the function and procedure names
# prefix: ""

# -- Trim the prefix in URL, e.g. /v1
# trimPrefix: ""

Expand Down
40 changes: 28 additions & 12 deletions jsonschema/convert-config.jsonschema
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,72 @@
"ConvertConfig": {
"properties": {
"file": {
"type": "string"
"type": "string",
"description": "File path needs to be converted"
},
"spec": {
"$ref": "#/$defs/SchemaSpecType"
"$ref": "#/$defs/SchemaSpecType",
"description": "The API specification of the file, is one of oas3 (openapi3), oas2 (openapi2)"
},
"methodAlias": {
"additionalProperties": {
"type": "string"
},
"type": "object"
"type": "object",
"description": "Alias names for HTTP method. Used for prefix renaming, e.g. getUsers, postUser"
},
"prefix": {
"type": "string",
"description": "Add a prefix to the function and procedure names"
},
"trimPrefix": {
"type": "string"
"type": "string",
"description": "Trim the prefix in URL, e.g. /v1"
},
"envPrefix": {
"type": "string"
"type": "string",
"description": "The environment variable prefix for security values, e.g. PET_STORE"
},
"pure": {
"type": "boolean"
"type": "boolean",
"description": "Return the pure NDC schema only"
},
"strict": {
"type": "boolean"
"type": "boolean",
"description": "Require strict validation"
},
"patchBefore": {
"items": {
"$ref": "#/$defs/PatchConfig"
},
"type": "array"
"type": "array",
"description": "Patch files to be applied into the input file before converting"
},
"patchAfter": {
"items": {
"$ref": "#/$defs/PatchConfig"
},
"type": "array"
"type": "array",
"description": "Patch files to be applied into the input file after converting"
},
"allowedContentTypes": {
"items": {
"type": "string"
},
"type": "array"
"type": "array",
"description": "Allowed content types. All content types are allowed by default"
},
"output": {
"type": "string"
"type": "string",
"description": "The location where the ndc schema file will be generated. Print to stdout if not set"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"file"
]
],
"description": "ConvertConfig represents the content of convert config file"
},
"PatchConfig": {
"properties": {
Expand Down
14 changes: 12 additions & 2 deletions jsonschema/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ func main() {
}

func jsonSchemaConvertConfig() error {
reflectSchema := jsonschema.Reflect(&command.ConvertConfig{})
r := new(jsonschema.Reflector)
if err := r.AddGoComments("github.com/hasura/ndc-rest-schema/command", "../command"); err != nil {
return err
}
reflectSchema := r.Reflect(&command.ConvertConfig{})

schemaBytes, err := json.MarshalIndent(reflectSchema, "", " ")
if err != nil {
return err
Expand All @@ -30,7 +35,12 @@ func jsonSchemaConvertConfig() error {
}

func jsonSchemaNdcRESTSchema() error {
reflectSchema := jsonschema.Reflect(&schema.NDCRestSchema{})
r := new(jsonschema.Reflector)
if err := r.AddGoComments("github.com/hasura/ndc-rest-schema/schema", "../schema"); err != nil {
return err
}

reflectSchema := r.Reflect(&schema.NDCRestSchema{})
schemaBytes, err := json.MarshalIndent(reflectSchema, "", " ")
if err != nil {
return err
Expand Down
Loading

0 comments on commit 3112447

Please sign in to comment.