Skip to content

Commit

Permalink
up go version since huma actually requires it, remove extra deps
Browse files Browse the repository at this point in the history
  • Loading branch information
cardinalby committed Jun 25, 2024
1 parent b861d4f commit d0df3b0
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
go-version-file: 'go.mod'

- run: go mod download
- run: go test ./...
- run: go test -vet=all -race ./...

- env:
GOPROXY: "proxy.golang.org"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
go-version-file: 'go.mod'

- run: go mod download
- run: go test ./...
- run: go test -vet=all -race ./...
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ go get github.com/cardinalby/hureg
```go
import "github.com/cardinalby/hureg"

chiRouter := chi.NewRouter() // --
httpServeMux := http.NewServeMux() // with go 1.22
cfg := huma.DefaultConfig("My API", "1.0.0") // default HUMA initialization
humaApi := humachi.New(chiRouter, cfg) // --
humaApi := humago.New(httpServeMux, cfg) // --

api := hureg.NewAPIGen(humaApi) // The new line
```
Expand Down Expand Up @@ -106,10 +106,15 @@ hureg.Get(trGr, "/crocodile", ...)
### 🔻 Complete server setup

Check out [integration_test.go](./integration_test.go) for a complete example of how to use the library:
- create `huma.API` from `chi` router
- create `huma.API` from `http.ServeMux` router
- create `APIGen` instance on top of `huma.API`
- register operations with `APIGen` instance
- use base paths, tags and _Transformers_ to the groups
- register OpenAPI endpoints manually with Basic Auth middleware

Uncommenting one line you can run the server and play with it in live mode.
Uncommenting one line you can run the server and play with it in live mode.

## Go version

Even though Huma declares Go 1.20 as the minimal supported version, it actually requires Go 1.22 for correct work
due to "slices" package usage. So `hureg` requires Go 1.22 explicitly.
6 changes: 3 additions & 3 deletions api_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
// It provides a fluent API to create derived APIGen instances with own set of actions/changes to an
// operation before its registration.
type APIGen struct {
humaAPI huma.API
humaAPIWrapper humaApiWrapper
regMiddlewares RegMiddlewares
transformers []huma.Transformer
}

// NewAPIGen creates a new APIGen instance with the given huma.API.
func NewAPIGen(humaApi huma.API) APIGen {
return APIGen{
humaAPI: newHumaApiWrapper(humaApi),
humaAPIWrapper: newHumaApiWrapper(humaApi),
}
}

// GetHumaAPI returns the wrapped huma.API.
func (a APIGen) GetHumaAPI() huma.API {
return a.humaAPI
return a.humaAPIWrapper
}

// AddRegMiddleware returns a new APIGen instance with the given RegMiddlewares added to the stored RegMiddlewares.
Expand Down
2 changes: 1 addition & 1 deletion api_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestAPIGen_GetHumaAPI(t *testing.T) {
t.Parallel()
humaAPI := humago.New(http.NewServeMux(), huma.Config{})
api := NewAPIGen(humaAPI)
require.Equal(t, humaAPI, api.GetHumaAPI())
require.Same(t, humaAPI, api.GetHumaAPI().(humaApiWrapper).API)
}

