Skip to content

Commit 28677e9

Browse files
authored
Fix tests and golint warnings (hypermodeinc#79)
1 parent 8c33afc commit 28677e9

File tree

6 files changed

+147
-128
lines changed

6 files changed

+147
-128
lines changed

client.go

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,18 @@ import (
2929
"google.golang.org/grpc/status"
3030
)
3131

32-
// Dgraph is a transaction aware client to a set of dgraph server instances.
32+
// Dgraph is a transaction aware client to a set of Dgraph server instances.
3333
type Dgraph struct {
3434
jwtMutex sync.RWMutex
3535
jwt api.Jwt
3636
dc []api.DgraphClient
3737
}
3838

39-
// NewDgraphClient creates a new Dgraph for interacting with the Dgraph store connected to in
40-
// conns.
41-
// The client can be backed by multiple connections (to the same server, or multiple servers in a
42-
// cluster).
39+
// NewDgraphClient creates a new Dgraph (client) for interacting with Alphas.
40+
// The client is backed by multiple connections to the same or different
41+
// servers in a cluster.
4342
//
44-
// A single client is thread safe for sharing with multiple go routines.
43+
// A single Dgraph (client) is thread safe for sharing with multiple goroutines.
4544
func NewDgraphClient(clients ...api.DgraphClient) *Dgraph {
4645
dg := &Dgraph{
4746
dc: clients,
@@ -50,6 +49,8 @@ func NewDgraphClient(clients ...api.DgraphClient) *Dgraph {
5049
return dg
5150
}
5251

52+
// Login logs in the current client using the provided credentials.
53+
// Valid for the duration the client is alive.
5354
func (d *Dgraph) Login(ctx context.Context, userid string, password string) error {
5455
d.jwtMutex.Lock()
5556
defer d.jwtMutex.Unlock()
@@ -67,6 +68,29 @@ func (d *Dgraph) Login(ctx context.Context, userid string, password string) erro
6768
return d.jwt.Unmarshal(resp.Json)
6869
}
6970

71+
// Alter can be used to do the following by setting various fields of api.Operation:
72+
// 1. Modify the schema.
73+
// 2. Drop a predicate.
74+
// 3. Drop the database.
75+
func (d *Dgraph) Alter(ctx context.Context, op *api.Operation) error {
76+
dc := d.anyClient()
77+
78+
ctx = d.getContext(ctx)
79+
_, err := dc.Alter(ctx, op)
80+
81+
if isJwtExpired(err) {
82+
err = d.retryLogin(ctx)
83+
if err != nil {
84+
return err
85+
}
86+
87+
ctx = d.getContext(ctx)
88+
_, err = dc.Alter(ctx, op)
89+
}
90+
91+
return err
92+
}
93+
7094
func (d *Dgraph) retryLogin(ctx context.Context) error {
7195
d.jwtMutex.Lock()
7296
defer d.jwtMutex.Unlock()
@@ -83,52 +107,29 @@ func (d *Dgraph) retryLogin(ctx context.Context) error {
83107
if err != nil {
84108
return err
85109
}
110+
86111
return d.jwt.Unmarshal(resp.Json)
87112
}
88113

89114
func (d *Dgraph) getContext(ctx context.Context) context.Context {
90115
d.jwtMutex.RLock()
91116
defer d.jwtMutex.RUnlock()
117+
92118
if len(d.jwt.AccessJwt) > 0 {
93119
md, ok := metadata.FromOutgoingContext(ctx)
94120
if !ok {
95121
// no metadata key is in the context, add one
96122
md = metadata.New(nil)
97123
}
124+
98125
md.Set("accessJwt", d.jwt.AccessJwt)
99126
return metadata.NewOutgoingContext(ctx, md)
100127
}
101128

102129
return ctx
103130
}
104131

105-
// By setting various fields of api.Operation, Alter can be used to do the
106-
// following:
107-
//
108-
// 1. Modify the schema.
109-
//
110-
// 2. Drop a predicate.
111-
//
112-
// 3. Drop the database.
113-
func (d *Dgraph) Alter(ctx context.Context, op *api.Operation) error {
114-
dc := d.anyClient()
115-
116-
ctx = d.getContext(ctx)
117-
_, err := dc.Alter(ctx, op)
118-
119-
if isJwtExpired(err) {
120-
err = d.retryLogin(ctx)
121-
if err != nil {
122-
return err
123-
}
124-
ctx = d.getContext(ctx)
125-
_, err = dc.Alter(ctx, op)
126-
}
127-
128-
return err
129-
}
130-
131-
// isJwtExpired returns true if the error indicates that the jwt has expired
132+
// isJwtExpired returns true if the error indicates that the jwt has expired.
132133
func isJwtExpired(err error) bool {
133134
if err == nil {
134135
return false
@@ -143,10 +144,10 @@ func (d *Dgraph) anyClient() api.DgraphClient {
143144
return d.dc[rand.Intn(len(d.dc))]
144145
}
145146

146-
// DeleteEdges sets the edges corresponding to predicates on the node with the given uid
147-
// for deletion.
148-
// This helper function doesn't run the mutation on the server. It must be done by the user
149-
// after the function returns.
147+
// DeleteEdges sets the edges corresponding to predicates
148+
// on the node with the given uid for deletion.
149+
// This helper function doesn't run the mutation on the server.
150+
// Txn needs to be committed in order to execute the mutation.
150151
func DeleteEdges(mu *api.Mutation, uid string, predicates ...string) {
151152
for _, predicate := range predicates {
152153
mu.Del = append(mu.Del, &api.NQuad{

doc.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
/*
18-
19-
Package dgo is used to interact with a Dgraph server. Queries, mutations,
20-
and most other types of admin tasks can be run from the client.
21-
22-
*/
17+
// Package dgo is used to interact with a Dgraph server. Queries, mutations,
18+
// and most other types of admin tasks can be run from the client.
2319
package dgo

example_set_object_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func Example_setObject() {
5252
// In the example below new nodes for Alice, Bob and Charlie and school are created (since they
5353
// dont have a Uid).
5454
p := Person{
55+
Uid: "_:alice",
5556
Name: "Alice",
5657
Age: 26,
5758
Married: true,
@@ -103,9 +104,9 @@ func Example_setObject() {
103104
}
104105

105106
// Assigned uids for nodes which were created would be returned in the assigned.Uids map.
106-
variables := map[string]string{"$id": assigned.Uids["blank-0"]}
107-
q := `query Me($id: string){
108-
me(func: uid($id)) {
107+
variables := map[string]string{"$id1": assigned.Uids["alice"]}
108+
q := `query Me($id1: string){
109+
me(func: uid($id1)) {
109110
name
110111
dob
111112
age

examples_test.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ func ExampleTxn_Mutate() {
168168
// In the example below new nodes for Alice, Bob and Charlie and school are created (since they
169169
// don't have a Uid).
170170
p := Person{
171+
Uid: "_:alice",
171172
Name: "Alice",
172173
Age: 26,
173174
Married: true,
@@ -214,7 +215,7 @@ func ExampleTxn_Mutate() {
214215
}
215216

216217
// Assigned uids for nodes which were created would be returned in the assigned.Uids map.
217-
puid := assigned.Uids["blank-0"]
218+
puid := assigned.Uids["alice"]
218219
const q = `query Me($id: string){
219220
me(func: uid($id)) {
220221
name
@@ -352,6 +353,7 @@ func ExampleTxn_Query_unmarshal() {
352353
}
353354

354355
p := Person{
356+
Uid: "_:bob",
355357
Name: "Bob",
356358
Age: 24,
357359
}
@@ -370,13 +372,14 @@ func ExampleTxn_Query_unmarshal() {
370372
if err != nil {
371373
log.Fatal(err)
372374
}
373-
bob := assigned.Uids["blank-0"]
375+
bob := assigned.Uids["bob"]
374376

375377
// While setting an object if a struct has a Uid then its properties in the graph are updated
376378
// else a new node is created.
377379
// In the example below new nodes for Alice and Charlie and school are created (since they dont
378380
// have a Uid). Alice is also connected via the friend edge to an existing node Bob.
379381
p = Person{
382+
Uid: "_:alice",
380383
Name: "Alice",
381384
Age: 26,
382385
Married: true,
@@ -407,7 +410,7 @@ func ExampleTxn_Query_unmarshal() {
407410
}
408411

409412
// Assigned uids for nodes which were created would be returned in the assigned.Uids map.
410-
puid := assigned.Uids["blank-0"]
413+
puid := assigned.Uids["alice"]
411414
variables := make(map[string]string)
412415
variables["$id"] = puid
413416
const q = `query Me($id: string){
@@ -489,6 +492,7 @@ func ExampleTxn_Mutate_facets() {
489492
}
490493

491494
type Person struct {
495+
Uid string `json:"uid,omitempty"`
492496
Name string `json:"name,omitempty"`
493497
NameOrigin string `json:"name|origin,omitempty"`
494498
Friends []Person `json:"friend,omitempty"`
@@ -504,6 +508,7 @@ func ExampleTxn_Mutate_facets() {
504508

505509
ti := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
506510
p := Person{
511+
Uid: "_:alice",
507512
Name: "Alice",
508513
NameOrigin: "Indonesia",
509514
Friends: []Person{
@@ -539,7 +544,7 @@ func ExampleTxn_Mutate_facets() {
539544
log.Fatal(err)
540545
}
541546

542-
auid := assigned.Uids["blank-0"]
547+
auid := assigned.Uids["alice"]
543548
variables := make(map[string]string)
544549
variables["$id"] = auid
545550

@@ -572,7 +577,7 @@ func ExampleTxn_Mutate_facets() {
572577
}
573578

574579
fmt.Printf("Me: %+v\n", r.Me)
575-
// Output: Me: [{Name:Alice NameOrigin:Indonesia Friends:[{Name:Bob NameOrigin: Friends:[] Since:2009-11-10 23:00:00 +0000 UTC Family:yes Age:13 Close:true School:[]}] Since:0001-01-01 00:00:00 +0000 UTC Family: Age:0 Close:false School:[{Name:Wellington School Since:2009-11-10 23:00:00 +0000 UTC}]}]
580+
// Output: Me: [{Uid: Name:Alice NameOrigin:Indonesia Friends:[{Uid: Name:Bob NameOrigin: Friends:[] Since:2009-11-10 23:00:00 +0000 UTC Family:yes Age:13 Close:true School:[]}] Since:0001-01-01 00:00:00 +0000 UTC Family: Age:0 Close:false School:[{Name:Wellington School Since:2009-11-10 23:00:00 +0000 UTC}]}]
576581
}
577582

578583
func ExampleTxn_Mutate_list() {
@@ -678,6 +683,7 @@ func ExampleDeleteEdges() {
678683

679684
// Lets add some data first.
680685
p := Person{
686+
Uid: "_:alice",
681687
Name: "Alice",
682688
Age: 26,
683689
Married: true,
@@ -708,7 +714,7 @@ func ExampleDeleteEdges() {
708714
log.Fatal(err)
709715
}
710716

711-
alice := assigned.Uids["blank-0"]
717+
alice := assigned.Uids["alice"]
712718

713719
variables := make(map[string]string)
714720
variables["$alice"] = alice
@@ -772,14 +778,17 @@ func ExampleTxn_Mutate_deleteNode() {
772778
}
773779

774780
p := Person{
781+
Uid: "_:alice",
775782
Name: "Alice",
776783
Age: 26,
777784
Married: true,
778785
DgraphType: "Person",
779786
Friends: []*Person{&Person{
787+
Uid: "_:bob",
780788
Name: "Bob",
781789
Age: 24,
782790
}, &Person{
791+
Uid: "_:charlie",
783792
Name: "Charlie",
784793
Age: 29,
785794
}},
@@ -817,9 +826,9 @@ func ExampleTxn_Mutate_deleteNode() {
817826
log.Fatal(err)
818827
}
819828

820-
alice := assigned.Uids["blank-0"]
821-
bob := assigned.Uids["blank-1"]
822-
charlie := assigned.Uids["blank-2"]
829+
alice := assigned.Uids["alice"]
830+
bob := assigned.Uids["bob"]
831+
charlie := assigned.Uids["charlie"]
823832

824833
variables := make(map[string]string)
825834
variables["$alice"] = alice
@@ -910,6 +919,7 @@ func ExampleTxn_Mutate_deletePredicate() {
910919
}
911920

912921
p := Person{
922+
Uid: "_:alice",
913923
Name: "Alice",
914924
Age: 26,
915925
Married: true,
@@ -948,7 +958,7 @@ func ExampleTxn_Mutate_deletePredicate() {
948958
log.Fatal(err)
949959
}
950960

951-
alice := assigned.Uids["blank-0"]
961+
alice := assigned.Uids["alice"]
952962

953963
variables := make(map[string]string)
954964
variables["$id"] = alice

0 commit comments

Comments
 (0)