Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use FinshedBytes() in go-echo example instead of manually encoding offset #7660

Merged
merged 4 commits into from Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 2 additions & 13 deletions examples/go-echo/client/client.go
Expand Up @@ -16,13 +16,7 @@ func RequestBody() *bytes.Reader {
b := flatbuffers.NewBuilder(0)
r := net.RequestT{Player: &hero.WarriorT{Name: "Krull", Hp: 100}}
b.Finish(r.Pack(b))

// Encode builder head in last 4 bytes of request body
buf := make([]byte, 4)
flatbuffers.WriteUOffsetT(buf, b.Head())
buf = append(b.Bytes, buf...)

return bytes.NewReader(buf)
return bytes.NewReader(b.FinishedBytes())
}

func ReadResponse(r *http.Response) {
Expand All @@ -32,18 +26,13 @@ func ReadResponse(r *http.Response) {
return
}

// Last 4 bytes is offset.
off := flatbuffers.GetUOffsetT(body[len(body)-4:])
buf := body[:len(body) - 4]

res := net.GetRootAsResponse(buf, off)
res := net.GetRootAsResponse(body, 0)
player := res.Player(nil)

fmt.Printf("Got response (name: %v, hp: %v)\n", string(player.Name()), player.Hp())
}

func main() {

body := RequestBody()
req, err := http.NewRequest("POST", "http://localhost:8080/echo", body)
if err != nil {
Expand Down
8 changes: 1 addition & 7 deletions examples/go-echo/server/server.go
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"io/ioutil"
"net/http"

flatbuffers "github.com/google/flatbuffers/go"
)

func echo(w http.ResponseWriter, r *http.Request) {
Expand All @@ -16,11 +14,7 @@ func echo(w http.ResponseWriter, r *http.Request) {
return
}

// Last 4 bytes is offset. See client.go.
off := flatbuffers.GetUOffsetT(body[len(body)-4:])
buf := body[:len(body) - 4]

req := net.GetRootAsRequest(buf, off)
req := net.GetRootAsRequest(body, 0)
player := req.Player(nil)

fmt.Printf("Got request (name: %v, hp: %v)\n", string(player.Name()), player.Hp())
Expand Down