Skip to content

Commit 836f108

Browse files
committed
support null value
1 parent a85a173 commit 836f108

File tree

17 files changed

+1539
-1356
lines changed

17 files changed

+1539
-1356
lines changed

_examples/02_simple/federation/federation.pb.go

Lines changed: 397 additions & 347 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/02_simple/federation/federation_grpc_federation.pb.go

Lines changed: 128 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/02_simple/main_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,10 @@ func TestFederation(t *testing.T) {
311311
BoolWrapperValue: &wrapperspb.BoolValue{
312312
Value: true,
313313
},
314-
Hello: "hello\nworld",
314+
Hello: "hello\nworld",
315+
NullTimestamp: nil,
316+
NullTimestamp2: nil,
317+
NullTimestamp3: nil,
315318
}, cmpopts.IgnoreUnexported(
316319
federation.GetPostResponse{},
317320
federation.Post{},

_examples/02_simple/proto/federation/federation.proto

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package federation;
44

55
import "google/protobuf/any.proto";
66
import "google/protobuf/wrappers.proto";
7+
import "google/protobuf/timestamp.proto";
78
import "grpc/federation/federation.proto";
89
import "post/post.proto";
910
import "user/user.proto";
@@ -38,6 +39,7 @@ message GetPostResponse {
3839
def { name: "sorted_values" by: "[4, 1, 3, 2].sortAsc(v, v)" }
3940
def { name: "sorted_items" by: "[user.Item{location:user.Item.Location{addr1:'a'}}, user.Item{location:user.Item.Location{addr1:'b'}}].sortDesc(v, v.location.addr1)" }
4041
def { name: "map_value" by : "{1: 'a', 2: 'b', 3: 'c'}" }
42+
def { name: "null_value" by: "null" }
4143
};
4244
Post post = 1 [(grpc.federation.field).by = "post"];
4345
string str = 2 [(grpc.federation.field).string = "hello"];
@@ -66,6 +68,9 @@ message GetPostResponse {
6668
google.protobuf.StringValue string_wrapper_value = 25 [(grpc.federation.field).by = "google.protobuf.StringValue{value: 'hello'}"];
6769
google.protobuf.BytesValue bytes_wrapper_value = 26 [(grpc.federation.field).by = "google.protobuf.BytesValue{value: bytes('world')}"];
6870
string hello = 27 [(grpc.federation.field).by = "'hello\\nworld'"];
71+
google.protobuf.Timestamp null_timestamp = 28 [(grpc.federation.field).by = "null"];
72+
google.protobuf.Timestamp null_timestamp2 = 29 [(grpc.federation.field).by = "null_value"];
73+
google.protobuf.Timestamp null_timestamp3 = 30 [(grpc.federation.field).by = "true ? null : google.protobuf.Timestamp{}"];
6974
}
7075

7176
message A {

_examples/17_error_handler/grpc/federation/generator.pb.go

Lines changed: 493 additions & 483 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

generator/code_generator.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ func toCELTypeDeclare(t *resolver.Type) string {
813813
}
814814

815815
func (f *File) toTypeText(t *resolver.Type) string {
816-
if t == nil {
816+
if t == nil || t.IsNull {
817817
return "any"
818818
}
819819
if t.OneofField != nil {
@@ -2829,6 +2829,9 @@ func toCELNativeType(t *resolver.Type) string {
28292829
cloned.Repeated = false
28302830
return fmt.Sprintf("grpcfed.CELListType(%s)", toCELNativeType(cloned))
28312831
}
2832+
if t.IsNull {
2833+
return "grpcfed.CELNullType"
2834+
}
28322835
switch t.Kind {
28332836
case types.Double, types.Float:
28342837
return "grpcfed.CELDoubleType"

grpc/federation/alias.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var (
4444
CELObjectType = cel.ObjectType
4545
CELListType = cel.ListType
4646
CELMapType = cel.MapType
47+
CELNullType = cel.NullType
4748
NewCELListType = types.NewListType
4849
NewCELObjectType = types.NewObjectType
4950
)

grpc/federation/cel.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,12 @@ func EvalCEL(ctx context.Context, req *EvalCELRequest) (any, error) {
684684
}
685685
out = opt.GetValue()
686686
}
687+
if _, ok := out.(celtypes.Null); ok {
688+
if req.OutType == nil {
689+
return nil, nil
690+
}
691+
return reflect.Zero(req.OutType).Interface(), nil
692+
}
687693
if req.OutType != nil {
688694
return out.ConvertToNative(req.OutType)
689695
}

grpc/federation/generator/decode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,7 @@ func (d *decoder) toType(t *plugin.Type) (*resolver.Type, error) {
17111711
return &resolver.Type{
17121712
Kind: d.toTypeKind(t.GetKind()),
17131713
Repeated: t.GetRepeated(),
1714+
IsNull: t.GetIsNull(),
17141715
Message: msg,
17151716
Enum: enum,
17161717
OneofField: oneofField,

grpc/federation/generator/encode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ func (e *encoder) toType(t *resolver.Type) *plugin.Type {
574574
ret := &plugin.Type{
575575
Kind: e.toTypeKind(t.Kind),
576576
Repeated: t.Repeated,
577+
IsNull: t.IsNull,
577578
}
578579
switch {
579580
case t.Message != nil:

0 commit comments

Comments
 (0)