From 9d3ebbea7d7013fbd35ac58ba30e1d1c9f17bb44 Mon Sep 17 00:00:00 2001 From: syumai Date: Sat, 24 Jun 2023 09:50:21 +0900 Subject: [PATCH] update simple-json-server example to use encoding/json --- _examples/simple-json-server/README.md | 2 - .../simple-json-server/app/app_easyjson.go | 151 ------------------ _examples/simple-json-server/app/hello.go | 38 ----- _examples/simple-json-server/go.mod | 7 +- _examples/simple-json-server/go.sum | 4 - _examples/simple-json-server/main.go | 32 +++- 6 files changed, 31 insertions(+), 203 deletions(-) delete mode 100644 _examples/simple-json-server/app/app_easyjson.go delete mode 100644 _examples/simple-json-server/app/hello.go diff --git a/_examples/simple-json-server/README.md b/_examples/simple-json-server/README.md index 6f5433b..2e5ca7e 100644 --- a/_examples/simple-json-server/README.md +++ b/_examples/simple-json-server/README.md @@ -32,8 +32,6 @@ This project requires these tools to be installed globally. * wrangler * tinygo -* [easyjson](https://github.com/mailru/easyjson) - - `go install github.com/mailru/easyjson/...@latest` ### Commands diff --git a/_examples/simple-json-server/app/app_easyjson.go b/_examples/simple-json-server/app/app_easyjson.go deleted file mode 100644 index 185bcfa..0000000 --- a/_examples/simple-json-server/app/app_easyjson.go +++ /dev/null @@ -1,151 +0,0 @@ -// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT. - -package app - -import ( - json "encoding/json" - easyjson "github.com/mailru/easyjson" - jlexer "github.com/mailru/easyjson/jlexer" - jwriter "github.com/mailru/easyjson/jwriter" -) - -// suppress unused package warning -var ( - _ *json.RawMessage - _ *jlexer.Lexer - _ *jwriter.Writer - _ easyjson.Marshaler -) - -func easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(in *jlexer.Lexer, out *HelloResponse) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeFieldName(false) - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "message": - out.Message = string(in.String()) - default: - in.SkipRecursive() - } - in.WantComma() - } - in.Delim('}') - if isTopLevel { - in.Consumed() - } -} -func easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(out *jwriter.Writer, in HelloResponse) { - out.RawByte('{') - first := true - _ = first - { - const prefix string = ",\"message\":" - out.RawString(prefix[1:]) - out.String(string(in.Message)) - } - out.RawByte('}') -} - -// MarshalJSON supports json.Marshaler interface -func (v HelloResponse) MarshalJSON() ([]byte, error) { - w := jwriter.Writer{} - easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(&w, v) - return w.Buffer.BuildBytes(), w.Error -} - -// MarshalEasyJSON supports easyjson.Marshaler interface -func (v HelloResponse) MarshalEasyJSON(w *jwriter.Writer) { - easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(w, v) -} - -// UnmarshalJSON supports json.Unmarshaler interface -func (v *HelloResponse) UnmarshalJSON(data []byte) error { - r := jlexer.Lexer{Data: data} - easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(&r, v) - return r.Error() -} - -// UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *HelloResponse) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp(l, v) -} -func easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(in *jlexer.Lexer, out *HelloRequest) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeFieldName(false) - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "name": - out.Name = string(in.String()) - default: - in.SkipRecursive() - } - in.WantComma() - } - in.Delim('}') - if isTopLevel { - in.Consumed() - } -} -func easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(out *jwriter.Writer, in HelloRequest) { - out.RawByte('{') - first := true - _ = first - { - const prefix string = ",\"name\":" - out.RawString(prefix[1:]) - out.String(string(in.Name)) - } - out.RawByte('}') -} - -// MarshalJSON supports json.Marshaler interface -func (v HelloRequest) MarshalJSON() ([]byte, error) { - w := jwriter.Writer{} - easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(&w, v) - return w.Buffer.BuildBytes(), w.Error -} - -// MarshalEasyJSON supports easyjson.Marshaler interface -func (v HelloRequest) MarshalEasyJSON(w *jwriter.Writer) { - easyjsonD2c14bEncodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(w, v) -} - -// UnmarshalJSON supports json.Unmarshaler interface -func (v *HelloRequest) UnmarshalJSON(data []byte) error { - r := jlexer.Lexer{Data: data} - easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(&r, v) - return r.Error() -} - -// UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *HelloRequest) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjsonD2c14bDecodeGithubComSyumaiWorkersExamplesSimpleJsonServerApp1(l, v) -} diff --git a/_examples/simple-json-server/app/hello.go b/_examples/simple-json-server/app/hello.go deleted file mode 100644 index 8ae3ab0..0000000 --- a/_examples/simple-json-server/app/hello.go +++ /dev/null @@ -1,38 +0,0 @@ -//go:generate easyjson . -package app - -import ( - "fmt" - "net/http" - "os" - - "github.com/mailru/easyjson" -) - -//easyjson:json -type HelloRequest struct { - Name string `json:"name"` -} - -//easyjson:json -type HelloResponse struct { - Message string `json:"message"` -} - -func HelloHandler(w http.ResponseWriter, req *http.Request) { - var helloReq HelloRequest - if err := easyjson.UnmarshalFromReader(req.Body, &helloReq); err != nil { - w.WriteHeader(http.StatusBadRequest) - w.Header().Set("Content-Type", "text/plain") - w.Write([]byte("request format is invalid")) - return - } - - w.Header().Set("Content-Type", "application/json") - msg := fmt.Sprintf("Hello, %s!", helloReq.Name) - helloRes := HelloResponse{Message: msg} - - if _, err := easyjson.MarshalToWriter(&helloRes, w); err != nil { - fmt.Fprintf(os.Stderr, "failed to encode response: %w\n", err) - } -} diff --git a/_examples/simple-json-server/go.mod b/_examples/simple-json-server/go.mod index 2073dc6..0edd525 100644 --- a/_examples/simple-json-server/go.mod +++ b/_examples/simple-json-server/go.mod @@ -2,11 +2,6 @@ module github.com/syumai/workers/_examples/simple-json-server go 1.18 -require ( - github.com/mailru/easyjson v0.7.7 - github.com/syumai/workers v0.0.0-00010101000000-000000000000 -) +require github.com/syumai/workers v0.0.0-00010101000000-000000000000 replace github.com/syumai/workers => ../../ - -require github.com/josharian/intern v1.0.0 // indirect diff --git a/_examples/simple-json-server/go.sum b/_examples/simple-json-server/go.sum index 7707cb6..e69de29 100644 --- a/_examples/simple-json-server/go.sum +++ b/_examples/simple-json-server/go.sum @@ -1,4 +0,0 @@ -github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= -github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= -github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= -github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= diff --git a/_examples/simple-json-server/main.go b/_examples/simple-json-server/main.go index a69c015..441027c 100644 --- a/_examples/simple-json-server/main.go +++ b/_examples/simple-json-server/main.go @@ -1,13 +1,41 @@ package main import ( + "encoding/json" + "fmt" "net/http" + "os" "github.com/syumai/workers" - "github.com/syumai/workers/examples/simple-json-server/app" ) +type HelloRequest struct { + Name string `json:"name"` +} + +type HelloResponse struct { + Message string `json:"message"` +} + +func HelloHandler(w http.ResponseWriter, req *http.Request) { + var helloReq HelloRequest + if err := json.NewDecoder(req.Body).Decode(&helloReq); err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("request format is invalid")) + return + } + + w.Header().Set("Content-Type", "application/json") + msg := fmt.Sprintf("Hello, %s!", helloReq.Name) + + helloRes := HelloResponse{Message: msg} + if err := json.NewEncoder(w).Encode(helloRes); err != nil { + fmt.Fprintf(os.Stderr, "failed to encode response: %w\n", err) + } +} + func main() { - http.HandleFunc("/hello", app.HelloHandler) + http.HandleFunc("/hello", HelloHandler) workers.Serve(nil) // use http.DefaultServeMux }