Skip to content

Commit 23cbfa8

Browse files
Added necessary pointers in struct
1 parent de0d620 commit 23cbfa8

File tree

3 files changed

+90
-66
lines changed

3 files changed

+90
-66
lines changed

v2/arangodb/tasks.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ type ClientTasks interface {
3232
Tasks(ctx context.Context, databaseName string) ([]Task, error)
3333

3434
// CreateTask creates a new task with the specified options.
35-
CreateTask(ctx context.Context, databaseName string, options *TaskOptions) (Task, error)
35+
CreateTask(ctx context.Context, databaseName string, options TaskOptions) (Task, error)
3636

3737
// If a task with the given ID already exists, a Conflict error is returned.
38-
CreateTaskWithID(ctx context.Context, databaseName string, id string, options *TaskOptions) (Task, error)
38+
CreateTaskWithID(ctx context.Context, databaseName string, id string, options TaskOptions) (Task, error)
3939

4040
// RemoveTask deletes an existing task by its ID.
4141
RemoveTask(ctx context.Context, databaseName string, id string) error
@@ -44,41 +44,41 @@ type ClientTasks interface {
4444
// TaskOptions contains options for creating a new task.
4545
type TaskOptions struct {
4646
// ID is an optional identifier for the task.
47-
ID string `json:"id,omitempty"`
47+
ID *string `json:"id,omitempty"`
4848
// Name is an optional name for the task.
49-
Name string `json:"name,omitempty"`
49+
Name *string `json:"name,omitempty"`
5050

5151
// Command is the JavaScript code to be executed.
52-
Command string `json:"command"`
52+
Command *string `json:"command"`
5353

5454
// Params are optional parameters passed to the command.
5555
Params interface{} `json:"params,omitempty"`
5656

5757
// Period is the interval (in seconds) at which the task runs periodically.
5858
// If zero, the task runs once after the offset.
59-
Period int64 `json:"period,omitempty"`
59+
Period *int64 `json:"period,omitempty"`
6060

6161
// Offset is the delay (in milliseconds) before the task is first executed.
62-
Offset float64 `json:"offset,omitempty"`
62+
Offset *float64 `json:"offset,omitempty"`
6363
}
6464

6565
// Task provides access to a single task on the server.
6666
type Task interface {
6767
// ID returns the ID of the task.
68-
ID() string
68+
ID() *string
6969

7070
// Name returns the name of the task.
71-
Name() string
71+
Name() *string
7272

7373
// Command returns the JavaScript code of the task.
74-
Command() string
74+
Command() *string
7575

7676
// Params returns the parameters of the task.
7777
Params(result interface{}) error
7878

7979
// Period returns the period (in seconds) of the task.
80-
Period() int64
80+
Period() *int64
8181

8282
// Offset returns the offset (in milliseconds) of the task.
83-
Offset() float64
83+
Offset() *float64
8484
}

v2/arangodb/tasks_impl.go

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ type task struct {
7878
}
7979

8080
// Task interface implementation for the task struct.
81-
func (t *task) ID() string {
82-
return t.id
81+
func (t *task) ID() *string {
82+
return &t.id
8383
}
8484

85-
func (t *task) Name() string {
86-
return t.name
85+
func (t *task) Name() *string {
86+
return &t.name
8787
}
8888

89-
func (t *task) Command() string {
90-
return t.command
89+
func (t *task) Command() *string {
90+
return &t.command
9191
}
9292

9393
func (t *task) Params(result interface{}) error {
@@ -97,17 +97,17 @@ func (t *task) Params(result interface{}) error {
9797
return json.Unmarshal(t.params, result)
9898
}
9999

100-
func (t *task) Period() int64 {
101-
return t.period
100+
func (t *task) Period() *int64 {
101+
return &t.period
102102
}
103103

104-
func (t *task) Offset() float64 {
105-
return t.offset
104+
func (t *task) Offset() *float64 {
105+
return &t.offset
106106
}
107107

108108
// Tasks retrieves all tasks from the specified database.
109109
// Retuns a slice of Task objects representing the tasks in the database.
110-
func (c clientTask) Tasks(ctx context.Context, databaseName string) ([]Task, error) {
110+
func (c *clientTask) Tasks(ctx context.Context, databaseName string) ([]Task, error) {
111111
urlEndpoint := connection.NewUrl("_db", url.PathEscape(databaseName), "_api", "tasks")
112112
response := make([]taskResponse, 0) // Direct array response
113113
resp, err := connection.CallGet(ctx, c.client.connection, urlEndpoint, &response)
@@ -131,7 +131,7 @@ func (c clientTask) Tasks(ctx context.Context, databaseName string) ([]Task, err
131131
// Task retrieves a specific task by its ID from the specified database.
132132
// If the task does not exist, it returns an error.
133133
// If the task exists, it returns a Task object.
134-
func (c clientTask) Task(ctx context.Context, databaseName string, id string) (Task, error) {
134+
func (c *clientTask) Task(ctx context.Context, databaseName string, id string) (Task, error) {
135135
urlEndpoint := connection.NewUrl("_db", url.PathEscape(databaseName), "_api", "tasks", url.PathEscape(id))
136136
response := struct {
137137
taskResponse `json:",inline"`
@@ -155,7 +155,7 @@ func validateTaskOptions(options *TaskOptions) error {
155155
if options == nil {
156156
return errors.New("TaskOptions must not be nil")
157157
}
158-
if options.Command == "" {
158+
if options.Command == nil {
159159
return errors.New("TaskOptions.Command must not be empty")
160160
}
161161
return nil
@@ -166,13 +166,13 @@ func validateTaskOptions(options *TaskOptions) error {
166166
// If the task does not exist, it will create a new task.
167167
// The options parameter contains the task configuration such as name, command, parameters, period, and offset.
168168
// The ID field in options is optional; if provided, it will be used as the task identifier.
169-
func (c clientTask) CreateTask(ctx context.Context, databaseName string, options *TaskOptions) (Task, error) {
170-
if err := validateTaskOptions(options); err != nil {
169+
func (c *clientTask) CreateTask(ctx context.Context, databaseName string, options TaskOptions) (Task, error) {
170+
if err := validateTaskOptions(&options); err != nil {
171171
return nil, errors.WithStack(err)
172172
}
173173
var urlEndpoint string
174-
if options.ID != "" {
175-
urlEndpoint = connection.NewUrl("_db", url.PathEscape(databaseName), "_api", "tasks", url.PathEscape(options.ID))
174+
if options.ID != nil {
175+
urlEndpoint = connection.NewUrl("_db", url.PathEscape(databaseName), "_api", "tasks", url.PathEscape(*options.ID))
176176
} else {
177177
urlEndpoint = connection.NewUrl("_db", url.PathEscape(databaseName), "_api", "tasks")
178178
}
@@ -184,12 +184,22 @@ func (c clientTask) CreateTask(ctx context.Context, databaseName string, options
184184
Params json.RawMessage `json:"params,omitempty"`
185185
Period int64 `json:"period,omitempty"`
186186
Offset float64 `json:"offset,omitempty"`
187-
}{
188-
ID: options.ID,
189-
Name: options.Name,
190-
Command: options.Command,
191-
Period: options.Period,
192-
Offset: options.Offset,
187+
}{}
188+
189+
if options.ID != nil {
190+
createRequest.ID = *options.ID
191+
}
192+
if options.Name != nil {
193+
createRequest.Name = *options.Name
194+
}
195+
if options.Command != nil {
196+
createRequest.Command = *options.Command
197+
}
198+
if options.Period != nil {
199+
createRequest.Period = *options.Period
200+
}
201+
if options.Offset != nil {
202+
createRequest.Offset = *options.Offset
193203
}
194204

195205
if options.Params != nil {
@@ -227,7 +237,7 @@ func (c clientTask) CreateTask(ctx context.Context, databaseName string, options
227237
// The ID parameter is the identifier of the task to be removed.
228238
// The databaseName parameter specifies the database from which the task should be removed.
229239
// It constructs the URL endpoint for the task API and calls the DELETE method to remove the task
230-
func (c clientTask) RemoveTask(ctx context.Context, databaseName string, id string) error {
240+
func (c *clientTask) RemoveTask(ctx context.Context, databaseName string, id string) error {
231241
urlEndpoint := connection.NewUrl("_db", url.PathEscape(databaseName), "_api", "tasks", url.PathEscape(id))
232242

233243
resp, err := connection.CallDelete(ctx, c.client.connection, urlEndpoint, nil)
@@ -246,7 +256,7 @@ func (c clientTask) RemoveTask(ctx context.Context, databaseName string, id stri
246256
// CreateTaskWithID creates a new task with the specified ID and options.
247257
// If a task with the given ID already exists, it returns a Conflict error.
248258
// If the task does not exist, it creates a new task with the provided options.
249-
func (c clientTask) CreateTaskWithID(ctx context.Context, databaseName string, id string, options *TaskOptions) (Task, error) {
259+
func (c *clientTask) CreateTaskWithID(ctx context.Context, databaseName string, id string, options TaskOptions) (Task, error) {
250260
// Check if task already exists
251261
existingTask, err := c.Task(ctx, databaseName, id)
252262
if err == nil && existingTask != nil {
@@ -257,6 +267,6 @@ func (c clientTask) CreateTaskWithID(ctx context.Context, databaseName string, i
257267
}
258268

259269
// Set the ID and call CreateTask
260-
options.ID = id
270+
options.ID = &id
261271
return c.CreateTask(ctx, databaseName, options)
262272
}

v2/tests/tasks_test.go

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,26 @@ func Test_CreateNewTask(t *testing.T) {
3939
dbName := "_system"
4040
testCases := map[string]*arangodb.TaskOptions{
4141
"taskWIthParams": {
42-
Name: "taskWIthParams",
43-
Command: "(function(params) { require('@arangodb').print(params); })(params)",
44-
Period: 2,
42+
Name: utils.NewType("taskWIthParams"),
43+
Command: utils.NewType("(function(params) { require('@arangodb').print(params); })(params)"),
44+
Period: utils.NewType(int64(2)),
4545
Params: map[string]interface{}{
4646
"test": "hello",
4747
},
4848
},
4949
"taskWIthOutParams": {
50-
Name: "taskWIthOutParams",
51-
Command: "(function() { require('@arangodb').print('Hello'); })()",
52-
Period: 2,
50+
Name: utils.NewType("taskWIthOutParams"),
51+
Command: utils.NewType("(function() { require('@arangodb').print('Hello'); })()"),
52+
Period: utils.NewType(int64(2)),
5353
},
5454
}
5555

5656
for name, options := range testCases {
5757
withContextT(t, defaultTestTimeout, func(ctx context.Context, tb testing.TB) {
58-
createdTask, err := client.CreateTask(ctx, dbName, options)
58+
createdTask, err := client.CreateTask(ctx, dbName, *options)
5959
require.NoError(t, err)
6060
require.NotNil(t, createdTask)
61-
require.Equal(t, name, createdTask.Name())
61+
require.Equal(t, name, *createdTask.Name())
6262
t.Logf("Params: %v", options.Params)
6363
// Proper params comparison
6464
// Check parameters
@@ -76,20 +76,27 @@ func Test_CreateNewTask(t *testing.T) {
7676
}
7777
}
7878

79-
taskInfo, err := client.Task(ctx, dbName, createdTask.ID())
79+
taskInfo, err := client.Task(ctx, dbName, *createdTask.ID())
8080
require.NoError(t, err)
8181
require.NotNil(t, taskInfo)
82-
require.Equal(t, name, taskInfo.Name())
82+
require.Equal(t, name, *taskInfo.Name())
8383

8484
tasks, err := client.Tasks(ctx, dbName)
8585
require.NoError(t, err)
8686
require.NotNil(t, tasks)
8787
require.Greater(t, len(tasks), 0, "Expected at least one task to be present")
8888
t.Logf("Found tasks: %v", tasks)
89-
t.Logf("Task Id to be removed: %s\n", tasks[0].ID())
90-
91-
require.NoError(t, client.RemoveTask(ctx, dbName, createdTask.ID()))
92-
t.Logf("Task %s removed successfully", createdTask.ID())
89+
if len(tasks) > 0 && tasks[0].ID() != nil {
90+
t.Logf("Task Id to be removed: %s\n", *tasks[0].ID())
91+
} else {
92+
t.Logf("Task Id to be removed: <nil>")
93+
}
94+
if id := createdTask.ID(); id != nil {
95+
require.NoError(t, client.RemoveTask(ctx, dbName, *id))
96+
t.Logf("Task %s removed successfully", *id)
97+
} else {
98+
t.Logf("Task ID is nil")
99+
}
93100
})
94101
}
95102
}, WrapOptions{
@@ -102,19 +109,26 @@ func Test_ValidationsForCreateNewTask(t *testing.T) {
102109
dbName := "_system"
103110
testCases := map[string]*arangodb.TaskOptions{
104111
"taskWIthOutCommand": {
105-
Name: "taskWIthOutCommand",
106-
Period: 2,
112+
Name: utils.NewType("taskWIthOutCommand"),
113+
Period: utils.NewType(int64(2)),
107114
},
108115
"taskWIthOutPeriod": nil,
109116
}
110117

111118
for name, options := range testCases {
112119
withContextT(t, defaultTestTimeout, func(ctx context.Context, tb testing.TB) {
113-
_, err := client.CreateTask(ctx, dbName, options)
120+
var err error
121+
if options == nil {
122+
_, err = client.CreateTask(ctx, dbName, arangodb.TaskOptions{})
123+
} else {
124+
_, err = client.CreateTask(ctx, dbName, *options)
125+
}
126+
114127
require.Error(t, err)
115128
t.Logf("Expected error for task '%s': %v", name, err)
116129
})
117130
}
131+
118132
}, WrapOptions{
119133
Parallel: utils.NewType(false),
120134
})
@@ -126,27 +140,27 @@ func Test_TaskCreationWithId(t *testing.T) {
126140
dbName := "_system"
127141
taskID := "test-task-id"
128142
options := &arangodb.TaskOptions{
129-
ID: taskID, // Optional if CreateTaskWithID sets it, but safe to keep
130-
Name: "TestTaskWithID",
131-
Command: "console.log('This is a test task with ID');",
132-
Period: 5,
143+
ID: &taskID, // Optional if CreateTaskWithID sets it, but safe to keep
144+
Name: utils.NewType("TestTaskWithID"),
145+
Command: utils.NewType("console.log('This is a test task with ID');"),
146+
Period: utils.NewType(int64(5)),
133147
}
134148

135149
// Create the task with explicit ID
136-
task, err := client.CreateTaskWithID(ctx, dbName, taskID, options)
150+
task, err := client.CreateTaskWithID(ctx, dbName, taskID, *options)
137151
require.NoError(t, err, "Expected task creation to succeed")
138152
require.NotNil(t, task, "Expected task to be non-nil")
139-
require.Equal(t, taskID, task.ID(), "Task ID mismatch")
140-
require.Equal(t, options.Name, task.Name(), "Task Name mismatch")
153+
require.Equal(t, taskID, *task.ID(), "Task ID mismatch")
154+
require.Equal(t, *options.Name, *task.Name(), "Task Name mismatch")
141155

142156
// Retrieve and validate
143157
retrievedTask, err := client.Task(ctx, dbName, taskID)
144158
require.NoError(t, err, "Expected task retrieval to succeed")
145159
require.NotNil(t, retrievedTask, "Expected retrieved task to be non-nil")
146-
require.Equal(t, taskID, retrievedTask.ID(), "Retrieved task ID mismatch")
147-
require.Equal(t, options.Name, retrievedTask.Name(), "Retrieved task Name mismatch")
148-
// Try to create task again with same ID — expect 429
149-
_, err = client.CreateTaskWithID(ctx, dbName, taskID, options)
160+
require.Equal(t, taskID, *retrievedTask.ID(), "Retrieved task ID mismatch")
161+
require.Equal(t, *options.Name, *retrievedTask.Name(), "Retrieved task Name mismatch")
162+
163+
_, err = client.CreateTaskWithID(ctx, dbName, taskID, *options)
150164
require.Error(t, err, "Creating a duplicate task should fail")
151165

152166
// Clean up

0 commit comments

Comments
 (0)