-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
|
||
"github.com/deepmap/oapi-codegen/pkg/codegen/exp" | ||
Check failure on line 11 in cmd/exp/oapi-codegen/oapi-codegen.go GitHub Actions / Build (go-version, 1.21)
Check failure on line 11 in cmd/exp/oapi-codegen/oapi-codegen.go GitHub Actions / Build (go-version-file, go.mod)
Check failure on line 11 in cmd/exp/oapi-codegen/oapi-codegen.go GitHub Actions / Build (go-version, 1.21)
|
||
"github.com/deepmap/oapi-codegen/pkg/util" | ||
Check failure on line 12 in cmd/exp/oapi-codegen/oapi-codegen.go GitHub Actions / Build (go-version, 1.21)
Check failure on line 12 in cmd/exp/oapi-codegen/oapi-codegen.go GitHub Actions / Build (go-version-file, go.mod)
Check failure on line 12 in cmd/exp/oapi-codegen/oapi-codegen.go GitHub Actions / Build (go-version, 1.21)
|
||
) | ||
|
||
func errExit(format string, args ...interface{}) { | ||
_, _ = fmt.Fprintf(os.Stderr, format, args...) | ||
os.Exit(1) | ||
} | ||
|
||
var ( | ||
// flagConfigFile specifies a path to an oapi-codegen configuration file | ||
flagConfigFile string | ||
// flagLogFile specifies the path to a file which will contain debug logs. | ||
// An empty path means log messages will be discarded. | ||
flagLogFile string | ||
) | ||
|
||
func main() { | ||
flag.StringVar(&flagConfigFile, "config", "", "A YAML config file that controls oapi-codegen behavior.") | ||
flag.StringVar(&flagLogFile, "log-file", "", "A path where log messages will be written") | ||
|
||
flag.Parse() | ||
|
||
if flag.NArg() < 1 { | ||
errExit("Please specify a path to a OpenAPI 3.0 spec file\n") | ||
} else if flag.NArg() > 1 { | ||
errExit("Only one OpenAPI 3.0 spec file is accepted and it must be the last CLI argument\n") | ||
} | ||
|
||
// Read the config file | ||
var config exp.Configuration | ||
if flagConfigFile != "" { | ||
buf, err := os.ReadFile(flagConfigFile) | ||
if err != nil { | ||
errExit("error reading config file '%s': %v\n", flagConfigFile, err) | ||
} | ||
config, err = exp.LoadConfiguration(buf) | ||
if err != nil { | ||
errExit("error loading configuration file: %v", err) | ||
} | ||
} | ||
|
||
spec, err := util.LoadSwagger(flag.Arg(0)) | ||
if err != nil { | ||
errExit("error loading OpenAPI spec in %s\n: %s", flag.Arg(0), err) | ||
} | ||
|
||
var logWriter io.Writer | ||
if flagLogFile != "" { | ||
logFile, err := os.OpenFile(flagLogFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) | ||
if err != nil { | ||
errExit("error opening log file '%s': %v", logWriter, err) | ||
} | ||
logWriter = logFile | ||
defer logFile.Close() | ||
} else { | ||
logWriter = io.Discard | ||
} | ||
|
||
internalLogger := log.New(logWriter, "", log.Lshortfile) | ||
|
||
var outputBuffer bytes.Buffer | ||
err = exp.Generate(spec, config, &outputBuffer, internalLogger) | ||
if err != nil { | ||
errExit("error generating code: %v\n", err) | ||
} | ||
fmt.Println(outputBuffer.String()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package exp | ||
|
||
import ( | ||
"io" | ||
"log" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
) | ||
|
||
func Generate(spec *openapi3.T, cfg Configuration, output io.Writer, logger *log.Logger) error { | ||
tree := BuildSchemaTree(spec) | ||
schemasByPath := GenerateSchemaInfosFromTree(tree) | ||
|
||
sortedSchemaKeys := SortedMapKeys(schemasByPath) | ||
logger.Println("Found paths:") | ||
for _, k := range sortedSchemaKeys { | ||
logger.Println(" ", k) | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package exp | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type Configuration struct { | ||
ImportMapping map[string]ImportSpec `yaml:"import-mapping,omitempty"` | ||
TypeOverrides OpenAPIToGoTypeMapping `yaml:"type-mapping,omitempty"` | ||
|
||
// typeMapping is the composite mapping of the default and the overrides | ||
typeMapping OpenAPIToGoTypeMapping `yaml:"-"` | ||
} | ||
|
||
type ImportSpec struct { | ||
Alias string `yaml:"alias,omitempty"` | ||
Package string `yaml:"package"` | ||
} | ||
|
||
func LoadConfiguration(buf []byte) (Configuration, error) { | ||
var cfg Configuration | ||
err := yaml.Unmarshal(buf, &cfg) | ||
if err != nil { | ||
return Configuration{}, fmt.Errorf("loading configuration: %w", err) | ||
} | ||
|
||
// Now, merge any overrides on top of default values. | ||
cfg.typeMapping = OverrideTypeMapping(cfg.typeMapping, GetDefaultTypeMapping()) | ||
|
||
return cfg, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package exp | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetDefaultTypeMapping(t *testing.T) { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package exp |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
|
||
v2 "github.com/deepmap/oapi-codegen/v2/pkg/codegen/exp" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) < 2 { | ||
log.Fatalln("Please specify an OpenAPI spec path") | ||
} | ||
|
||
buf, err := os.ReadFile(os.Args[1]) | ||
if err != nil { | ||
log.Fatalln("Error reading spec: ", err) | ||
} | ||
|
||
loader := openapi3.NewLoader() | ||
spec, err := loader.LoadFromData(buf) | ||
if err != nil { | ||
log.Fatalln("Error loading spec: ", err) | ||
} | ||
|
||
root := v2.BuildSchemaTree(spec) | ||
printTree(0, root) | ||
} | ||
|
||
func printTree(indentLevel int, node *v2.PathTreeNode) { | ||
padding := strings.Repeat(" ", indentLevel) | ||
if node.Ref != "" { | ||
fmt.Printf(padding+"%s (Ref=\"%s\")\n", node.PathElement, node.Ref) | ||
} else { | ||
fmt.Printf(padding+"%s\n", node.PathElement) | ||
} | ||
for _, c := range node.Children { | ||
printTree(indentLevel+1, c) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
integer: | ||
default: int | ||
formats: | ||
int64: | ||
type: int64 | ||
int32: | ||
type: int32 | ||
int16: | ||
type: int16 | ||
int8: | ||
type: int8 | ||
int: | ||
type: int | ||
uint64: | ||
type: uint64 | ||
uint32: | ||
type: uint32 | ||
uint16: | ||
type: uint16 | ||
uint8: | ||
type: uint8 | ||
uint: | ||
type: uint | ||
number: | ||
default: float32 | ||
formats: | ||
double: | ||
type: float64 | ||
float: | ||
type: float32 | ||
boolean: | ||
default: bool | ||
string: | ||
default: string | ||
formats: | ||
byte: | ||
type: "[]byte" | ||
nullable: true | ||
email: | ||
type: openapi_types.Email | ||
import: openapi_types github.com/deepmap/oapi-codegen/pkg/types | ||
date: | ||
type: openapi_types.Date | ||
import: openapi_types github.com/deepmap/oapi-codegen/pkg/types | ||
date-time: | ||
type: time.Time | ||
import: time | ||
json: | ||
type: json.RawMessage | ||
import: encoding/json | ||
uuid: | ||
type: openapi_types.UUID | ||
import: openapi_types github.com/deepmap/oapi-codegen/pkg/types | ||
binary: | ||
type: openapi_types.File | ||
import: openapi_types github.com/deepmap/oapi-codegen/pkg/types |