Skip to content

Commit 598f5b9

Browse files
authored
feat(internal/godocfx): different metadata for different modules (#4297)
1 parent 37607b4 commit 598f5b9

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

internal/godocfx/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
cloud.google.com/go/bigquery v1.8.0
88
cloud.google.com/go/datastore v1.1.0
99
cloud.google.com/go/storage v1.11.0
10+
github.com/google/go-cmp v0.5.6
1011
github.com/yuin/goldmark v1.3.8
1112
golang.org/x/tools v0.1.3
1213
gopkg.in/yaml.v2 v2.4.0

internal/godocfx/godocfx_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
package main
1818

1919
import (
20+
"bytes"
2021
"flag"
22+
"fmt"
2123
"io/ioutil"
2224
"os"
2325
"path/filepath"
2426
"testing"
27+
"time"
2528

2629
_ "cloud.google.com/go/bigquery" // Implicitly required by test.
2730
_ "cloud.google.com/go/storage" // Implicitly required by test.
31+
"github.com/google/go-cmp/cmp"
32+
"golang.org/x/tools/go/packages"
2833
)
2934

3035
var updateGoldens bool
@@ -198,3 +203,54 @@ func TestHasPrefix(t *testing.T) {
198203
}
199204
}
200205
}
206+
207+
func TestWriteMetadata(t *testing.T) {
208+
now := time.Now()
209+
210+
want := fmt.Sprintf(`update_time {
211+
seconds: %d
212+
nanos: %d
213+
}
214+
name: "cloud.google.com/go"
215+
version: "100.0.0"
216+
language: "go"
217+
`, now.Unix(), now.Nanosecond())
218+
219+
wantAppEngine := fmt.Sprintf(`update_time {
220+
seconds: %d
221+
nanos: %d
222+
}
223+
name: "google.golang.org/appengine/v2"
224+
version: "2.0.0"
225+
language: "go"
226+
stem: "/appengine/docs/standard/go/reference/services/bundled"
227+
`, now.Unix(), now.Nanosecond())
228+
229+
tests := []struct {
230+
path string
231+
version string
232+
want string
233+
}{
234+
{
235+
path: "cloud.google.com/go",
236+
version: "100.0.0",
237+
want: want,
238+
},
239+
{
240+
path: "google.golang.org/appengine/v2",
241+
version: "2.0.0",
242+
want: wantAppEngine,
243+
},
244+
}
245+
for _, test := range tests {
246+
var buf bytes.Buffer
247+
module := &packages.Module{
248+
Path: test.path,
249+
Version: test.version,
250+
}
251+
writeMetadata(&buf, now, module)
252+
if diff := cmp.Diff(test.want, buf.String()); diff != "" {
253+
t.Errorf("writeMetadata(%q) got unexpected diff (-want +got):\n\n%s", test.path, diff)
254+
}
255+
}
256+
}

internal/godocfx/main.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
"strings"
5050
"time"
5151

52+
"golang.org/x/tools/go/packages"
5253
"gopkg.in/yaml.v2"
5354
)
5455

@@ -245,12 +246,27 @@ func write(outDir string, r *result) error {
245246
}
246247
defer f.Close()
247248
now := time.Now().UTC()
248-
fmt.Fprintf(f, `update_time {
249-
seconds: %d
250-
nanos: %d
249+
writeMetadata(f, now, r.module)
250+
return nil
251+
}
252+
253+
func writeMetadata(w io.Writer, now time.Time, module *packages.Module) {
254+
fmt.Fprintf(w, `update_time {
255+
seconds: %d
256+
nanos: %d
251257
}
252258
name: %q
253259
version: %q
254-
language: "go"`, now.Unix(), now.Nanosecond(), r.module.Path, r.module.Version)
255-
return nil
260+
language: "go"
261+
`, now.Unix(), now.Nanosecond(), module.Path, module.Version)
262+
263+
// Some modules specify a different path to serve from.
264+
// The URL will be /[stem]/[version]/[pkg path relative to module].
265+
// Alternatively, we could plumb this through command line flags.
266+
switch module.Path {
267+
case "google.golang.org/appengine":
268+
fmt.Fprintf(w, "stem: \"/appengine/docs/standard/go111/reference\"\n")
269+
case "google.golang.org/appengine/v2":
270+
fmt.Fprintf(w, "stem: \"/appengine/docs/standard/go/reference/services/bundled\"\n")
271+
}
256272
}

0 commit comments

Comments
 (0)