This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.go
72 lines (65 loc) · 1.74 KB
/
dataset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package jexiasdkgo
import (
"fmt"
"sync"
)
// Dataset a struct containing the name of the dataset and the client memory pointer
// As the client is a memory pointer, any changes made to the client will be reflected within the dataset, therefore token refreshes will still work
type Dataset struct {
Name string
Client *Client
mux sync.Mutex
}
// GetDataset returns a dataset instance that can be used to perform actions against
func (c *Client) GetDataset(name string) *Dataset {
return &Dataset{
Name: name,
Client: c,
}
}
// GetName fetches the dataset name
func (d *Dataset) GetName() string {
d.mux.Lock()
name := d.Name
d.mux.Unlock()
return name
}
// GetClient fetches the current client
func (d *Dataset) GetClient() *Client {
d.mux.Lock()
client := d.Client
d.mux.Unlock()
return client
}
// Insert allows you to add sets of data to the dataset
// If you are inputing a single interface you must wrap it in an array: []interface{}{yourStruct}
func (d *Dataset) Insert(dataArray []interface{}) ([]interface{}, error) {
var result []interface{}
payload, err := marshal(dataArray)
if err != nil {
return nil, err
}
err = d.GetClient().post(
fmt.Sprintf("%v/ds/%v", d.GetClient().projectURL, d.GetName()),
&result,
addToken(d.GetClient().GetToken().Access),
setBody(payload),
)
if err != nil {
return nil, err
}
return result, nil
}
// Select allows you to select the data from a dataset
// You should pass an array of types you are expecting to receive: *[]interface{}
func (d *Dataset) Select(target interface{}) error {
err := d.GetClient().get(
fmt.Sprintf("%v/ds/%v", d.GetClient().projectURL, d.GetName()),
&target,
addToken(d.GetClient().GetToken().Access),
)
if err != nil {
return err
}
return nil
}