Skip to content

Commit 683eea6

Browse files
committed
fix: update aep-lib-go
updating aep-lib-go, which contains a few fixes and new features (such as being able to dereference http-based schema refs).
1 parent 9257807 commit 683eea6

File tree

7 files changed

+120
-79
lines changed

7 files changed

+120
-79
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.22.3
44

55
require (
66
github.com/BurntSushi/toml v1.4.0
7-
github.com/aep-dev/aep-lib-go v0.0.0-20250226051706-6eab5b746243
7+
github.com/aep-dev/aep-lib-go v0.0.0-20250506053130-6fd1d4e67239
88
github.com/spf13/cobra v1.8.1
99
github.com/stretchr/testify v1.9.0
1010
)
@@ -14,5 +14,6 @@ require (
1414
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1515
github.com/pmezard/go-difflib v1.0.0 // indirect
1616
github.com/spf13/pflag v1.0.5 // indirect
17+
golang.org/x/text v0.19.0 // indirect
1718
gopkg.in/yaml.v3 v3.0.1 // indirect
1819
)

go.sum

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
22
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
3-
github.com/aep-dev/aep-lib-go v0.0.0-20250226051706-6eab5b746243 h1:R0zh8mlKsE04B5MWcvs22xaeGzQrXWtRxqHemCh4jBY=
4-
github.com/aep-dev/aep-lib-go v0.0.0-20250226051706-6eab5b746243/go.mod h1:M+h1D6T2uIUPelmaEsJbjR6JhqKsTlPX3lxp25zQQsk=
3+
github.com/aep-dev/aep-lib-go v0.0.0-20250506053130-6fd1d4e67239 h1:X9ylZ4HkzEpBny2vAE9zLDtTXpoe1BHeqhnIpsZZfaA=
4+
github.com/aep-dev/aep-lib-go v0.0.0-20250506053130-6fd1d4e67239/go.mod h1:YWfg3gjAGpvwFJnEQl1JjBGd0evGBKobt5wU/QxV6pA=
55
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
66
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
77
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
9+
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
810
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
911
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
12+
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
13+
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
1014
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1115
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1216
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -16,7 +20,11 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
1620
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1721
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1822
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
23+
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
24+
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
1925
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2026
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
27+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
28+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
2129
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2230
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type API struct {
2525
func ReadConfigFromFile(file string) (*Config, error) {
2626
// Check if file exists first
2727
if _, err := os.Stat(file); os.IsNotExist(err) {
28-
slog.Warn("Config file does not exist, using default configuration", "file", file)
28+
slog.Debug("Config file does not exist, using default configuration", "file", file)
2929
return &Config{APIs: make(map[string]API)}, nil
3030
}
3131

internal/service/resource_definition.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ func ExecuteResourceCommand(r *api.Resource, args []string) (*http.Request, stri
1919
var parents []*string
2020

2121
i := 1
22-
for i < len(r.PatternElems)-1 {
23-
p := r.PatternElems[i]
22+
patternElems := r.PatternElems()
23+
for i < len(patternElems)-1 {
24+
p := patternElems[i]
2425
flagName := p[1 : len(p)-1]
2526
var flagValue string
2627
parents = append(parents, &flagValue)
@@ -31,9 +32,9 @@ func ExecuteResourceCommand(r *api.Resource, args []string) (*http.Request, stri
3132

3233
withPrefix := func(path string) string {
3334
pElems := []string{}
34-
for i, p := range r.PatternElems {
35+
for i, p := range patternElems {
3536
// last element, we assume this was handled by the caller.
36-
if i == len(r.PatternElems)-1 {
37+
if i == len(patternElems)-1 {
3738
continue
3839
}
3940
if i%2 == 0 {
@@ -46,10 +47,10 @@ func ExecuteResourceCommand(r *api.Resource, args []string) (*http.Request, stri
4647
return fmt.Sprintf("%s%s", prefix, path)
4748
}
4849

49-
if r.CreateMethod != nil {
50+
if r.Methods.Create != nil {
5051
use := "create [id]"
5152
args := cobra.ExactArgs(1)
52-
if !r.CreateMethod.SupportsUserSettableCreate {
53+
if !r.Methods.Create.SupportsUserSettableCreate {
5354
use = "create"
5455
args = cobra.ExactArgs(0)
5556
}
@@ -60,7 +61,7 @@ func ExecuteResourceCommand(r *api.Resource, args []string) (*http.Request, stri
6061
Args: args,
6162
Run: func(cmd *cobra.Command, args []string) {
6263
p := withPrefix("")
63-
if r.CreateMethod.SupportsUserSettableCreate {
64+
if r.Methods.Create.SupportsUserSettableCreate {
6465
id := args[0]
6566
p = withPrefix(fmt.Sprintf("?id=%s", id))
6667
}
@@ -78,7 +79,7 @@ func ExecuteResourceCommand(r *api.Resource, args []string) (*http.Request, stri
7879
c.AddCommand(createCmd)
7980
}
8081

81-
if r.GetMethod != nil {
82+
if r.Methods.Get != nil {
8283
getCmd := &cobra.Command{
8384
Use: "get [id]",
8485
Short: fmt.Sprintf("Get a %v", strings.ToLower(r.Singular)),
@@ -92,7 +93,7 @@ func ExecuteResourceCommand(r *api.Resource, args []string) (*http.Request, stri
9293
c.AddCommand(getCmd)
9394
}
9495

95-
if r.UpdateMethod != nil {
96+
if r.Methods.Update != nil {
9697

9798
updateArgs := map[string]interface{}{}
9899
updateCmd := &cobra.Command{
@@ -116,7 +117,7 @@ func ExecuteResourceCommand(r *api.Resource, args []string) (*http.Request, stri
116117
c.AddCommand(updateCmd)
117118
}
118119

119-
if r.DeleteMethod != nil {
120+
if r.Methods.Delete != nil {
120121

121122
deleteCmd := &cobra.Command{
122123
Use: "delete [id]",
@@ -131,7 +132,7 @@ func ExecuteResourceCommand(r *api.Resource, args []string) (*http.Request, stri
131132
c.AddCommand(deleteCmd)
132133
}
133134

134-
if r.ListMethod != nil {
135+
if r.Methods.List != nil {
135136

136137
listCmd := &cobra.Command{
137138
Use: "list",

internal/service/resource_definition_test.go

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,91 @@ import (
88
"github.com/aep-dev/aep-lib-go/pkg/openapi"
99
)
1010

11-
var projectResource = api.Resource{
12-
Singular: "project",
13-
Plural: "projects",
14-
PatternElems: []string{"projects", "{project}"},
15-
Parents: []*api.Resource{},
16-
Schema: &openapi.Schema{
17-
Properties: map[string]openapi.Schema{
18-
"name": {
19-
Type: "string",
20-
},
21-
"description": {
22-
Type: "string",
11+
func getTestAPI() *api.API {
12+
projectResource := api.Resource{
13+
Singular: "project",
14+
Plural: "projects",
15+
Parents: []string{},
16+
Schema: &openapi.Schema{
17+
Properties: map[string]openapi.Schema{
18+
"name": {
19+
Type: "string",
20+
},
21+
"description": {
22+
Type: "string",
23+
},
24+
"active": {
25+
Type: "boolean",
26+
},
27+
"tags": {
28+
Type: "array",
29+
Items: &openapi.Schema{
30+
Type: "string",
31+
},
32+
},
33+
"metadata": {
34+
Type: "object",
35+
},
36+
"priority": {
37+
Type: "integer",
38+
},
2339
},
24-
"active": {
25-
Type: "boolean",
40+
Required: []string{"name"},
41+
},
42+
Methods: api.Methods{
43+
Get: &api.GetMethod{},
44+
List: &api.ListMethod{},
45+
Create: &api.CreateMethod{
46+
SupportsUserSettableCreate: true,
2647
},
27-
"tags": {
28-
Type: "array",
29-
Items: &openapi.Schema{
30-
Type: "string",
48+
Update: &api.UpdateMethod{},
49+
Delete: &api.DeleteMethod{},
50+
},
51+
}
52+
53+
a := &api.API{
54+
Name: "test",
55+
ServerURL: "https://api.example.com",
56+
Resources: map[string]*api.Resource{
57+
"project": &projectResource,
58+
"dataset": &api.Resource{
59+
Singular: "dataset",
60+
Plural: "datasets",
61+
Parents: []string{"project"},
62+
Schema: &openapi.Schema{},
63+
Methods: api.Methods{
64+
Get: &api.GetMethod{},
65+
List: &api.ListMethod{},
66+
Create: &api.CreateMethod{},
67+
Update: &api.UpdateMethod{},
68+
Delete: &api.DeleteMethod{},
3169
},
3270
},
33-
"metadata": {
34-
Type: "object",
71+
"user": &api.Resource{
72+
Singular: "user",
73+
Plural: "users",
74+
Parents: []string{},
75+
Schema: &openapi.Schema{},
3576
},
36-
"priority": {
37-
Type: "integer",
77+
"comment": &api.Resource{
78+
Singular: "comment",
79+
Plural: "comments",
80+
Parents: []string{},
81+
Schema: &openapi.Schema{},
3882
},
3983
},
40-
Required: []string{"name"},
41-
},
42-
GetMethod: &api.GetMethod{},
43-
ListMethod: &api.ListMethod{},
44-
CreateMethod: &api.CreateMethod{
45-
SupportsUserSettableCreate: true,
46-
},
47-
UpdateMethod: &api.UpdateMethod{},
48-
DeleteMethod: &api.DeleteMethod{},
84+
}
85+
err := api.AddImplicitFieldsAndValidate(a)
86+
if err != nil {
87+
panic(err)
88+
}
89+
return a
4990
}
5091

5192
func TestExecuteCommand(t *testing.T) {
5293
tests := []struct {
5394
name string
54-
resource api.Resource
95+
resource string
5596
args []string
5697
expectedQuery string
5798
expectedPath string
@@ -61,7 +102,7 @@ func TestExecuteCommand(t *testing.T) {
61102
}{
62103
{
63104
name: "simple resource no parents",
64-
resource: projectResource,
105+
resource: "project",
65106
args: []string{"list"},
66107
expectedPath: "projects",
67108
expectedMethod: "GET",
@@ -70,7 +111,7 @@ func TestExecuteCommand(t *testing.T) {
70111
},
71112
{
72113
name: "create with tags",
73-
resource: projectResource,
114+
resource: "project",
74115
args: []string{"create", "myproject", "--name=test-project", "--tags=tag1,tag2,tag3"},
75116
expectedPath: "projects",
76117
expectedMethod: "POST",
@@ -80,7 +121,7 @@ func TestExecuteCommand(t *testing.T) {
80121
},
81122
{
82123
name: "create with tags quoted",
83-
resource: projectResource,
124+
resource: "project",
84125
args: []string{"create", "myproject", "--name=test-project", "--tags=\"tag1,\",tag2,tag3"},
85126
expectedPath: "projects",
86127
expectedMethod: "POST",
@@ -89,19 +130,8 @@ func TestExecuteCommand(t *testing.T) {
89130
body: `{"name":"test-project","tags":["tag1,","tag2","tag3"]}`,
90131
},
91132
{
92-
name: "resource with parent",
93-
resource: api.Resource{
94-
Singular: "dataset",
95-
Plural: "datasets",
96-
PatternElems: []string{"projects", "{project}", "datasets", "{dataset}"},
97-
Parents: []*api.Resource{&projectResource},
98-
Schema: &openapi.Schema{},
99-
GetMethod: &api.GetMethod{},
100-
ListMethod: &api.ListMethod{},
101-
CreateMethod: &api.CreateMethod{},
102-
UpdateMethod: &api.UpdateMethod{},
103-
DeleteMethod: &api.DeleteMethod{},
104-
},
133+
name: "resource with parent",
134+
resource: "dataset",
105135
args: []string{"--project=foo", "get", "abc"},
106136
expectedPath: "projects/foo/datasets/abc",
107137
expectedMethod: "GET",
@@ -112,7 +142,8 @@ func TestExecuteCommand(t *testing.T) {
112142

113143
for _, tt := range tests {
114144
t.Run(tt.name, func(t *testing.T) {
115-
req, _, err := ExecuteResourceCommand(&tt.resource, tt.args)
145+
a := getTestAPI()
146+
req, _, err := ExecuteResourceCommand(a.Resources[tt.resource], tt.args)
116147
if (err != nil) != tt.wantErr {
117148
t.Errorf("ExecuteCommand() error = %v, wantErr %v", err, tt.wantErr)
118149
return

internal/service/service.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,21 @@ func (s *ServiceCommand) Execute(args []string) (string, error) {
5757
if err != nil {
5858
return "", fmt.Errorf("unable to execute request: %v", err)
5959
}
60-
return strings.Join([]string{output, reqOutput}, "\n"), nil
60+
outputs := []string{}
61+
for _, o := range []string{output, reqOutput} {
62+
if o != "" {
63+
outputs = append(outputs, o)
64+
}
65+
}
66+
return strings.Join(outputs, "\n"), nil
6167
}
6268

6369
func (s *ServiceCommand) doRequest(r *http.Request) (string, error) {
64-
r.Header.Set("Content-Type", "application/json")
70+
contentType := "application/json"
71+
if r.Method == http.MethodPatch {
72+
contentType = "application/merge-patch+json"
73+
}
74+
r.Header.Set("Content-Type", contentType)
6575
for k, v := range s.Headers {
6676
r.Header.Set(k, v)
6777
}

internal/service/service_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,11 @@ package service
33
import (
44
"strings"
55
"testing"
6-
7-
"github.com/aep-dev/aep-lib-go/pkg/api"
86
)
97

108
func TestService_ExecuteCommand_ListResources(t *testing.T) {
119
// Test setup
12-
svc := NewServiceCommand(&api.API{
13-
ServerURL: "http://test.com",
14-
Resources: map[string]*api.Resource{
15-
"project": &projectResource,
16-
"user": {},
17-
"post": {},
18-
"comment": {},
19-
},
20-
}, nil, false, false)
10+
svc := NewServiceCommand(getTestAPI(), nil, false, false)
2111

2212
tests := []struct {
2313
name string
@@ -28,12 +18,12 @@ func TestService_ExecuteCommand_ListResources(t *testing.T) {
2818
{
2919
name: "no arguments",
3020
args: []string{},
31-
expected: "Available resources:\n - comment\n - post\n - project\n - user\n",
21+
expected: "Available resources:\n - comment\n - dataset\n - project\n - user\n",
3222
},
3323
{
3424
name: "help flag",
3525
args: []string{"--help"},
36-
expected: "Available resources:\n - comment\n - post\n - project\n - user\n",
26+
expected: "Available resources:\n - comment\n - dataset\n - project\n - user\n",
3727
},
3828
{
3929
name: "unknown resource",

0 commit comments

Comments
 (0)