Skip to content

Commit 522e888

Browse files
Apply suggestions from code review
Co-authored-by: Mattias Sjödin <86059470+MattiasMTS@users.noreply.github.com>
1 parent 9f19f01 commit 522e888

1 file changed

Lines changed: 15 additions & 46 deletions

File tree

dbee/adapters/arango_driver.go

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,6 @@ func (a *arangoDriver) Close() {
5858
}
5959

6060
func (a *arangoDriver) Columns(opts *core.TableOptions) ([]*core.Column, error) {
61-
if a == nil || a.c == nil {
62-
return nil, errors.New("arangoDriver is not initialized")
63-
}
64-
if a.dbName == "" {
65-
return nil, errors.New("database not selected")
66-
}
67-
68-
log.Printf("Fetching columns for table: %s", opts.Table)
69-
7061
db, err := a.c.GetDatabase(context.Background(), a.dbName, nil)
7162
if err != nil {
7263
return nil, fmt.Errorf("failed to get database: %w", err)
@@ -82,37 +73,31 @@ func (a *arangoDriver) Columns(opts *core.TableOptions) ([]*core.Column, error)
8273
sort attribute
8374
RETURN {attribute}`
8475

85-
bindVars := map[string]interface{}{"@col": opts.Table}
76+
bindVars := map[string]any{"@col": opts.Table}
8677
cursor, err := db.Query(context.Background(), aql, &arangodb.QueryOptions{BindVars: bindVars})
8778
if err != nil {
8879
return nil, fmt.Errorf("failed to execute query: %w", err)
8980
}
9081
defer cursor.Close()
9182

92-
columns := []*core.Column{}
93-
var doc map[string]any
83+
columns := make([]*core.Column, 0)
84+
doc := make(map[string]any)
9485

9586
for cursor.HasMore() {
96-
_, err := cursor.ReadDocument(context.Background(), &doc)
97-
if err != nil {
87+
if _, err := cursor.ReadDocument(context.Background(), &doc); err != nil {
9888
return nil, fmt.Errorf("failed to read document: %w", err)
9989
}
100-
column := fmt.Sprintf("%s", doc["attribute"])
90+
column, ok := doc["attribute"].(string)
91+
if !ok {
92+
column = ""
93+
}
10194
columns = append(columns, &core.Column{Type: "collection", Name: column})
10295
}
10396

10497
return columns, nil
10598
}
10699

107100
func (a *arangoDriver) Query(ctx context.Context, query string) (core.ResultStream, error) {
108-
if a == nil || a.c == nil {
109-
return nil, errors.New("arangoDriver is not initialized")
110-
}
111-
if a.dbName == "" {
112-
return nil, errors.New("database not selected")
113-
}
114-
115-
log.Printf("Executing query: %s", query)
116101
db, err := a.c.GetDatabase(ctx, a.dbName, nil)
117102
if err != nil {
118103
return nil, fmt.Errorf("failed to get database: %w", err)
@@ -123,21 +108,12 @@ func (a *arangoDriver) Query(ctx context.Context, query string) (core.ResultStre
123108
return nil, fmt.Errorf("query execution failed: %w", err)
124109
}
125110
defer cursor.Close()
126-
next, hasNext := builders.NextNil()
127111

128-
next, hasNext = builders.NextYield(func(yield func(...any)) error {
129-
if !cursor.HasMore() {
130-
next, hasNext = builders.NextNil()
131-
}
112+
next, hasNext := builders.NextYield(func(yield func(...any)) error {
113+
for cursor.HasMore() {
114+
var doc any
132115

133-
for {
134-
if !cursor.HasMore() {
135-
break
136-
}
137-
var doc interface{}
138-
139-
_, err = cursor.ReadDocument(ctx, &doc)
140-
if err != nil {
116+
if _, err := cursor.ReadDocument(ctx, &doc); err != nil {
141117
return err
142118
}
143119

@@ -211,23 +187,16 @@ func (ar *arangoResponse) MarshalJSON() ([]byte, error) {
211187
}
212188

213189
func (ar *arangoResponse) GobEncode() ([]byte, error) {
214-
var err error
215190
w := new(bytes.Buffer)
216191
encoder := gob.NewEncoder(w)
217-
err = encoder.Encode(ar.Value)
218-
if err != nil {
192+
if err := encoder.Encode(ar.Value); err != nil {
219193
return nil, err
220194
}
221-
return w.Bytes(), err
195+
return w.Bytes(), nil
222196
}
223197

224198
func (ar *arangoResponse) GobDecode(buf []byte) error {
225-
var err error
226199
r := bytes.NewBuffer(buf)
227200
decoder := gob.NewDecoder(r)
228-
err = decoder.Decode(&ar.Value)
229-
if err != nil {
230-
return err
231-
}
232-
return err
201+
return decoder.Decode(&ar.Value)
233202
}

0 commit comments

Comments
 (0)