Skip to content

Commit 8a10cc2

Browse files
authored
SampleGen: generate comments in output handling (#152)
1 parent 0645300 commit 8a10cc2

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

cmd/gen-go-sample/gapic.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type GAPICSample struct {
6262
}
6363

6464
type OutputSpec struct {
65+
Comment []string
6566
Define string
6667
Print []string
6768
Loop *LoopSpec

cmd/gen-go-sample/main_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func TestUnary(t *testing.T) {
6262
OnSuccess: []OutputSpec{
6363
{Define: "out_a = $resp.a"},
6464
{Print: []string{"x = %s", "$resp.a.x"}},
65+
{Comment: []string{
66+
"%s contains the field %s,\n%%%% is a single percent,\nand '\\n' specifies a newline in comment too",
67+
"out_a",
68+
"y"}},
6569
{Print: []string{"y = %s", "out_a.y"}},
6670
{
6771
Loop: &LoopSpec{

cmd/gen-go-sample/out.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package main
1616

1717
import (
18+
"bytes"
1819
"fmt"
1920
"strings"
2021
"text/scanner"
@@ -101,6 +102,10 @@ func writeOutputSpec(out OutputSpec, st *symTab, gen *generator) error {
101102
used++
102103
err = writeDump(dp.FileName[0], dp.FileName[1:], dp.Contents, st, gen)
103104
}
105+
if c := out.Comment; len(c) > 0 {
106+
used++
107+
err = writeComment(c[0], c[1:], gen)
108+
}
104109

105110
if used == 0 {
106111
return errors.E(nil, "OutputSpec not defined")
@@ -252,6 +257,28 @@ func writeMap(l *LoopSpec, st *symTab, gen *generator) error {
252257
return nil
253258
}
254259

260+
func writeComment(cmtFmt string, cmtArgs []string, gen *generator) error {
261+
var buf bytes.Buffer
262+
args := make([]interface{}, len(cmtArgs))
263+
for i := range cmtArgs {
264+
args[i] = snakeToCamel(cmtArgs[i])
265+
}
266+
267+
if _, err := fmt.Fprintf(&buf, cmtFmt, args...); err != nil {
268+
return errors.E(err, "comment spec: bad format")
269+
}
270+
buf.WriteString("\n")
271+
prependLines(&buf, "// ", false)
272+
cmts := strings.Split(buf.String(), "\n")
273+
for i, c := range(cmts) {
274+
if i == len(cmts) - 1 {
275+
continue
276+
}
277+
gen.pt.Printf(c)
278+
}
279+
return nil
280+
}
281+
255282
func writePath(sc *scanner.Scanner, st *symTab, info pbinfo.Info) (string, initType, error) {
256283
if sc.Scan() != scanner.Ident {
257284
return "", initType{}, errors.E(nil, "expecting ident, got %s", sc.TokenText())

cmd/gen-go-sample/testdata/sample_unary.want

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ func sampleUnaryMethod(theX int64, theB string, theFoo string, bobFile string) e
8080

8181
outA := resp.GetA()
8282
fmt.Printf("x = %v\n", resp.GetA().GetX())
83+
// outA contains the field y,
84+
// % is a single percent,
85+
// and '\n' specifies a newline in comment too
8386
fmt.Printf("y = %v\n", outA.GetY())
8487
for _, r := range resp.GetR() {
8588
fmt.Printf("resp.r contains %v\n", r)

0 commit comments

Comments
 (0)