diff options
Diffstat (limited to 'internal/api')
| -rw-r--r-- | internal/api/api.go | 9 | ||||
| -rw-r--r-- | internal/api/api_test.go | 88 | ||||
| -rw-r--r-- | internal/api/params.go | 1 |
3 files changed, 95 insertions, 3 deletions
diff --git a/internal/api/api.go b/internal/api/api.go index 831b1af2..2d4d80b5 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -54,7 +54,7 @@ func ServePackage(w http.ResponseWriter, r *http.Request, ds internal.DataSource if params.Imports { fs |= internal.WithImports } - if params.Doc != "" { + if params.Doc != "" || params.Examples { fs |= internal.WithDocsSource } @@ -543,6 +543,10 @@ func paginate[T any](all []T, lp ListParams, defaultLimit int) (PaginatedRespons // unitToPackage processes unit documentation into a Package struct. func unitToPackage(unit *internal.Unit, params PackageParams) (*Package, error) { + if params.Examples && params.Doc == "" { + return nil, fmt.Errorf("%w: examples require doc format to be specified", derrors.InvalidArgument) + } + // Although unit.Documentation is a slice, it will // have at most one item, the documentation matching // the build context. @@ -565,8 +569,7 @@ func unitToPackage(unit *internal.Unit, params PackageParams) (*Package, error) } if params.Doc != "" { var err error - const examples = true // TODO(jba): make examples configurable. - docs, err = renderDocumentation(unit, d, params.Doc, examples) + docs, err = renderDocumentation(unit, d, params.Doc, params.Examples) if err != nil { return nil, err } diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 36066f46..5d5c9c03 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -106,6 +106,32 @@ func TestServePackage(t *testing.T) { }}, }) + ds.MustInsertModule(ctx, &internal.Module{ + ModuleInfo: internal.ModuleInfo{ + ModulePath: "example.com/ex", + Version: "v1.0.0", + LatestVersion: "v1.0.0", + }, + Units: []*internal.Unit{{ + UnitMeta: internal.UnitMeta{ + Path: "example.com/ex/pkg", + ModuleInfo: internal.ModuleInfo{ + ModulePath: "example.com/ex", + Version: "v1.0.0", + LatestVersion: "v1.0.0", + }, + Name: "pkg", + }, + Documentation: []*internal.Documentation{sample.DocumentationWithExamples("linux", "amd64", "", ` + import "fmt" + func Example() { + fmt.Println("hello") + // Output: hello + } + `)}, + }}, + }) + for _, test := range []struct { name string url string @@ -195,6 +221,68 @@ func TestServePackage(t *testing.T) { Docs: "package p\n\nPackage p is a package.\n\n# Links\n\n- pkg.go.dev, https://pkg.go.dev\n\nVARIABLES\n\nvar V int\n\n", }, }, + { + name: "doc with examples", + url: "/v1/package/example.com/ex/pkg?version=v1.0.0&doc=text&examples=true", + wantStatus: http.StatusOK, + want: &Package{ + Path: "example.com/ex/pkg", + ModulePath: "example.com/ex", + ModuleVersion: "v1.0.0", + Synopsis: "This is a package synopsis for GOOS=linux, GOARCH=amd64", + IsLatest: true, + GOOS: "linux", + GOARCH: "amd64", + Docs: "package pkg\n\nPackage pkg is a package.\n\nExample:\n\t{\n\t\tfmt.Println(\"hello\")\n\t}\n\n\tOutput:\n\thello\n\n", + }, + }, + { + name: "examples without doc (returns 400)", + url: "/v1/package/example.com/ex/pkg?version=v1.0.0&examples=true", + wantStatus: http.StatusBadRequest, + want: &Error{ + Code: http.StatusBadRequest, + Message: "ServePackage: invalid argument: examples require doc format to be specified", + }, + }, + { + name: "doc without examples", + url: "/v1/package/example.com/ex/pkg?version=v1.0.0&doc=text&examples=false", + wantStatus: http.StatusOK, + want: &Package{ + Path: "example.com/ex/pkg", + ModulePath: "example.com/ex", + ModuleVersion: "v1.0.0", + Synopsis: "This is a package synopsis for GOOS=linux, GOARCH=amd64", + IsLatest: true, + GOOS: "linux", + GOARCH: "amd64", + Docs: "package pkg\n\nPackage pkg is a package.\n\n", + }, + }, + { + name: "invalid doc format", + url: "/v1/package/example.com/pkg?version=v1.2.3&doc=invalid", + wantStatus: http.StatusBadRequest, + want: &Error{ + Code: http.StatusBadRequest, + Message: "ServePackage: invalid argument: bad doc format: need one of 'text', 'md', 'markdown' or 'html'", + }, + }, + { + name: "empty doc format", + url: "/v1/package/example.com/pkg?version=v1.2.3&doc=", + wantStatus: http.StatusOK, + want: &Package{ + Path: "example.com/pkg", + ModulePath: "example.com", + ModuleVersion: version, + Synopsis: "This is a package synopsis for GOOS=linux, GOARCH=amd64", + GOOS: "linux", + GOARCH: "amd64", + Docs: "", + }, + }, } { t.Run(test.name, func(t *testing.T) { r := httptest.NewRequest("GET", test.url, nil) diff --git a/internal/api/params.go b/internal/api/params.go index 255a6128..6858de5e 100644 --- a/internal/api/params.go +++ b/internal/api/params.go @@ -27,6 +27,7 @@ type PackageParams struct { GOOS string `form:"goos"` GOARCH string `form:"goarch"` Doc string `form:"doc"` + Examples bool `form:"examples"` Imports bool `form:"imports"` Licenses bool `form:"licenses"` } |
