|
1 | 1 | package mock |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | 4 | "fmt" |
6 | | - "sync" |
7 | | - "testing" |
| 5 | + "strings" |
8 | 6 |
|
9 | 7 | "google.golang.org/protobuf/proto" |
10 | 8 | "google.golang.org/protobuf/reflect/protodesc" |
11 | | - "google.golang.org/protobuf/reflect/protoreflect" |
12 | 9 | "google.golang.org/protobuf/reflect/protoregistry" |
13 | 10 | "google.golang.org/protobuf/types/descriptorpb" |
14 | | - "google.golang.org/protobuf/types/dynamicpb" |
| 11 | + "google.golang.org/protobuf/types/known/wrapperspb" |
15 | 12 | ) |
16 | 13 |
|
17 | | -// ServerMethodName is the method supported by the mock package. |
18 | | -const ServerMethodName = "mock.Server.Test" |
19 | | - |
20 | | -var ( |
21 | | - // Mock Service Descriptors |
22 | | - mockFile *descriptorpb.FileDescriptorProto |
23 | | - requestType protoreflect.MessageType |
24 | | - responseType protoreflect.MessageType |
| 14 | +// TestMethod and GetValueMethod are the methods supported by the mock package. |
| 15 | +const ( |
| 16 | + TestMethod = "mock.MockService.Test" |
| 17 | + GetValueMethod = "mock.MockService.GetValue" |
25 | 18 | ) |
26 | 19 |
|
27 | | -func init() { |
28 | | - // Initialize Mock Definitions |
29 | | - mockFile = &descriptorpb.FileDescriptorProto{ |
30 | | - Name: proto.String("mock/mock.proto"), |
31 | | - Package: proto.String("mock"), |
32 | | - MessageType: []*descriptorpb.DescriptorProto{ |
33 | | - { |
34 | | - Name: proto.String("Request"), |
35 | | - Field: []*descriptorpb.FieldDescriptorProto{ |
36 | | - { |
37 | | - Name: proto.String("val"), |
38 | | - Number: proto.Int32(1), |
39 | | - Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(), |
40 | | - Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(), |
41 | | - }, |
42 | | - }, |
43 | | - }, |
44 | | - { |
45 | | - Name: proto.String("Response"), |
46 | | - Field: []*descriptorpb.FieldDescriptorProto{ |
47 | | - { |
48 | | - Name: proto.String("val"), |
49 | | - Number: proto.Int32(1), |
50 | | - Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(), |
51 | | - Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(), |
52 | | - }, |
53 | | - }, |
54 | | - }, |
55 | | - }, |
56 | | - Service: []*descriptorpb.ServiceDescriptorProto{ |
57 | | - { |
58 | | - Name: proto.String("Server"), |
59 | | - Method: []*descriptorpb.MethodDescriptorProto{ |
60 | | - { |
61 | | - Name: proto.String("Test"), |
62 | | - InputType: proto.String(".mock.Request"), |
63 | | - OutputType: proto.String(".mock.Response"), |
64 | | - }, |
65 | | - }, |
66 | | - }, |
67 | | - }, |
68 | | - } |
| 20 | +// Service represents a service to be registered. |
| 21 | +type Service struct { |
| 22 | + Name string // Full package and service name, e.g., "mock.MockService" |
| 23 | + Methods []Method |
69 | 24 | } |
70 | 25 |
|
71 | | -// Register registers the mock types in the global registry. |
72 | | -// It is safe to call multiple times. |
73 | | -func Register(t testing.TB) { |
74 | | - t.Helper() |
75 | | - if err := registerOnce(); err != nil { |
76 | | - t.Fatal(err) |
77 | | - } |
| 26 | +// Method represents a method in a service. |
| 27 | +type Method struct { |
| 28 | + Name string |
| 29 | + Input proto.Message |
| 30 | + Output proto.Message |
78 | 31 | } |
79 | 32 |
|
80 | | -var registerOnce = sync.OnceValue(func() error { |
81 | | - if fd, err := protoregistry.GlobalFiles.FindFileByPath(mockFile.GetName()); err == nil { |
82 | | - // Already registered |
83 | | - return initDescriptors(fd) |
84 | | - } |
| 33 | +// RegisterServices registers the given services in the global registry. |
| 34 | +// It is safe to call multiple times, but services with the same package name |
| 35 | +// must be registered in the same call or be identical to previous registrations. |
| 36 | +// Returns an error if registration fails. |
| 37 | +func RegisterServices(services []Service) error { |
| 38 | + // Group by package |
| 39 | + packages := make(map[string][]*descriptorpb.ServiceDescriptorProto) |
85 | 40 |
|
86 | | - fd, err := protodesc.NewFile(mockFile, nil) |
87 | | - if err != nil { |
88 | | - return fmt.Errorf("failed to create mock file descriptor: %v", err) |
89 | | - } |
90 | | - if err := protoregistry.GlobalFiles.RegisterFile(fd); err != nil { |
91 | | - return fmt.Errorf("failed to register mock file: %v", err) |
92 | | - } |
93 | | - if err := initDescriptors(fd); err != nil { |
94 | | - return fmt.Errorf("failed to initialize mock descriptors: %v", err) |
95 | | - } |
96 | | - if err := protoregistry.GlobalTypes.RegisterMessage(requestType); err != nil { |
97 | | - return fmt.Errorf("failed to register Request type: %v", err) |
98 | | - } |
99 | | - if err := protoregistry.GlobalTypes.RegisterMessage(responseType); err != nil { |
100 | | - return fmt.Errorf("failed to register Response type: %v", err) |
101 | | - } |
102 | | - return nil |
103 | | -}) |
| 41 | + for _, s := range services { |
| 42 | + pkgName, svcName, found := strings.Cut(s.Name, ".") |
| 43 | + if !found { |
| 44 | + return fmt.Errorf("service name %q must contain a package", s.Name) |
| 45 | + } |
104 | 46 |
|
105 | | -func initDescriptors(fd protoreflect.FileDescriptor) error { |
106 | | - mockServiceDesc := fd.Services().ByName("Server") |
107 | | - if mockServiceDesc == nil { |
108 | | - return errors.New("service Server not found") |
109 | | - } |
110 | | - mockMethodDesc := mockServiceDesc.Methods().ByName("Test") |
111 | | - if mockMethodDesc == nil { |
112 | | - return errors.New("method Test not found") |
113 | | - } |
114 | | - requestMsgDesc := fd.Messages().ByName("Request") |
115 | | - if requestMsgDesc == nil { |
116 | | - return errors.New("message Request not found") |
117 | | - } |
118 | | - responseMsgDesc := fd.Messages().ByName("Response") |
119 | | - if responseMsgDesc == nil { |
120 | | - return errors.New("message Response not found") |
| 47 | + svcDesc := &descriptorpb.ServiceDescriptorProto{ |
| 48 | + Name: proto.String(svcName), |
| 49 | + } |
| 50 | + |
| 51 | + for _, m := range s.Methods { |
| 52 | + inDesc := m.Input.ProtoReflect().Descriptor() |
| 53 | + outDesc := m.Output.ProtoReflect().Descriptor() |
| 54 | + inName := string(inDesc.FullName()) |
| 55 | + outName := string(outDesc.FullName()) |
| 56 | + |
| 57 | + svcDesc.Method = append(svcDesc.Method, &descriptorpb.MethodDescriptorProto{ |
| 58 | + Name: proto.String(m.Name), |
| 59 | + InputType: proto.String("." + inName), |
| 60 | + OutputType: proto.String("." + outName), |
| 61 | + }) |
| 62 | + } |
| 63 | + packages[pkgName] = append(packages[pkgName], svcDesc) |
121 | 64 | } |
122 | | - requestType = dynamicpb.NewMessageType(requestMsgDesc) |
123 | | - responseType = dynamicpb.NewMessageType(responseMsgDesc) |
124 | | - return nil |
125 | | -} |
126 | 65 |
|
127 | | -// Helpers for Mock messages |
| 66 | + for pkg, svcDescriptors := range packages { |
| 67 | + // Collect dependencies |
| 68 | + deps := make(map[string]struct{}) |
128 | 69 |
|
129 | | -const panicMsg = "mock.Register() must be called before using NewRequest/NewResponse" |
| 70 | + // Iterate over the original services to find dependencies for this package. |
| 71 | + for _, s := range services { |
| 72 | + pName, _, found := strings.Cut(s.Name, ".") |
| 73 | + if !found || pName != pkg { |
| 74 | + continue |
| 75 | + } |
130 | 76 |
|
131 | | -func NewRequest(val string) proto.Message { |
132 | | - if requestType == nil { |
133 | | - panic(panicMsg) |
134 | | - } |
135 | | - msg := requestType.New() |
136 | | - if val != "" { |
137 | | - fd := msg.Descriptor().Fields().ByName("val") |
138 | | - if fd != nil { |
139 | | - msg.Set(fd, protoreflect.ValueOfString(val)) |
| 77 | + for _, m := range s.Methods { |
| 78 | + if d := m.Input.ProtoReflect().Descriptor().ParentFile(); d != nil { |
| 79 | + deps[d.Path()] = struct{}{} |
| 80 | + } |
| 81 | + if d := m.Output.ProtoReflect().Descriptor().ParentFile(); d != nil { |
| 82 | + deps[d.Path()] = struct{}{} |
| 83 | + } |
| 84 | + } |
140 | 85 | } |
141 | | - } |
142 | | - return msg.Interface() |
143 | | -} |
144 | 86 |
|
145 | | -func NewResponse(val string) proto.Message { |
146 | | - if responseType == nil { |
147 | | - panic(panicMsg) |
148 | | - } |
149 | | - msg := responseType.New() |
150 | | - if val != "" { |
151 | | - fd := msg.Descriptor().Fields().ByName("val") |
152 | | - if fd != nil { |
153 | | - msg.Set(fd, protoreflect.ValueOfString(val)) |
| 87 | + fd := &descriptorpb.FileDescriptorProto{ |
| 88 | + Name: proto.String(fmt.Sprintf("mock/%s.proto", pkg)), |
| 89 | + Package: proto.String(pkg), |
| 90 | + Service: svcDescriptors, |
| 91 | + } |
| 92 | + |
| 93 | + for dep := range deps { |
| 94 | + fd.Dependency = append(fd.Dependency, dep) |
| 95 | + } |
| 96 | + |
| 97 | + // Check if already registered |
| 98 | + if _, err := protoregistry.GlobalFiles.FindFileByPath(fd.GetName()); err == nil { |
| 99 | + continue // Already registered |
| 100 | + } |
| 101 | + |
| 102 | + fileDesc, err := protodesc.NewFile(fd, protoregistry.GlobalFiles) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("failed to create file descriptor for %s: %v", pkg, err) |
| 105 | + } |
| 106 | + |
| 107 | + if err := protoregistry.GlobalFiles.RegisterFile(fileDesc); err != nil { |
| 108 | + return fmt.Errorf("failed to register file %s: %v", pkg, err) |
154 | 109 | } |
155 | 110 | } |
156 | | - return msg.Interface() |
| 111 | + return nil |
157 | 112 | } |
158 | 113 |
|
| 114 | +// Helpers for Mock messages |
| 115 | + |
159 | 116 | func GetVal(msg proto.Message) string { |
160 | 117 | if msg == nil { |
161 | 118 | return "" |
162 | 119 | } |
163 | | - m := msg.ProtoReflect() |
164 | | - if !m.IsValid() { |
165 | | - return "" |
166 | | - } |
167 | | - fd := m.Descriptor().Fields().ByName("val") |
168 | | - if fd == nil { |
169 | | - return "" |
| 120 | + if m, ok := msg.(*wrapperspb.StringValue); ok { |
| 121 | + return m.Value |
170 | 122 | } |
171 | | - return m.Get(fd).String() |
| 123 | + return "" |
172 | 124 | } |
173 | 125 |
|
174 | 126 | func SetVal(msg proto.Message, val string) { |
175 | 127 | if msg == nil { |
176 | 128 | return |
177 | 129 | } |
178 | | - m := msg.ProtoReflect() |
179 | | - if !m.IsValid() { |
180 | | - return |
181 | | - } |
182 | | - fd := m.Descriptor().Fields().ByName("val") |
183 | | - if fd == nil { |
184 | | - return |
| 130 | + if m, ok := msg.(*wrapperspb.StringValue); ok { |
| 131 | + m.Value = val |
185 | 132 | } |
186 | | - m.Set(fd, protoreflect.ValueOfString(val)) |
187 | 133 | } |
0 commit comments