serverEncodeDecode returns the file defining the gRPC server encoding and decoding logic.
(genpkg string, svc *expr.GRPCServiceExpr, services *ServicesData)
| 124 | // serverEncodeDecode returns the file defining the gRPC server encoding and |
| 125 | // decoding logic. |
| 126 | func serverEncodeDecode(genpkg string, svc *expr.GRPCServiceExpr, services *ServicesData) *codegen.File { |
| 127 | var ( |
| 128 | fpath string |
| 129 | sections []*codegen.SectionTemplate |
| 130 | |
| 131 | data = services.Get(svc.Name()) |
| 132 | ) |
| 133 | { |
| 134 | svcName := data.Service.PathName |
| 135 | fpath = filepath.Join(codegen.Gendir, "grpc", svcName, "server", "encode_decode.go") |
| 136 | title := fmt.Sprintf("%s gRPC server encoders and decoders", svc.Name()) |
| 137 | imports := []*codegen.ImportSpec{ |
| 138 | {Path: "context"}, |
| 139 | {Path: "strings"}, |
| 140 | {Path: "strconv"}, |
| 141 | {Path: "unicode/utf8"}, |
| 142 | {Path: "google.golang.org/grpc"}, |
| 143 | {Path: "google.golang.org/grpc/metadata"}, |
| 144 | codegen.GoaImport(""), |
| 145 | codegen.GoaNamedImport("grpc", "goagrpc"), |
| 146 | {Path: path.Join(genpkg, svcName), Name: data.Service.PkgName}, |
| 147 | {Path: path.Join(genpkg, svcName, "views"), Name: data.Service.ViewsPkg}, |
| 148 | {Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: data.PkgName}, |
| 149 | } |
| 150 | sections = []*codegen.SectionTemplate{codegen.Header(title, "server", imports)} |
| 151 | |
| 152 | for _, e := range data.Endpoints { |
| 153 | if e.Response.ServerConvert != nil { |
| 154 | sections = append(sections, &codegen.SectionTemplate{ |
| 155 | Name: "response-encoder", |
| 156 | Source: grpcTemplates.Read(grpcResponseEncoderT, grpcConvertTypeToStringP, "string_conversion"), |
| 157 | Data: e, |
| 158 | FuncMap: map[string]any{ |
| 159 | "typeConversionData": typeConversionData, |
| 160 | "metadataEncodeDecodeData": metadataEncodeDecodeData, |
| 161 | }, |
| 162 | }) |
| 163 | } |
| 164 | if e.PayloadRef != "" { |
| 165 | fm := transTmplFuncs(svc, services) |
| 166 | fm["isEmpty"] = isEmpty |
| 167 | sections = append(sections, &codegen.SectionTemplate{ |
| 168 | Name: "request-decoder", |
| 169 | Source: grpcTemplates.Read(grpcRequestDecoderT, grpcConvertStringToTypeP, "type_conversion", "slice_conversion", "slice_item_conversion"), |
| 170 | Data: e, |
| 171 | FuncMap: fm, |
| 172 | }) |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | return &codegen.File{Path: fpath, SectionTemplates: sections} |
| 177 | } |
| 178 | |
| 179 | func transTmplFuncs(s *expr.GRPCServiceExpr, services *ServicesData) map[string]any { |
| 180 | return map[string]any{ |
no test coverage detected