Skip to content

Commit 1a8cc77

Browse files
authored
fix: generate files to specified model package (#24)
fix: generate files to specific model package
1 parent 96af9c1 commit 1a8cc77

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

pkg/swagger/generator/shared.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ import (
3636

3737
//go:generate go-bindata -mode 420 -modtime 1482416923 -pkg=generator -ignore=.*\.sw? -ignore=.*\.md ./templates/...
3838

39-
const (
40-
// default generation targets structure
41-
defaultModelsTarget = "models"
42-
)
43-
4439
func init() {
4540
// all initializations for the generator package
4641
debugOptions()

pkg/swagger/generator/support.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func newGenerator(opts *GenOpts) (*generator, error) {
5454
Analyzed: analyzed,
5555
Models: models,
5656
Target: opts.Target,
57-
ModelsPackage: opts.LanguageOpts.ManglePackagePath("", defaultModelsTarget),
57+
ModelsPackage: opts.LanguageOpts.ManglePackagePath("", opts.ModelPackage),
5858
GenOpts: opts,
5959
}, nil
6060
}

pkg/swagger/generator/support_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ func TestGenerate_CRD2KCL(t *testing.T) {
3434
utils.DoTestDirs(t, utils.KubeTestDirs, apiConvertModel, true)
3535
}
3636

37-
func apiConvertModel(binaryPath string, sourceSpec string, outputDir string, crd bool) error {
37+
func apiConvertModel(integrationGenOpts utils.IntegrationGenOpts) error {
3838
opts := new(GenOpts)
39-
opts.Spec = sourceSpec
40-
opts.Target = outputDir
39+
opts.Spec = integrationGenOpts.SpecPath
40+
opts.Target = integrationGenOpts.TargetDir
4141
opts.KeepOrder = true
42-
opts.ValidateSpec = !crd
42+
opts.ValidateSpec = !integrationGenOpts.IsCrd
43+
opts.ModelPackage = integrationGenOpts.ModelPackage
44+
4345
if err := opts.EnsureDefaults(); err != nil {
4446
return fmt.Errorf("fill default options failed: %s", err.Error())
4547
}
46-
if crd {
48+
if integrationGenOpts.IsCrd {
4749
spec, err := crdGen.GetSpec(&crdGen.GenOpts{
4850
Spec: opts.Spec,
4951
})

pkg/utils/initegrate_gen.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ type TestCase struct {
3939
GenPath string
4040
}
4141

42+
type IntegrationGenOpts struct {
43+
BinaryPath string
44+
SpecPath string
45+
TargetDir string
46+
IsCrd bool
47+
ModelPackage string
48+
}
49+
4250
func InitTestDirs(projectRoot string, buildBinary bool) error {
4351
// calculate root dir of project/testdata/examples
4452
ProjectRoot = projectRoot
@@ -80,7 +88,7 @@ func InitTestDirs(projectRoot string, buildBinary bool) error {
8088
return nil
8189
}
8290

83-
func DoTestDirs(t *testing.T, dirs []string, convertFunc func(binaryPath, specPath, targetDir string, isCrd bool) error, crd bool) {
91+
func DoTestDirs(t *testing.T, dirs []string, convertFunc func(opts IntegrationGenOpts) error, crd bool) {
8492
for _, dir := range dirs {
8593
testCases, err := FindCases(dir)
8694
if err != nil {
@@ -97,23 +105,26 @@ func DoTestDirs(t *testing.T, dirs []string, convertFunc func(binaryPath, specPa
97105
}
98106
}
99107

100-
func DoTestConvert(testDir string, tCase TestCase, convertFunc func(binaryPath, specPath, targetDir string, isCrd bool) error, crd bool) error {
108+
func DoTestConvert(testDir string, tCase TestCase, convertFunc func(opts IntegrationGenOpts) error, crd bool) error {
101109
var tmpPrefix string
110+
var modelPackage string
102111
if crd {
103112
tmpPrefix = tmpCrdGen
113+
modelPackage = "crd_models"
104114
} else {
105115
tmpPrefix = tmpOaiGen
116+
modelPackage = "models"
106117
}
107118
tmpDir, err := os.MkdirTemp(testDir, fmt.Sprintf("%s_%s", tmpPrefix, tCase.Name))
108119
if err != nil {
109120
return fmt.Errorf("creat temp output dir failed: %v", err)
110121
}
111-
err = convertFunc(BinaryPath, tCase.SpecPath, tmpDir, crd)
122+
err = convertFunc(IntegrationGenOpts{BinaryPath: BinaryPath, SpecPath: tCase.SpecPath, TargetDir: tmpDir, IsCrd: crd, ModelPackage: modelPackage})
112123
if err != nil {
113124
return err
114125
}
115126
// compare two dir
116-
err = CompareDir(filepath.Join(tCase.GenPath, "models"), filepath.Join(tmpDir, "models"))
127+
err = CompareDir(filepath.Join(tCase.GenPath, "models"), filepath.Join(tmpDir, modelPackage))
117128
if err != nil {
118129
return err
119130
}
@@ -218,15 +229,18 @@ func readLines(path string) ([]string, error) {
218229
return lines, scanner.Err()
219230
}
220231

221-
func BinaryConvertModel(binaryPath string, sourceSpec string, outputDir string, crd bool) error {
232+
func BinaryConvertModel(integrationGenOpts IntegrationGenOpts) error {
222233
convertArgs := []string{
223234
"generate", "model", "-f",
224235
}
225-
convertArgs = append(convertArgs, sourceSpec, "-t", outputDir)
226-
if crd {
236+
convertArgs = append(convertArgs, integrationGenOpts.SpecPath, "-t", integrationGenOpts.TargetDir)
237+
if integrationGenOpts.ModelPackage != "models" {
238+
convertArgs = append(convertArgs, "-m", integrationGenOpts.ModelPackage)
239+
}
240+
if integrationGenOpts.IsCrd {
227241
convertArgs = append(convertArgs, "--skip-validation", "--crd")
228242
}
229-
cmd := exec.Command(binaryPath, convertArgs...)
243+
cmd := exec.Command(integrationGenOpts.BinaryPath, convertArgs...)
230244
cmd.Env = os.Environ()
231245
var stdout, stderr bytes.Buffer
232246
cmd.Stdout = &stdout

0 commit comments

Comments
 (0)