2018-08-19 05:45:56 +03:00
|
|
|
// Copyright 2018 Gin Core Team. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-08-19 10:46:31 +03:00
|
|
|
"google.golang.org/protobuf/proto"
|
2018-08-19 05:45:56 +03:00
|
|
|
)
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// ProtoBuf contains the given interface object.
|
2018-08-19 05:45:56 +03:00
|
|
|
type ProtoBuf struct {
|
2022-03-21 04:43:17 +03:00
|
|
|
Data any
|
2018-08-19 05:45:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var protobufContentType = []string{"application/x-protobuf"}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// Render (ProtoBuf) marshals the given interface object and writes data with custom ContentType.
|
2018-08-19 05:45:56 +03:00
|
|
|
func (r ProtoBuf) Render(w http.ResponseWriter) error {
|
|
|
|
r.WriteContentType(w)
|
|
|
|
|
2018-08-19 12:39:58 +03:00
|
|
|
bytes, err := proto.Marshal(r.Data.(proto.Message))
|
2018-08-19 05:45:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-18 04:32:53 +03:00
|
|
|
_, err = w.Write(bytes)
|
|
|
|
return err
|
2018-08-19 05:45:56 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// WriteContentType (ProtoBuf) writes ProtoBuf ContentType.
|
2018-08-19 05:45:56 +03:00
|
|
|
func (r ProtoBuf) WriteContentType(w http.ResponseWriter) {
|
|
|
|
writeContentType(w, protobufContentType)
|
|
|
|
}
|