0

I want to start a simple grpc gateway:

import (
    "context"
    "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
    "google.golang.org/grpc"
    "log"
    "net/http"
    authpb "seekCar/auth/api/gen/v1" // the auth code generated by protobuf
)

func main() {
    c := context.Background()
    c, cancel := context.WithCancel(c)
    defer cancel()

    mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{
        EnumsAsInts: true, // Unknown field 'EnumsAsInts' in struct literal
        OrigName:    true, // Unknown field 'OrigName' in struct literal
    }))

    err := authpb.RegisterAuthServiceHandlerFromEndpoint(
        c,
        mux,
        "localhost:8081",
        []grpc.DialOption{grpc.WithInsecure()},
    )

    if err != nil {
        log.Fatalf("cannot register auth service: %v", err)
    }

    log.Fatal(http.ListenAndServe(":8080", mux))
}

But EnumsAsInts and OrigName are marked red by the compiler and an error is reported:

Unknown field 'EnumsAsInts' in struct literal
Unknown field 'OrigName' in struct literal

The go.mod is:

module learnGo

go 1.18

require (
    github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3
    go.uber.org/zap v1.16.0
    google.golang.org/grpc v1.49.0
    google.golang.org/protobuf v1.28.1
)

require (
    github.com/golang/protobuf v1.5.2 // indirect
    github.com/pkg/errors v0.9.1 // indirect
    go.uber.org/atomic v1.6.0 // indirect
    go.uber.org/multierr v1.5.0 // indirect
    golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect
    golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
    golang.org/x/text v0.3.7 // indirect
    google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 // indirect
    gopkg.in/yaml.v2 v2.4.0 // indirect
)

But jsonp/encode.go do have these two fields. enter image description here

What's wrong with my code?

Up Pu
  • 33
  • 4
  • You're not creating [`runtime.JSONPb`](https://pkg.go.dev/github.com/grpc-ecosystem/grpc-gateway/v2@v2.11.3/runtime#JSONPb) correctly. – DazWilkin Oct 10 '22 at 16:54
  • 1
    Back grpc-gateway from v2 to v1.16.0 works! Refer to this page:https://stackoverflow.com/questions/66133168/grpc-gateway-runtime-version-conflict – Criwran Oct 11 '22 at 01:53

0 Answers0