Skip to content

Commit 2d9e778

Browse files
authored
✨ Update the generator to templatize the package name (#84)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Proprietary --> ### Description This change updates the generator to make the package name for the generated files based on the arguments, this is to support packages that are not named `client`. `-c/--client_path` is now a mandatory argument. ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [X] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent 937925f commit 2d9e778

15 files changed

+109
-60
lines changed

.github/workflows/update-client.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ jobs:
9393
run: |
9494
cd generator
9595
go mod tidy
96-
go run main.go -i ../all-service-definition/all-def-flat.yaml -o ../extensions/extension_entities.gen.go -t templates/entities.go.tmpl -g collections
97-
go run main.go -i ../all-service-definition/all-def-flat.yaml -o ../extensions/extension_jobs.gen.go -t templates/jobs.go.tmpl -g jobs
96+
go run main.go -i ../all-service-definition/all-def-flat.yaml -o ../extensions/extension_entities.gen.go -t templates/entities.go.tmpl -g collections -c ../client
97+
go run main.go -i ../all-service-definition/all-def-flat.yaml -o ../extensions/extension_jobs.gen.go -t templates/jobs.go.tmpl -g jobs -c ../client
9898
go run main.go -i ../all-service-definition/all-def-flat.yaml -o ../extensions/extension_link_followers.gen.go -t templates/linkfollowers.go.tmpl -g links -c ../client
9999
- name: Copy extensions to client
100100
run: |

changes/20250408155442.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: Update the generator to templatize the package name

generator/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func RunCLI(ctx context.Context) (err error) {
8181
return
8282
}
8383

84-
err = codegen.CopyStaticFiles(ctx, filepath.Dir(d.DestinationPath))
84+
err = codegen.CopyStaticFiles(ctx, d.PackageName, filepath.Dir(d.DestinationPath))
8585
if err != nil {
8686
return
8787
}

generator/codegen/collection_extensions_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ func TestCollectionExtensions(t *testing.T) {
1515
t.Run("Test Successful Generation", func(t *testing.T) {
1616
tmpDir := t.TempDir()
1717
d, err := GenerateDataStruct(ExtensionsConfig{
18-
Input: filepath.Join("testdata", "collections", "test.json"),
19-
Template: filepath.Join("templates", "entities.go.tmpl"),
20-
Output: filepath.Join(tmpDir, "test.go"),
18+
Input: filepath.Join("testdata", "collections", "test.json"),
19+
Template: filepath.Join("templates", "entities.go.tmpl"),
20+
Output: filepath.Join(tmpDir, "test.go"),
21+
ClientPackagePath: filepath.Join("..", "..", "client"),
2122
})
2223
require.NoError(t, err)
2324

@@ -41,9 +42,10 @@ func TestCollectionExtensions(t *testing.T) {
4142
t.Run("Test Successful Generation (with all redacts)", func(t *testing.T) {
4243
tmpDir := t.TempDir()
4344
d, err := GenerateDataStruct(ExtensionsConfig{
44-
Input: filepath.Join("testdata", "collections", "test-all-redacted.json"),
45-
Template: filepath.Join("templates", "entities.go.tmpl"),
46-
Output: filepath.Join(tmpDir, "test.go"),
45+
Input: filepath.Join("testdata", "collections", "test-all-redacted.json"),
46+
Template: filepath.Join("templates", "entities.go.tmpl"),
47+
Output: filepath.Join(tmpDir, "test.go"),
48+
ClientPackagePath: filepath.Join("..", "..", "client"),
4749
})
4850
require.NoError(t, err)
4951

@@ -67,9 +69,10 @@ func TestCollectionExtensions(t *testing.T) {
6769
t.Run("Test Successful Generation (with some redacts)", func(t *testing.T) {
6870
tmpDir := t.TempDir()
6971
d, err := GenerateDataStruct(ExtensionsConfig{
70-
Input: filepath.Join("testdata", "collections", "test-some-redacted.json"),
71-
Template: filepath.Join("templates", "entities.go.tmpl"),
72-
Output: filepath.Join(tmpDir, "test.go"),
72+
Input: filepath.Join("testdata", "collections", "test-some-redacted.json"),
73+
Template: filepath.Join("templates", "entities.go.tmpl"),
74+
Output: filepath.Join(tmpDir, "test.go"),
75+
ClientPackagePath: filepath.Join("..", "..", "client"),
7376
})
7477
require.NoError(t, err)
7578

generator/codegen/common.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Data struct {
4343
TemplatePath string
4444
DestinationPath string
4545
ClientPackagePath string
46+
PackageName string
4647
}
4748

4849
var ValidGenerationTargets = map[string]func(*Data) error{
@@ -82,12 +83,38 @@ func (cfg *ExtensionsConfig) Validate() error {
8283
)
8384
}
8485

86+
func getPackageNameFromPath(clientPath string) (clientName string, err error) {
87+
if clientPath == "" {
88+
err = commonerrors.Newf(commonerrors.ErrUndefined, "missing client package path, provide a client package path using '-c' or '--client_path'")
89+
return
90+
}
91+
isDir, err := filesystem.IsDir(clientPath)
92+
if err != nil {
93+
return
94+
}
95+
96+
if !isDir {
97+
err = commonerrors.Newf(commonerrors.ErrInvalid, "client path '%s' is not a directory", clientPath)
98+
return
99+
}
100+
101+
clientName = filepath.Base(clientPath)
102+
103+
return
104+
}
105+
85106
func GenerateDataStruct(cfg ExtensionsConfig) (d *Data, err error) {
107+
clientName, err := getPackageNameFromPath(cfg.ClientPackagePath)
108+
if err != nil {
109+
return
110+
}
111+
86112
return &Data{
87113
DestinationPath: cfg.Output,
88114
SpecPath: cfg.Input,
89115
TemplatePath: cfg.Template,
90116
ClientPackagePath: cfg.ClientPackagePath,
117+
PackageName: clientName,
91118
}, nil
92119
}
93120

@@ -135,7 +162,7 @@ func generateSourceCode(ctx context.Context, data *Data, template *template.Temp
135162
}
136163

137164
var tempBuffer bytes.Buffer
138-
err = template.Execute(&tempBuffer, data.Params)
165+
err = template.Execute(&tempBuffer, data)
139166
if err != nil {
140167
return
141168
}
@@ -200,7 +227,7 @@ func isExtensionFlagSet(props openapi3.ExtensionProps, flagKey string) (isSet bo
200227
return
201228
}
202229

203-
func CopyStaticFiles(ctx context.Context, destination string) error {
230+
func CopyStaticFiles(ctx context.Context, clientName string, destination string) (err error) {
204231
efs, fsErr := filesystem.NewEmbedFileSystem(&static)
205232
if fsErr != nil {
206233
return commonerrors.Newf(commonerrors.ErrUnexpected, "failed to create a filesystem for directory `%s`: %s", destination, fsErr.Error())
@@ -216,12 +243,27 @@ func CopyStaticFiles(ctx context.Context, destination string) error {
216243
return commonerrors.Newf(commonerrors.ErrUnexpected, "could not create directory `%s`: %s", destination, mkdirErr.Error())
217244
}
218245

219-
sfs := filesystem.NewStandardFileSystem()
220246
for _, f := range files {
247+
t, tmplErr := template.
248+
New(filesystem.FilePathBase(efs, f)).
249+
ParseFS(static, f)
250+
if tmplErr != nil {
251+
return tmplErr
252+
}
253+
221254
resultFileName := strings.TrimPrefix(f, "static/") // The embedded filesystem always uses Unix-style paths, regardless of the target platform
222255
resultFileName = strings.TrimSuffix(resultFileName, ".static")
223-
filesystem.CopyBetweenFS(ctx, efs, f, sfs, filepath.Join(destination, resultFileName))
256+
257+
d := Data{
258+
DestinationPath: filepath.Join(destination, resultFileName),
259+
PackageName: clientName,
260+
}
261+
err = generateSourceCode(ctx, &d, t)
262+
if err != nil {
263+
return
264+
}
265+
224266
}
225267

226-
return nil
268+
return
227269
}

generator/codegen/jobitem_extensions_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ func TestJobItemsExtensions(t *testing.T) {
1414
t.Run("Test Successful Generation", func(t *testing.T) {
1515
tmpDir := t.TempDir()
1616
d, err := GenerateDataStruct(ExtensionsConfig{
17-
Input: filepath.Join("testdata", "jobs", "test.json"),
18-
Template: filepath.Join("templates", "jobs.go.tmpl"),
19-
Output: filepath.Join(tmpDir, "test.go"),
17+
Input: filepath.Join("testdata", "jobs", "test.json"),
18+
Template: filepath.Join("templates", "jobs.go.tmpl"),
19+
Output: filepath.Join(tmpDir, "test.go"),
20+
ClientPackagePath: filepath.Join("..", "..", "client"),
2021
})
2122
require.NoError(t, err)
2223

@@ -40,9 +41,10 @@ func TestJobItemsExtensions(t *testing.T) {
4041
t.Run("Test Successful Generation (with all redacts)", func(t *testing.T) {
4142
tmpDir := t.TempDir()
4243
d, err := GenerateDataStruct(ExtensionsConfig{
43-
Input: filepath.Join("testdata", "jobs", "test-all-redacted.json"),
44-
Template: filepath.Join("templates", "jobs.go.tmpl"),
45-
Output: filepath.Join(tmpDir, "test.go"),
44+
Input: filepath.Join("testdata", "jobs", "test-all-redacted.json"),
45+
Template: filepath.Join("templates", "jobs.go.tmpl"),
46+
Output: filepath.Join(tmpDir, "test.go"),
47+
ClientPackagePath: filepath.Join("..", "..", "client"),
4648
})
4749
require.NoError(t, err)
4850

@@ -66,9 +68,10 @@ func TestJobItemsExtensions(t *testing.T) {
6668
t.Run("Test Successful Generation (with some redacts)", func(t *testing.T) {
6769
tmpDir := t.TempDir()
6870
d, err := GenerateDataStruct(ExtensionsConfig{
69-
Input: filepath.Join("testdata", "jobs", "test-some-redacted.json"),
70-
Template: filepath.Join("templates", "jobs.go.tmpl"),
71-
Output: filepath.Join(tmpDir, "test.go"),
71+
Input: filepath.Join("testdata", "jobs", "test-some-redacted.json"),
72+
Template: filepath.Join("templates", "jobs.go.tmpl"),
73+
Output: filepath.Join(tmpDir, "test.go"),
74+
ClientPackagePath: filepath.Join("..", "..", "client"),
7275
})
7376
require.NoError(t, err)
7477

generator/codegen/static/extension_client.go.static

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
// Package client defines an HTTP client for communicating with the web services.
6+
// Package {{ .PackageName }} defines an HTTP client for communicating with the web services.
77
// It includes the definition of request/response types as well as provides helpers for calling specific helpers.
8-
package client
8+
package {{ .PackageName }}
99

1010
import (
1111
"context"

generator/codegen/static/extension_model_hal_link_data.go.static

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
// Package client defines an HTTP client for communicating with the web services.
6+
// Package {{ .PackageName }} defines an HTTP client for communicating with the web services.
77
// It includes the definition of request/response types as well as provides helpers for calling specific helpers.
8-
package client
8+
package {{ .PackageName }}
99

1010
import (
1111
"errors"

generator/codegen/static/extension_model_interface.go.static

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
// Package client defines an HTTP client for communicating with the web services.
6+
// Package {{ .PackageName }} defines an HTTP client for communicating with the web services.
77
// It includes the definition of request/response types as well as provides helpers for calling specific helpers.
8-
package client
8+
package {{ .PackageName }}
99

1010
import "github.com/ARM-software/golang-utils/utils/collection/pagination"
1111

generator/codegen/static/extension_test.go.static

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (C) 2020-2025 Arm Limited or its affiliates and Contributors. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
package client
5+
package {{ .PackageName }}
66

77
import (
88
"testing"

0 commit comments

Comments
 (0)