Skip to content

Commit

Permalink
docs: Disable Go reformatting (#1000)
Browse files Browse the repository at this point in the history
(This is a new version of #961. I can't push to the original branch
anymore.)

---

Upgrade to latest markdownfmt
which does not reformat Go source code blocks by default.

With this change, formatting of code blocks in Fx docs will be
consistent
instead of some being gofmt-ed to use tabs,
and others using 2 spaces (per our 'region' script).

This is a landable version of #961 without any temporary version pins.
  • Loading branch information
abhinav authored Dec 12, 2022
1 parent fa108cc commit 93a9a6d
Show file tree
Hide file tree
Showing 16 changed files with 326 additions and 205 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ coverage.txt
/.bin
/.cache
/bin
.vscode
.vscode
.mdoxcache
2 changes: 1 addition & 1 deletion docs/annotate.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ into an interface consumed by another function.
```go mdox-exec='region ex/annotate/cast.go interface'
type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
Do(*http.Request) (*http.Response, error)
}

// This is a compile-time check that verifies
Expand Down
38 changes: 19 additions & 19 deletions docs/get-started/another-handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@ Let's add another.
// HelloHandler is an HTTP handler that
// prints a greeting to the user.
type HelloHandler struct {
log *zap.Logger
log *zap.Logger
}

// NewHelloHandler builds a new HelloHandler.
func NewHelloHandler(log *zap.Logger) *HelloHandler {
return &HelloHandler{log: log}
return &HelloHandler{log: log}
}
```

2. Implement the `Route` interface for this handler.

```go mdox-exec='region ex/get-started/06-another-handler/main.go hello-methods'
func (*HelloHandler) Pattern() string {
return "/hello"
return "/hello"
}

func (h *HelloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
h.log.Error("Failed to read request", zap.Error(err))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}

if _, err := fmt.Fprintf(w, "Hello, %s\n", body); err != nil {
h.log.Error("Failed to write response", zap.Error(err))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
h.log.Error("Failed to read request", zap.Error(err))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}

if _, err := fmt.Fprintf(w, "Hello, %s\n", body); err != nil {
h.log.Error("Failed to write response", zap.Error(err))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}
```

Expand Down Expand Up @@ -103,10 +103,10 @@ Let's add another.
// NewServeMux builds a ServeMux that will route requests
// to the given routes.
func NewServeMux(route1, route2 Route) *http.ServeMux {
mux := http.NewServeMux()
mux.Handle(route1.Pattern(), route1)
mux.Handle(route2.Pattern(), route2)
return mux
mux := http.NewServeMux()
mux.Handle(route1.Pattern(), route1)
mux.Handle(route2.Pattern(), route2)
return mux
}
```

Expand Down
14 changes: 7 additions & 7 deletions docs/get-started/echo-handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ Let's fix that.

// NewEchoHandler builds a new EchoHandler.
func NewEchoHandler() *EchoHandler {
return &EchoHandler{}
return &EchoHandler{}
}

// ServeHTTP handles an HTTP request to the /echo endpoint.
func (*EchoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if _, err := io.Copy(w, r.Body); err != nil {
fmt.Fprintln(os.Stderr, "Failed to handle request:", err)
}
if _, err := io.Copy(w, r.Body); err != nil {
fmt.Fprintln(os.Stderr, "Failed to handle request:", err)
}
}
```

Expand All @@ -46,9 +46,9 @@ Let's fix that.
// NewServeMux builds a ServeMux that will route requests
// to the given EchoHandler.
func NewServeMux(echo *EchoHandler) *http.ServeMux {
mux := http.NewServeMux()
mux.Handle("/echo", echo)
return mux
mux := http.NewServeMux()
mux.Handle("/echo", echo)
return mux
}
```

Expand Down
50 changes: 25 additions & 25 deletions docs/get-started/http-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Let's add an HTTP server to it.
// NewHTTPServer builds an HTTP server that will begin serving requests
// when the Fx application starts.
func NewHTTPServer(lc fx.Lifecycle) *http.Server {
srv := &http.Server{Addr: ":8080"}
return srv
srv := &http.Server{Addr: ":8080"}
return srv
}
```

Expand All @@ -23,32 +23,32 @@ Let's add an HTTP server to it.

```go mdox-exec='region ex/get-started/02-http-server/main.go full'
func NewHTTPServer(lc fx.Lifecycle) *http.Server {
srv := &http.Server{Addr: ":8080"}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
ln, err := net.Listen("tcp", srv.Addr)
if err != nil {
return err
}
fmt.Println("Starting HTTP server at", srv.Addr)
go srv.Serve(ln)
return nil
},
OnStop: func(ctx context.Context) error {
return srv.Shutdown(ctx)
},
})
return srv
srv := &http.Server{Addr: ":8080"}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
ln, err := net.Listen("tcp", srv.Addr)
if err != nil {
return err
}
fmt.Println("Starting HTTP server at", srv.Addr)
go srv.Serve(ln)
return nil
},
OnStop: func(ctx context.Context) error {
return srv.Shutdown(ctx)
},
})
return srv
}
```

3. Provide this to your Fx application above with `fx.Provide`.

```go mdox-exec='region ex/get-started/02-http-server/main.go provide-server'
func main() {
fx.New(
fx.Provide(NewHTTPServer),
).Run()
fx.New(
fx.Provide(NewHTTPServer),
).Run()
}
```

Expand All @@ -70,10 +70,10 @@ Let's add an HTTP server to it.
5. To fix that, add an `fx.Invoke` that requests the constructed server.

```go mdox-exec='region ex/get-started/02-http-server/main.go app'
fx.New(
fx.Provide(NewHTTPServer),
fx.Invoke(func(*http.Server) {}),
).Run()
fx.New(
fx.Provide(NewHTTPServer),
fx.Invoke(func(*http.Server) {}),
).Run()
```

6. Run the application again.
Expand Down
10 changes: 5 additions & 5 deletions docs/get-started/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ but you should be able to use any logging system.

```go mdox-exec='region ex/get-started/04-logger/main.go echo-init'
type EchoHandler struct {
log *zap.Logger
log *zap.Logger
}

func NewEchoHandler(log *zap.Logger) *EchoHandler {
return &EchoHandler{log: log}
return &EchoHandler{log: log}
}
```

Expand All @@ -41,9 +41,9 @@ but you should be able to use any logging system.

```go mdox-exec='region ex/get-started/04-logger/main.go echo-serve'
func (h *EchoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if _, err := io.Copy(w, r.Body); err != nil {
h.log.Warn("Failed to handle request", zap.Error(err))
}
if _, err := io.Copy(w, r.Body); err != nil {
h.log.Warn("Failed to handle request", zap.Error(err))
}
}
```

Expand Down
20 changes: 10 additions & 10 deletions docs/get-started/many-handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ Let's do that.

```go mdox-exec='region ex/get-started/07-many-handlers/main.go mux'
func NewServeMux(routes []Route) *http.ServeMux {
mux := http.NewServeMux()
for _, route := range routes {
mux.Handle(route.Pattern(), route)
}
return mux
mux := http.NewServeMux()
for _, route := range routes {
mux.Handle(route.Pattern(), route)
}
return mux
}
```

Expand All @@ -40,11 +40,11 @@ Let's do that.
// AsRoute annotates the given constructor to state that
// it provides a route to the "routes" group.
func AsRoute(f any) any {
return fx.Annotate(
f,
fx.As(new(Route)),
fx.ResultTags(`group:"routes"`),
)
return fx.Annotate(
f,
fx.As(new(Route)),
fx.ResultTags(`group:"routes"`),
)
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/get-started/minimal.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This application won't do anything yet except print a bunch of logs.
import "go.uber.org/fx"

func main() {
fx.New().Run()
fx.New().Run()
}
```

Expand Down
14 changes: 7 additions & 7 deletions docs/get-started/registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ Let's try to fix this.
// Route is an http.Handler that knows the mux pattern
// under which it will be registered.
type Route interface {
http.Handler
http.Handler

// Pattern reports the path at which this is registered.
Pattern() string
// Pattern reports the path at which this is registered.
Pattern() string
}
```

2. Modify `EchoHandler` to implement this interface.

```go mdox-exec='region ex/get-started/05-registration/main.go echo-pattern'
func (*EchoHandler) Pattern() string {
return "/echo"
return "/echo"
}
```

Expand All @@ -52,9 +52,9 @@ Let's try to fix this.
// NewServeMux builds a ServeMux that will route requests
// to the given Route.
func NewServeMux(route Route) *http.ServeMux {
mux := http.NewServeMux()
mux.Handle(route.Pattern(), route)
return mux
mux := http.NewServeMux()
mux.Handle(route.Pattern(), route)
return mux
}
```

Expand Down
31 changes: 16 additions & 15 deletions docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ To write an Fx module:

```go mdox-exec='region ex/modules/module.go provide'
var Module = fx.Module("server",
fx.Provide(
New,
parseConfig,
),
fx.Provide(
New,
parseConfig,
),
)
```

Expand All @@ -34,11 +34,11 @@ To write an Fx module:

```go mdox-exec='region ex/modules/module.go invoke'
var Module = fx.Module("server",
fx.Provide(
New,
parseConfig,
),
fx.Invoke(startServer),
fx.Provide(
New,
parseConfig,
),
fx.Invoke(startServer),
)
```

Expand All @@ -47,12 +47,13 @@ To write an Fx module:

```go mdox-exec='region ex/modules/module.go decorate'
var Module = fx.Module("server",
fx.Provide(
New,
parseConfig,
),
fx.Invoke(startServer),
fx.Decorate(wrapLogger),
fx.Provide(
New,
parseConfig,
),
fx.Invoke(startServer),
fx.Decorate(wrapLogger),

)
```

Expand Down
14 changes: 7 additions & 7 deletions docs/parameter-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ To use parameter objects in Fx, take the following steps:
```go mdox-exec='region ex/parameter-objects/define.go fields'
type ClientParams struct {
fx.In
fx.In

Config ClientConfig
HTTPClient *http.Client
Config ClientConfig
HTTPClient *http.Client
}
```
Expand Down Expand Up @@ -100,11 +100,11 @@ the new fields must be **optional**.
```go mdox-exec='region ex/parameter-objects/extend.go full'
type Params struct {
fx.In
fx.In

Config ClientConfig
HTTPClient *http.Client
Logger *zap.Logger `optional:"true"`
Config ClientConfig
HTTPClient *http.Client
Logger *zap.Logger `optional:"true"`
}
```
Expand Down
Loading

0 comments on commit 93a9a6d

Please sign in to comment.