xdgraph

package module
v0.0.0-...-c27b752 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2017 License: Apache-2.0 Imports: 5 Imported by: 0

README

xdgraph: a simple Go helper for manipulating Dgraph gRPC responses

Installation

go get github.com/etix/xdgraph

Usage
package main

import (
    "github.com/etix/xdgraph"
    "..."
)

int main() {
    // Set up the gRPC connection
    conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure())
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()

    // Initialize the Dgraph gRPC object
    c := graph.NewDgraphClient(conn)
    req := client.Req{}
    req.SetQuery(`<your query>`)

    // Execute the query and fetch the response
    resp, err := c.Run(context.Background(), req.Request())
    if err != nil {
        log.Fatal(err)
    }

    // Let's use xdgraph to manipulate the results!
    xd := xdgraph.ReadResponse(resp)

    // Get the UID of the "me" attribute
    fmt.Println(xd.Attribute("me")..Property("_uid_").ToUid())

    // Get the "name" property of the "me" attribute
    fmt.Println(xd.Attribute("me").Property("name").ToString())

    // Skipping the first attribute using First()
    fmt.Println(xd.First().Property("sign").ToString())

    // Check if a property is set (true/false)
    fmt.Println(xd.First().Property("address").IsNil())

    // Traverse multiple attributes
    fmt.Println(xd.First().Attribute("follows").Property("name").ToString())

    // Use different types
    fmt.Println(xd.First().Attribute("follows").Property("birthdate").ToDate())

    // Execute a function literal for each matched attribute
    xd.First().Attribute("follows").Attribute("plays").Each(func(r xdgraph.Response) {
        fmt.Println(r.Property("name").ToString())
    })

    // Get multiple properties at once
    for _, sign := range xd.First().Attribute("follows").Properties("sign") {
        fmt.Println(sign.ToString())
    }

    // Output the full graph in JSON
    fmt.Println(xd.Json())

    // Output a sub-graph in JSON
    fmt.Println(xd.First().Attribute("follows").Json())
}

See the example folder for a complete working example.

Supported property types

See here for the list of supported types upstream.

Type
string .ToString()
[]byte .ToBytes()
int64 .ToInt()
bool .ToBool()
float64 .ToFloat()
geom .ToGeo()
datetime .ToDate() or .ToDateTime()
uid .ToUid()
Note

Since the Dgraph project is quite young, the APIs can change at any time.

Documentation

Overview

Package xdgraph provides a simple helper for manipulating Dgraph gRPC responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Property

type Property struct {
	// contains filtered or unexported fields
}

Property is a struct that carries the current graph.Value.

func (Property) IsNil

func (p Property) IsNil() bool

IsNil returns true if the property is not available in the response.

func (Property) String

func (p Property) String() string

String returns the RAW value

func (Property) ToBool

func (p Property) ToBool() bool

ToBool returns the property as a bool

func (Property) ToBytes

func (p Property) ToBytes() []byte

ToBytes returns the property as []byte

func (Property) ToDateTime

func (p Property) ToDateTime() time.Time

ToDateTime returns the property as a time.Time

func (Property) ToFloat

func (p Property) ToFloat() float64

ToFloat returns the property as a float64

func (Property) ToGeo

func (p Property) ToGeo() geom.T

ToGeo returns the property as a geom.T

func (Property) ToInt

func (p Property) ToInt() int64

ToInt returns the property as an int64

func (Property) ToPassword

func (p Property) ToPassword() string

ToPassword returns the property as a string

func (Property) ToString

func (p Property) ToString() string

ToString returns the property as a string

func (Property) ToUid

func (p Property) ToUid() uint64

ToUid returns the property as a uint64

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response is a struct that carries the current graph.Node.

func ReadResponse

func ReadResponse(resp *protos.Response) *Response

ReadResponse is the entry point of this package It takes in parameter the response from a gRPC call:

resp, _ := c.Run(...)
xd := xdgraph.ReadResponse(resp)
[...]

func (Response) Attribute

func (r Response) Attribute(name string) Response

Attribute moves to the given attribute name. It must be a children of the current attribute. This can be asserted using the IsNil() function.

func (Response) Each

func (r Response) Each(fn func(Response))

Each will run the provided function for each elements contained in the response. Example:

xd.First().Attribute("follows").Each(func(r xdgraph.Response) {
    fmt.Println(r.Property("name").ToString())
})

func (Response) First

func (r Response) First() Response

First can be used to access the first attribute without explicitely giving its name.

func (Response) IsNil

func (r Response) IsNil() bool

IsNil returns true if the attribute is not available in the response.

func (Response) Json

func (r Response) Json() string

Json returns the attribute content in JSON format.

func (Response) Properties

func (r Response) Properties(name string) []Property

Properties returns a slice of properties by name.

func (Response) Property

func (r Response) Property(name string) Property

Property returns the given property by name.

func (Response) String

func (r Response) String() string

String returns the attribute content in RAW format.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL