Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support oneof based conditional value #33

Merged
merged 7 commits into from
Oct 16, 2023
Merged

Support oneof based conditional value #33

merged 7 commits into from
Oct 16, 2023

Conversation

goccy
Copy link
Member

@goccy goccy commented Oct 13, 2023

This PR adds two features related to oneof.

1. Set the value for the oneof field

Previously, when assigning a value to a oneof field, it was only possible to use autobind to assign the preconstructed oneof value as is.

However, from now on, it will be possible to create a value for each oneof the fields with a grpc.federation.field option.

Evaluate messages only if expr is true.
Finally, evaluate by and assign it to field.

message UserSelection {
  option (grpc.federation.message) = {
    messages {
      name: "m"
      message: "M"
    }
  };
  oneof user {
    User user_a = 1 [
      (grpc.federation.field).oneof = {
        expr: "m.value == $.value"
        messages {
          name: "ua"
          message: "User"
          args: { name: "user_id", string: "a" }
        }
        by: "ua"
      }
    ];

    User user_b = 2 [
      (grpc.federation.field).oneof = {
        expr: "m.value != $.value"
        messages {
          name: "ub"
          message: "User"
          args: { name: "user_id", string: "b" }
        }
        by: "ub"
      }
    ];
  }
}

2. Retrieving the value of oneof

sel is a variable name for got message.
user is a oneof's name of the above proto.

Normally, we cannot directly reference the name of oneof, but if all of the oneof fields have the same type, we can retrieve the value of any field that is not nil.

message GetResponse {
  option (grpc.federation.message) = {
    messages: [
      {
        name: "sel"
        message: "UserSelection"
        args { name: "value", string: "foo" }
      }
    ]
  };
  User user = 1 [(grpc.federation.field).by = "sel.user"];
}

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link

Code Metrics Report

main (f3f58fc) #33 (e2731c5) +/-
Coverage 65.9% 66.1% +0.2%
Code to Test Ratio 1:0.1 1:0.1 -0.0
Test Execution Time 3m32s 3m8s -24s
Details
  |                     | main (f3f58fc) | #33 (e2731c5) |  +/-  |
  |---------------------|----------------|---------------|-------|
+ | Coverage            |          65.9% |         66.1% | +0.2% |
  |   Files             |             47 |            47 |     0 |
  |   Lines             |           6974 |          7295 |  +321 |
+ |   Covered           |           4595 |          4819 |  +224 |
- | Code to Test Ratio  |          1:0.1 |         1:0.1 |  -0.0 |
  |   Code              |          52128 |         57951 | +5823 |
+ |   Test              |           6167 |          6411 |  +244 |
+ | Test Execution Time |          3m32s |          3m8s |  -24s |

Code coverage of files in pull request scope (72.0% → 71.9%)

Files Coverage +/-
generator/code_generator.go 86.9% -0.1%
grpc/federation/federation.pb.go 22.8% +0.4%
internal/testutil/builder.go 89.2% +0.5%
internal/testutil/cmpopt.go 100.0% 0.0%
resolver/cel.go 69.2% +2.6%
resolver/graph.go 87.2% -8.7%
resolver/message.go 85.2% +0.7%
resolver/oneof.go 76.9% -3.1%
resolver/resolver.go 87.8% -0.0%
resolver/types.go 90.5% 0.0%
resolver/value.go 76.5% +0.1%
source/file.go 74.2% -2.2%
source/location.go 54.0% -10.3%

Reported by octocov

@goccy goccy merged commit 0e3d4b9 into main Oct 16, 2023
4 checks passed
@goccy goccy deleted the oneof branch October 16, 2023 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant