docs: add example of using `go tool` for Go 1.24+ · oapi-codegen/oapi-codegen@2647a10 · GitHub
Skip to content

Commit 2647a10

Browse files
Jamie Tannajamietanna
authored andcommitted
docs: add example of using go tool for Go 1.24+
1 parent 5630994 commit 2647a10

9 files changed

Lines changed: 481 additions & 0 deletions

File tree

README.md

Lines changed: 19 additions & 0 deletions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
SHELL:=/bin/bash
2+
3+
YELLOW := \e[0;33m
4+
RESET := \e[0;0m
5+
6+
GOVER := $(shell go env GOVERSION)
7+
GOMINOR := $(shell bash -c "cut -f2 -d. <<< $(GOVER)")
8+
9+
define execute-if-go-124
10+
@{ \
11+
if [[ 24 -le $(GOMINOR) ]]; then \
12+
$1; \
13+
else \
14+
echo -e "$(YELLOW)Skipping task as you're running Go v1.$(GOMINOR).x which is < Go 1.24, which this module requires$(RESET)"; \
15+
fi \
16+
}
17+
endef
18+
19+
lint:
20+
$(call execute-if-go-124,$(GOBIN)/golangci-lint run ./...)
21+
22+
lint-ci:
23+
24+
$(call execute-if-go-124,$(GOBIN)/golangci-lint run ./... --out-format=colored-line-number --timeout=5m)
25+
26+
generate:
27+
$(call execute-if-go-124,go generate ./...)
28+
29+
test:
30+
$(call execute-if-go-124,go test -cover ./...)
31+
32+
tidy:
33+
$(call execute-if-go-124,go mod tidy)
34+
35+
tidy-ci:
36+
$(call execute-if-go-124,tidied -verbose)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# yaml-language-server: $schema=../../../../configuration-schema.json
2+
package: api
3+
output: ping.gen.go
4+
generate:
5+
models: true
6+
std-http-server: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package api
2+
3+
//go:generate go tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml ../../api.yaml
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
)
7+
8+
// ensure that we've conformed to the `ServerInterface` with a compile-time check
9+
var _ ServerInterface = (*Server)(nil)
10+
11+
type Server struct{}
12+
13+
func NewServer() Server {
14+
return Server{}
15+
}
16+
17+
// (GET /ping)
18+
func (Server) GetPing(w http.ResponseWriter, r *http.Request) {
19+
resp := Pong{
20+
Ping: "pong",
21+
}
22+
23+
w.WriteHeader(http.StatusOK)
24+
_ = json.NewEncoder(w).Encode(resp)
25+
}

examples/minimal-server/stdhttp-go-tool/api/ping.gen.go

Lines changed: 171 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions

0 commit comments

Comments
 (0)