protobuf.go
This file
Path
binding/protobuf.go
41 lines · 923 bytes · Go
Last changed by
Not known.
History of this fileTake it
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package binding67import (8 "errors"9 "io"10 "net/http"1112 "google.golang.org/protobuf/proto"13)1415type protobufBinding struct{}1617func (protobufBinding) Name() string {18 return "protobuf"19}2021func (b protobufBinding) Bind(req *http.Request, obj any) error {22 buf, err := io.ReadAll(req.Body)23 if err != nil {24 return err25 }26 return b.BindBody(buf, obj)27}2829func (protobufBinding) BindBody(body []byte, obj any) error {30 msg, ok := obj.(proto.Message)31 if !ok {32 return errors.New("obj is not ProtoMessage")33 }34 if err := proto.Unmarshal(body, msg); err != nil {35 return err36 }37 // Here it's same to return validate(obj), but until now we can't add38 // `binding:""` to the struct which automatically generate by gen-proto39 return nil40 // return validate(obj)41}