func TestAPIGen_GetRegMiddlewares(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions docs/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ registered with the particular instance of `APIGen`.
```go
import "github.com/cardinalby/hureg"

chiRouter := chi.NewRouter() // chi router is used in the example for
cfg := huma.DefaultConfig("My API", "1.0.0") // go 1.20 compatibility.
humaApi := humachi.New(chiRouter, cfg) // It's default Huma initialization
httpServeMux := http.NewServeMux() // with Go 1.22
cfg := huma.DefaultConfig("My API", "1.0.0") //
humaApi := humago.New(httpServeMux, cfg) // It's default Huma initialization

api := hureg.NewAPIGen(humaApi) // That's how APIGen instance is created

Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
module github.com/cardinalby/hureg

go 1.20
go 1.22

require (
github.com/danielgtaylor/huma/v2 v2.18.0
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-chi/chi/v5 v5.0.13 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/chi/v5 v5.0.13 h1:JlH2F2M8qnwl0N1+JFFzlX9TlKJYas3aPXdiuTmJL+w=
github.com/go-chi/chi/v5 v5.0.13/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
14 changes: 8 additions & 6 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
"time"

"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/adapters/humachi"
"github.com/go-chi/chi/v5"
"github.com/danielgtaylor/huma/v2/adapters/humago"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

Expand All @@ -30,21 +29,23 @@ func TestHttpServer(t *testing.T) {
addr, stop := listenAndServe(t, handler)
defer stop()

_ = addr
testServerEndpoints(t, addr)
testOpenApiSpec(t, addr)

// uncomment to play with the server
waitSigInt(stop)
//waitSigInt(stop)
}

func createTestServer(t *testing.T) http.Handler {
chiRouter := chi.NewRouter()
httpServeMux := http.NewServeMux()

cfg := huma.DefaultConfig("My API", "1.0.0")
cfg.OpenAPIPath = ""
cfg.DocsPath = ""
cfg.SchemasPath = ""

humaApi := humachi.New(chiRouter, cfg)
humaApi := humago.New(httpServeMux, cfg)

api := NewAPIGen(humaApi)

Expand All @@ -53,7 +54,7 @@ func createTestServer(t *testing.T) http.Handler {
apiWithBasicAuth := api.AddMiddlewares(newTestBasicAuthMiddleware())
defineManualOpenApiEndpoints(t, apiWithBasicAuth)

return chiRouter
return httpServeMux
}

func defineAnimalEndpoints(api APIGen) {
Expand Down Expand Up @@ -246,6 +247,7 @@ func getBytesResponse(t *testing.T, addr, path string, headers http.Header) []by
return data
}

//goland:noinspection GoUnusedFunction
func waitSigInt(stop func()) {
onInterrupt := make(chan os.Signal, 1)
signal.Notify(onInterrupt, os.Interrupt)
Expand Down
2 changes: 2 additions & 0 deletions pkg/huma/oapi_handlers/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func GetSchemaAdapterHandler(humaApi huma.API, schemasPath string) func(ctx huma

// GetSchemaTypedHandler returns a handler that will return a schema from the OpenAPI spec.
// The handler format is suitable for passing it to huma or hureg registration functions.
//
//goland:noinspection GoUnusedExportedFunction
func GetSchemaTypedHandler(humaApi huma.API, schemasPath string) TypedStreamHandler {
adapterHandler := GetSchemaAdapterHandler(humaApi, schemasPath)
return getTypedStreamHandler(adapterHandler)
Expand Down
1 change: 0 additions & 1 deletion pkg/huma/op_handler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ What can you do with provided _Operation Handlers_ is:

- [Add Security entries to the operation](./add_security.go)
- [Add Tags to the operation](./add_tags.go)
- [Add description to the operation](./add_description.go)
- [Generate operationID](./generate_operation_id.go) - can be useful for explicitly defined operations
- [Generate operation Summary](./generate_summary.go) - can be useful for explicitly defined operations
- [Conditionally apply other handlers](./if.go)
Expand Down
3 changes: 2 additions & 1 deletion pkg/huma/op_handler/add_tags.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package op_handler

import (
"slices"

"github.com/danielgtaylor/huma/v2"
"golang.org/x/exp/slices"
)

// AddTags creates an OperationHandler that appends the given tags to the operation's Tags field.
Expand Down
4 changes: 2 additions & 2 deletions pkg/huma/operation/clone.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package operation

import (
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"maps"
"slices"

"github.com/danielgtaylor/huma/v2"
)
Expand Down
12 changes: 12 additions & 0 deletions register.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func Get[I, O any](

// Post is a shortcut for Register method that implicitly generates Operation object
// metadata.KeyIsExplicitOperationID, metadata.KeyIsExplicitSummary keys will not be set.
//
//goland:noinspection GoUnusedExportedFunction
func Post[I, O any](
api API,
path string,
Expand All @@ -62,6 +64,8 @@ func Post[I, O any](

// Put is a shortcut for Register method that implicitly generates Operation object
// metadata.KeyIsExplicitOperationID, metadata.KeyIsExplicitSummary keys will not be set.
//
//goland:noinspection GoUnusedExportedFunction
func Put[I, O any](
api API,
path string,
Expand All @@ -73,6 +77,8 @@ func Put[I, O any](

// Patch is a shortcut for Register method that implicitly generates Operation object
// metadata.KeyIsExplicitOperationID, metadata.KeyIsExplicitSummary keys will not be set.
//
//goland:noinspection GoUnusedExportedFunction
func Patch[I, O any](
api API,
path string,
Expand All @@ -84,6 +90,8 @@ func Patch[I, O any](

// Delete is a shortcut for Register method that implicitly generates Operation object
// metadata.KeyIsExplicitOperationID, metadata.KeyIsExplicitSummary keys will not be set.
//
//goland:noinspection GoUnusedExportedFunction
func Delete[I, O any](
api API,
path string,
Expand All @@ -96,6 +104,8 @@ func Delete[I, O any](

// Options is a shortcut for Register method that implicitly generates Operation object
// metadata.KeyIsExplicitOperationID, metadata.KeyIsExplicitSummary keys will not be set.
//
//goland:noinspection GoUnusedExportedFunction
func Options[I, O any](
api API,
path string,
Expand All @@ -108,6 +118,8 @@ func Options[I, O any](

// Head is a shortcut for Register method that implicitly generates Operation object
// metadata.KeyIsExplicitOperationID, metadata.KeyIsExplicitSummary keys will not be set.
//
//goland:noinspection GoUnusedExportedFunction
func Head[I, O any](
api API,
path string,
Expand Down

0 comments on commit d0df3b0

Please sign in to comment.