From e99645cf93c14e0fd93571c7ab2efdebc50b3aef Mon Sep 17 00:00:00 2001 From: Mateusz Filipowicz Date: Sun, 9 Feb 2025 02:07:44 +0100 Subject: [PATCH] fix: renamed generator template to use ErrNotFound instead of NotFoundError --- README.md | 16 ++++++++-------- codegen/api.go.tmpl | 6 +++--- codegen/generator.go | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index be2c327..4dc486b 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,8 @@ as well as dedicated user restricted to local access only. ```go c, err := unifi.NewClient(&unifi.ClientConfig{ - BaseURL: "https://unifi.localdomain", - APIKey: "your-api-key", + BaseURL: "https://unifi.localdomain", + APIKey: "your-api-key", }) ``` @@ -106,12 +106,12 @@ func (l *LoggingInterceptor) InterceptRequest(req *http.Request) error { } func (l *LoggingInterceptor) InterceptResponse(resp *http.Response) error { - log.Printf("Response status: %d", resp.StatusCode) + log.Printf("Response status: %d", resp.StatusCode) return nil } c, err := unifi.NewClient(&unifi.ClientConfig{ - ... + ... Interceptors: []unifi.ClientInterceptor{&LoggingInterceptor{}}, }) ``` @@ -126,10 +126,10 @@ network, err := c.ListNetwork(ctx, "site-name") Create user assigned to network: ```go user, err := c.CreateUser(ctx, "site-name", &unifi.User{ - Name: "My Network User", - MAC: "00:00:00:00:00:00", - NetworkID: network[0].ID, - IP: "10.0.21.37", + Name: "My Network User", + MAC: "00:00:00:00:00:00", + NetworkID: network[0].ID, + IP: "10.0.21.37", }) ``` diff --git a/codegen/api.go.tmpl b/codegen/api.go.tmpl index 6cac7f5..d964cb7 100644 --- a/codegen/api.go.tmpl +++ b/codegen/api.go.tmpl @@ -95,7 +95,7 @@ func (c *Client) get{{ .StructName }}(ctx context.Context, site{{ if not .IsSett } if len(respBody.Data) != 1 { - return nil, NotFoundError + return nil, ErrNotFound } d := respBody.Data[0] @@ -123,7 +123,7 @@ func (c *Client) create{{ .StructName }}(ctx context.Context, site string, d *{{ } if len(respBody.Data) != 1 { - return nil, NotFoundError + return nil, ErrNotFound } new := respBody.Data[0] @@ -148,7 +148,7 @@ func (c *Client) update{{ .StructName }}(ctx context.Context, site string, d *{{ } if len(respBody.Data) != 1 { - return nil, NotFoundError + return nil, ErrNotFound } new := respBody.Data[0] diff --git a/codegen/generator.go b/codegen/generator.go index d7a0176..6d380df 100644 --- a/codegen/generator.go +++ b/codegen/generator.go @@ -368,7 +368,6 @@ func generateCode(fieldsDir string, outDir string) error { urlPath := strings.ToLower(name) structName := cleanName(name, fileReps) - goFile := strcase.ToSnake(structName) + ".generated.go" fieldsFilePath := filepath.Join(fieldsDir, fieldsFile.Name()) b, err := os.ReadFile(fieldsFilePath) if err != nil { @@ -504,9 +503,10 @@ func generateCode(fieldsDir string, outDir string) error { continue } + goFile := strcase.ToSnake(structName) + ".generated.go" goFilePath := filepath.Join(outDir, goFile) _ = os.Remove(goFilePath) - if err := os.WriteFile(goFile, ([]byte)(code), 0o644); err != nil { + if err := os.WriteFile(goFilePath, ([]byte)(code), 0o644); err != nil { log.Errorf("failed to write file %s: %s", goFile, err) continue }