File: orgvarlinkservice.go

package info (click to toggle)
golang-github-varlink-go 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, experimental, forky, sid, trixie
  • size: 272 kB
  • sloc: makefile: 13
file content (164 lines) | stat: -rw-r--r-- 4,896 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package varlink

import "context"

// The requested interface was not found.
type InterfaceNotFound struct {
	Interface string `json:"interface"`
}

func (e InterfaceNotFound) Error() string {
	return "org.varlink.service.InterfaceNotFound"
}

// The requested method was not found
type MethodNotFound struct {
	Method string `json:"method"`
}

func (e MethodNotFound) Error() string {
	return "org.varlink.service.MethodNotFound"
}

// The interface defines the requested method, but the service does not
// implement it.
type MethodNotImplemented struct {
	Method string `json:"method"`
}

func (e MethodNotImplemented) Error() string {
	return "org.varlink.service.MethodNotImplemented"
}

// One of the passed parameters is invalid.
type InvalidParameter struct {
	Parameter string `json:"parameter"`
}

func (e InvalidParameter) Error() string {
	return "org.varlink.service.InvalidParameter"
}

func doReplyError(ctx context.Context, c *Call, name string, parameters interface{}) error {
	return c.sendMessage(ctx, &serviceReply{
		Error:      name,
		Parameters: parameters,
	})
}

// ReplyInterfaceNotFound sends a org.varlink.service error reply to this method call
func (c *Call) ReplyInterfaceNotFound(ctx context.Context, interfaceA string) error {
	var out InterfaceNotFound
	out.Interface = interfaceA
	return doReplyError(ctx, c, "org.varlink.service.InterfaceNotFound", &out)
}

// ReplyMethodNotFound sends a org.varlink.service error reply to this method call
func (c *Call) ReplyMethodNotFound(ctx context.Context, method string) error {
	var out MethodNotFound
	out.Method = method
	return doReplyError(ctx, c, "org.varlink.service.MethodNotFound", &out)
}

// ReplyMethodNotImplemented sends a org.varlink.service error reply to this method call
func (c *Call) ReplyMethodNotImplemented(ctx context.Context, method string) error {
	var out MethodNotImplemented
	out.Method = method
	return doReplyError(ctx, c, "org.varlink.service.MethodNotImplemented", &out)
}

// ReplyInvalidParameter sends a org.varlink.service error reply to this method call
func (c *Call) ReplyInvalidParameter(ctx context.Context, parameter string) error {
	var out InvalidParameter
	out.Parameter = parameter
	return doReplyError(ctx, c, "org.varlink.service.InvalidParameter", &out)
}

func (c *Call) replyGetInfo(ctx context.Context, vendor string, product string, version string, url string, interfaces []string) error {
	var out struct {
		Vendor     string   `json:"vendor,omitempty"`
		Product    string   `json:"product,omitempty"`
		Version    string   `json:"version,omitempty"`
		URL        string   `json:"url,omitempty"`
		Interfaces []string `json:"interfaces,omitempty"`
	}
	out.Vendor = vendor
	out.Product = product
	out.Version = version
	out.URL = url
	out.Interfaces = interfaces
	return c.Reply(ctx, &out)
}

func (c *Call) replyGetInterfaceDescription(ctx context.Context, description string) error {
	var out struct {
		Description string `json:"description,omitempty"`
	}
	out.Description = description
	return c.Reply(ctx, &out)
}

func (s *Service) orgvarlinkserviceDispatch(ctx context.Context, c Call, methodname string) error {
	switch methodname {
	case "GetInfo":
		return s.getInfo(ctx, c)
	case "GetInterfaceDescription":
		var in struct {
			Interface string `json:"interface"`
		}
		err := c.GetParameters(&in)
		if err != nil {
			return c.ReplyInvalidParameter(ctx, "parameters")
		}
		return s.getInterfaceDescription(ctx, c, in.Interface)

	default:
		return c.ReplyMethodNotFound(ctx, methodname)
	}
}

func (s *orgvarlinkserviceInterface) VarlinkDispatch(ctx context.Context, call Call, methodname string) error {
	return nil
}

func (s *orgvarlinkserviceInterface) VarlinkGetName() string {
	return `org.varlink.service`
}

func (s *orgvarlinkserviceInterface) VarlinkGetDescription() string {
	return `# The Varlink Service Interface is provided by every varlink service. It
# describes the service and the interfaces it implements.
interface org.varlink.service

# Get a list of all the interfaces a service provides and information
# about the implementation.
method GetInfo() -> (
  vendor: string,
  product: string,
  version: string,
  url: string,
  interfaces: []string
)

# Get the description of an interface that is implemented by this service.
method GetInterfaceDescription(interface: string) -> (description: string)

# The requested interface was not found.
error InterfaceNotFound (interface: string)

# The requested method was not found
error MethodNotFound (method: string)

# The interface defines the requested method, but the service does not
# implement it.
error MethodNotImplemented (method: string)

# One of the passed parameters is invalid.
error InvalidParameter (parameter: string)`
}

type orgvarlinkserviceInterface struct{}

func orgvarlinkserviceNew() *orgvarlinkserviceInterface {
	return &orgvarlinkserviceInterface{}
}