-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmessage_type.go
More file actions
111 lines (96 loc) · 3.41 KB
/
message_type.go
File metadata and controls
111 lines (96 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hyperpb
import (
"fmt"
"slices"
_ "unsafe"
"google.golang.org/protobuf/reflect/protoreflect"
"buf.build/go/hyperpb/internal/tdp"
"buf.build/go/hyperpb/internal/tdp/empty"
"buf.build/go/hyperpb/internal/tdp/profile"
"buf.build/go/hyperpb/internal/xunsafe"
)
// MessageType implements [protoreflect.MessageType].
//
// To obtain an optimized [MessageType], use any of the Compile* functions.
type MessageType struct {
impl tdp.Type
}
// Profile can be used to profile messages associated with the same
// collection of types. It can later be used to re-compile the original types
// to be more efficient.
//
// Profile itself is an opaque pointer; it only exists to be passed into
// different calls to [WithProfile].
//
// See [MessageType.NewProfile].
type Profile struct {
impl profile.Recorder
}
// Descriptor returns the message descriptor.
//
// Descriptor implements [protoreflect.MessageType].
func (t *MessageType) Descriptor() protoreflect.MessageDescriptor {
if t == nil {
return nil
}
return t.impl.Descriptor
}
// New returns a newly allocated empty message.
// It may return nil for synthetic messages representing a map entry.
//
// New implements [protoreflect.MessageType].
func (t *MessageType) New() protoreflect.Message {
return new(Shared).NewMessage(t).ProtoReflect()
}
// Zero returns an empty, read-only message.
// It may return nil for synthetic messages representing a map entry.
//
// Zero implements [protoreflect.MessageType].
func (t *MessageType) Zero() protoreflect.Message {
return empty.NewMessage(&t.impl)
}
// Format implements [fmt.Formatter].
func (t *MessageType) Format(f fmt.State, verb rune) {
if f.Flag('#') {
fmt.Fprintf(f, fmt.FormatString(f, verb), t.Descriptor())
} else {
fmt.Fprint(f, t.Descriptor().FullName())
}
}
// NewProfile creates a new profiler for this type, which can be used to
// profile messages of this type when unmarshaling.
//
// The returned profiler cannot be used with messages of other types.
func (t *MessageType) NewProfile() *Profile {
return xunsafe.Cast[Profile](profile.NewRecorder(t.impl.Library))
}
// Recompile recompiles this type with a recorded profile.
//
// Note that this profile cannot be used with the new type; you must create a
// fresh profile using [MessageType.NewProfile] and begin recording anew.
func (t *MessageType) Recompile(profile *Profile) *MessageType {
options := slices.Clone(t.impl.Library.Metadata.([]CompileOption)) //nolint:errcheck
options = append(options, WithProfile(profile))
return CompileMessageDescriptor(t.Descriptor(), options...)
}
// wrapType wraps an internal Type pointer.
func wrapType(s *tdp.Type) *MessageType {
return xunsafe.Cast[MessageType](s)
}
//go:linkname protoReflectType buf.build/go/hyperpb/internal/tdp.hyperpb_ProtoReflect
func protoReflectType(m *tdp.Type) protoreflect.MessageType {
return wrapType(m)
}