Skip to content

Commit d96efc7

Browse files
committed
chore: rewrite the generator
1 parent 156cc5b commit d96efc7

File tree

8 files changed

+318
-310
lines changed

8 files changed

+318
-310
lines changed

Makefile

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ check:
1717
golangci-lint run
1818

1919
## Useful to initiate structures
20-
gen-struct:
21-
echo 'package namesilo' > "gen_struct.go";
22-
echo '' >> "gen_struct.go";
23-
echo 'import "encoding/xml"' >> "gen_struct.go";
24-
echo '' >> "gen_struct.go";
25-
for i in $$(ls samples/ -1); do \
26-
zek -c -n $${i%.xml} "./samples/$$i" >> "gen_struct.go"; \
27-
done
20+
# TODO: must be updated to support the subfolders.
21+
#gen-struct:
22+
# # go install github.com/miku/zek/cmd/zek@latest
23+
# echo 'package namesilo' > "gen_struct.go";
24+
# echo '' >> "gen_struct.go";
25+
# echo 'import "encoding/xml"' >> "gen_struct.go";
26+
# echo '' >> "gen_struct.go";
27+
# for i in $$(ls samples/ -1); do \
28+
# zek -c -n $${i%.xml} "./samples/$$i" >> "gen_struct.go"; \
29+
# done
30+
31+
generate:
32+
go run ./internal/

gen/gen_test.go

Lines changed: 0 additions & 302 deletions
This file was deleted.

internal/client-methods.go.tmpl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Code generated by gen/gen_test.go DO NOT EDIT.
2+
3+
package namesilo
4+
5+
import (
6+
"context"
7+
"encoding/xml"
8+
"fmt"
9+
"io"
10+
"net/http"
11+
)
12+
{{range $key, $value := .Names }}
13+
// {{ $value.Upper }} Execute operation {{ $value.Lower }}.
14+
func (c *Client) {{ $value.Upper }}(ctx context.Context, params *{{ $value.Upper }}Params) (*{{ $value.Upper }}, error) {
15+
resp, err := c.get(ctx, "{{ $value.Lower }}", params)
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
defer func() { _ = resp.Body.Close() }()
21+
22+
if resp.StatusCode != http.StatusOK {
23+
return nil, fmt.Errorf("error: HTTP status code %v", resp.StatusCode)
24+
}
25+
26+
bytes, err := io.ReadAll(resp.Body)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
op := &{{ $value.Upper }}{}
32+
33+
err = xml.Unmarshal(bytes, op)
34+
if err != nil {
35+
return nil, fmt.Errorf("failed to decode: %w: %s", err, bytes)
36+
}
37+
38+
switch op.Reply.Code {
39+
case SuccessfulAPIOperation:
40+
// Successful API operation
41+
return op, nil
42+
case SuccessfulRegistration:
43+
// Successful registration, but not all provided hosts were valid resulting in our nameservers being used
44+
return op, nil
45+
case SuccessfulOrder:
46+
// Successful order, but there was an error with the contact information provided so your account default contact profile was used
47+
return op, nil
48+
default:
49+
// error
50+
return op, fmt.Errorf("code: %s, details: %s", op.Reply.Code, op.Reply.Detail)
51+
}
52+
}
53+
{{end}}

0 commit comments

Comments
 (0)