File: response.go

package info (click to toggle)
golang-github-facebook-ent 0.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,284 kB
  • sloc: javascript: 349; makefile: 8
file content (126 lines) | stat: -rw-r--r-- 3,345 bytes parent folder | download | duplicates (2)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package gremlin

import (
	"github.com/facebook/ent/dialect/gremlin/encoding/graphson"
	"github.com/facebook/ent/dialect/gremlin/graph"

	"github.com/pkg/errors"
)

// A Response models a response message received from the server.
type Response struct {
	RequestID string `json:"requestId" graphson:"g:UUID"`
	Status    struct {
		Code       int                    `json:"code"`
		Attributes map[string]interface{} `json:"attributes"`
		Message    string                 `json:"message"`
	} `json:"status"`
	Result struct {
		Data graphson.RawMessage    `json:"data"`
		Meta map[string]interface{} `json:"meta"`
	} `json:"result"`
}

// IsErr returns whether response indicates an error.
func (rsp *Response) IsErr() bool {
	switch rsp.Status.Code {
	case StatusSuccess, StatusNoContent, StatusPartialContent:
		return false
	default:
		return true
	}
}

// Err returns an error representing response status.
func (rsp *Response) Err() error {
	if rsp.IsErr() {
		return errors.Errorf("gremlin: code=%d, message=%q", rsp.Status.Code, rsp.Status.Message)
	}
	return nil
}

// ReadVal reads gremlin response data into v.
func (rsp *Response) ReadVal(v interface{}) error {
	if err := rsp.Err(); err != nil {
		return err
	}
	if err := graphson.Unmarshal(rsp.Result.Data, v); err != nil {
		return errors.Wrapf(err, "gremlin: unmarshal response data: type=%T", v)
	}
	return nil
}

// ReadVertices returns response data as slice of vertices.
func (rsp *Response) ReadVertices() ([]graph.Vertex, error) {
	var v []graph.Vertex
	err := rsp.ReadVal(&v)
	return v, err
}

// ReadVertexProperties returns response data as slice of vertex properties.
func (rsp *Response) ReadVertexProperties() ([]graph.VertexProperty, error) {
	var vp []graph.VertexProperty
	err := rsp.ReadVal(&vp)
	return vp, err
}

// ReadEdges returns response data as slice of edges.
func (rsp *Response) ReadEdges() ([]graph.Edge, error) {
	var e []graph.Edge
	err := rsp.ReadVal(&e)
	return e, err
}

// ReadProperties returns response data as slice of properties.
func (rsp *Response) ReadProperties() ([]graph.Property, error) {
	var p []graph.Property
	err := rsp.ReadVal(&p)
	return p, err
}

// ReadValueMap returns response data as a value map.
func (rsp *Response) ReadValueMap() (graph.ValueMap, error) {
	var m graph.ValueMap
	err := rsp.ReadVal(&m)
	return m, err
}

// ReadBool returns response data as a bool.
func (rsp *Response) ReadBool() (bool, error) {
	var b [1]*bool
	if err := rsp.ReadVal(&b); err != nil {
		return false, err
	}
	if b[0] == nil {
		return false, errors.New("gremlin: no boolean value")
	}
	return *b[0], nil
}

// ReadInt returns response data as an int.
func (rsp *Response) ReadInt() (int, error) {
	var v [1]*int
	if err := rsp.ReadVal(&v); err != nil {
		return 0, err
	}
	if v[0] == nil {
		return 0, errors.New("gremlin: no integer value")
	}
	return *v[0], nil
}

// ReadString returns response data as a string.
func (rsp *Response) ReadString() (string, error) {
	var v [1]*string
	if err := rsp.ReadVal(&v); err != nil {
		return "", err
	}
	if v[0] == nil {
		return "", errors.New("gremlin: no string value")
	}
	return *v[0], nil
}