/
util_interface.go
72 lines (65 loc) · 1.97 KB
/
util_interface.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package java
import (
"bytes"
"path"
"v.io/x/ref/lib/vdl/compile"
"v.io/x/ref/lib/vdl/vdlutil"
)
// allEmbeddedIfaces returns all unique interfaces in the embed tree
// starting at the provided interface (not including that interface).
func allEmbeddedIfaces(iface *compile.Interface) (ret []*compile.Interface) {
added := make(map[string]bool)
for _, eIface := range iface.Embeds {
for _, eIface = range append(allEmbeddedIfaces(eIface), eIface) {
path := path.Join(eIface.File.Package.GenPath, vdlutil.FirstRuneToUpper(eIface.Name))
if added[path] { // already added iface
continue
}
ret = append(ret, eIface)
added[path] = true
}
}
return
}
// interfaceFullyQualifiedName outputs the fully qualified name of an interface
// e.g. "com.a.B"
func interfaceFullyQualifiedName(iface *compile.Interface) string {
return path.Join(javaGenPkgPath(iface.File.Package.GenPath), vdlutil.FirstRuneToUpper(iface.Name))
}
// javaClientExtendsStr creates an extends clause for a client interface
// e.g. "extends com.a.B, com.d.E"
func javaClientExtendsStr(embeds []*compile.Interface) string {
if len(embeds) == 0 {
return ""
}
var buf bytes.Buffer
buf.WriteString("extends ")
for i, embed := range embeds {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(javaPath(interfaceFullyQualifiedName(embed)))
buf.WriteString("Client")
}
return buf.String()
}
// javaServerExtendsStr creates an extends clause for a server interface
// e.g. "extends com.a.B, com.d.E"
func javaServerExtendsStr(embeds []*compile.Interface) string {
if len(embeds) == 0 {
return ""
}
var buf bytes.Buffer
buf.WriteString("extends ")
for i, embed := range embeds {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(javaPath(interfaceFullyQualifiedName(embed)))
buf.WriteString("Server")
}
return buf.String()
}