Skip to content

feat(sdk): add shorthand enum constants for policy types#3408

Merged
marythought merged 3 commits intomainfrom
feat/DSPX-2959-shorthand-sdk-enums
Apr 28, 2026
Merged

feat(sdk): add shorthand enum constants for policy types#3408
marythought merged 3 commits intomainfrom
feat/DSPX-2959-shorthand-sdk-enums

Conversation

@marythought
Copy link
Copy Markdown
Contributor

@marythought marythought commented Apr 27, 2026

Summary

  • Add readable constant aliases for verbose proto enum names so developers can write policy.OperatorIn instead of policy.SubjectMappingOperatorEnum_SUBJECT_MAPPING_OPERATOR_ENUM_IN
  • Uses the source-file codegen pattern from ADR DSPX-2594 — helpers authored in protocol/go/internal/policy/ with full IDE support, copied into the policy package at build time
  • Zero runtime cost — these are typed compile-time constant aliases

Constants added

Shorthand Full proto name
policy.OperatorIn SubjectMappingOperatorEnum_SUBJECT_MAPPING_OPERATOR_ENUM_IN
policy.OperatorNotIn SubjectMappingOperatorEnum_SUBJECT_MAPPING_OPERATOR_ENUM_NOT_IN
policy.OperatorInContains SubjectMappingOperatorEnum_SUBJECT_MAPPING_OPERATOR_ENUM_IN_CONTAINS
policy.BooleanAnd ConditionBooleanTypeEnum_CONDITION_BOOLEAN_TYPE_ENUM_AND
policy.BooleanOr ConditionBooleanTypeEnum_CONDITION_BOOLEAN_TYPE_ENUM_OR
policy.RuleAllOf AttributeRuleTypeEnum_ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF
policy.RuleAnyOf AttributeRuleTypeEnum_ATTRIBUTE_RULE_TYPE_ENUM_ANY_OF
policy.RuleHierarchy AttributeRuleTypeEnum_ATTRIBUTE_RULE_TYPE_ENUM_HIERARCHY
policy.StateActive common.ActiveStateEnum_ACTIVE_STATE_ENUM_ACTIVE
policy.StateInactive common.ActiveStateEnum_ACTIVE_STATE_ENUM_INACTIVE
policy.StateAny common.ActiveStateEnum_ACTIVE_STATE_ENUM_ANY

Before / After

// Before (numeric codes — what the docs previously showed)
condition := &policy.Condition{
    Operator: 1, // SUBJECT_MAPPING_OPERATOR_ENUM_IN
}
group := &policy.ConditionGroup{
    BooleanOperator: 1, // CONDITION_BOOLEAN_TYPE_ENUM_AND
}

// Before (full proto enum names)
condition := &policy.Condition{
    Operator: policy.SubjectMappingOperatorEnum_SUBJECT_MAPPING_OPERATOR_ENUM_IN,
}
group := &policy.ConditionGroup{
    BooleanOperator: policy.ConditionBooleanTypeEnum_CONDITION_BOOLEAN_TYPE_ENUM_AND,
}

// After
condition := &policy.Condition{
    Operator: policy.OperatorIn,
}
group := &policy.ConditionGroup{
    BooleanOperator: policy.BooleanAnd,
}

Resolves #3338

Test plan

  • Source file tests pass: go test ./protocol/go/internal/policy/...
  • Generated file compiles: go vet ./protocol/go/policy/...
  • Codegen runs cleanly: cd protocol/codegen && go run .
  • CI passes

🤖 Generated with Claude Code

Add readable constant aliases for verbose proto enum names using the
source-file codegen pattern (ADR DSPX-2594). Developers can now write:

  condition.Operator = policy.OperatorIn
  group.BooleanOperator = policy.BooleanAnd
  attr.Rule = policy.RuleHierarchy

Instead of:

  condition.Operator = policy.SubjectMappingOperatorEnum_SUBJECT_MAPPING_OPERATOR_ENUM_IN

Constants covered:
- OperatorIn, OperatorNotIn, OperatorInContains (SubjectMappingOperatorEnum)
- BooleanAnd, BooleanOr (ConditionBooleanTypeEnum)
- RuleAllOf, RuleAnyOf, RuleHierarchy (AttributeRuleTypeEnum)
- StateActive, StateInactive, StateAny (ActiveStateEnum)

Resolves: #3338

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@marythought marythought requested review from a team as code owners April 27, 2026 22:12
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Note

Gemini is unable to generate a summary for this pull request due to the file types involved not being currently supported.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

📝 Walkthrough

Walkthrough

The changes add a code generator configuration to automatically produce a public enum aliases file (enums.gen.go) from an internal template, along with shorthand constant definitions mapping verbose protobuf enum names to concise developer-friendly names for operators, booleans, rules, and states.

Changes

Cohort / File(s) Summary
Code Generator Configuration
protocol/codegen/main.go
Adds a new helper copy mapping enabling the code generator to process Go helper files from protocol/go/internal/policy and emit transformed .gen.go files to protocol/go/policy with proto import rewriting applied.
Enum Alias Definitions
protocol/go/internal/policy/enums.go, protocol/go/policy/enums.gen.go
Defines shorthand exported constant aliases for verbose protobuf enum types: OperatorIn/OperatorNotIn/OperatorInContains for SubjectMappingOperatorEnum; BooleanAnd/BooleanOr for ConditionBooleanTypeEnum; RuleAllOf/RuleAnyOf/RuleHierarchy for AttributeRuleTypeEnum; and StateActive/StateInactive/StateAny for ActiveStateEnum, with illustrative usage examples included.
Enum Tests
protocol/go/internal/policy/enums_test.go
Adds four test functions verifying that internally defined policy enum constant aliases correctly map to their corresponding protobuf enum values from the policy and common packages.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 No more typing SUBJECT_MAPPING_OPERATOR_ENUM_IN today!
Just shorthand aliases hopping into your API way,
IN and AND are shorter, they say,
Code generation brings peace and ballet.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding shorthand enum constant aliases for policy types to improve developer experience.
Linked Issues check ✅ Passed The PR successfully implements the core objective from issue #3338: providing shorthand enum constants (IN, NOT_IN, IN_CONTAINS, AND, OR, etc.) as typed compile-time alternatives to verbose proto enum names.
Out of Scope Changes check ✅ Passed All changes are scoped to the stated objectives: the codegen configuration enables copying helper files, and new enum constants are defined with tests, with no unrelated modifications.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/DSPX-2959-shorthand-sdk-enums

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

marythought added a commit to opentdf/docs that referenced this pull request Apr 27, 2026
Update Go code samples to use the new policy.OperatorIn, policy.BooleanAnd,
policy.RuleAnyOf shorthand constants instead of verbose proto enum names.

Companion to opentdf/platform#3408.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

Benchmark results, click to expand

Benchmark authorization.GetDecisions Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 149.49189ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 82.176789ms

Benchmark Statistics

Name № Requests Avg Duration Min Duration Max Duration

Bulk Benchmark Results

Metric Value
Total Decrypts 100
Successful Decrypts 100
Failed Decrypts 0
Total Time 406.7413ms
Throughput 245.86 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 40.93365522s
Average Latency 408.093425ms
Throughput 122.15 requests/second

The new enums_test.go in protocol/go/internal/policy/ imports
github.com/stretchr/testify, which needs to be in the module's go.mod.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@marythought marythought requested a review from a team as a code owner April 27, 2026 22:18
@github-actions
Copy link
Copy Markdown
Contributor

Use stdlib testing patterns consistent with the existing
authorization/v2 helper tests. No new dependencies needed.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

X-Test Failure Report

@github-actions
Copy link
Copy Markdown
Contributor

Benchmark results, click to expand

Benchmark authorization.GetDecisions Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 204.455615ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 102.915855ms

Benchmark Statistics

Name № Requests Avg Duration Min Duration Max Duration

Bulk Benchmark Results

Metric Value
Total Decrypts 100
Successful Decrypts 100
Failed Decrypts 0
Total Time 399.068573ms
Throughput 250.58 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 42.592139366s
Average Latency 423.863109ms
Throughput 117.39 requests/second

@github-actions
Copy link
Copy Markdown
Contributor

Benchmark results, click to expand

Benchmark authorization.GetDecisions Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 182.200055ms

Benchmark authorization.v2.GetMultiResourceDecision Results:

Metric Value
Approved Decision Requests 1000
Denied Decision Requests 0
Total Time 98.104438ms

Benchmark Statistics

Name № Requests Avg Duration Min Duration Max Duration

Bulk Benchmark Results

Metric Value
Total Decrypts 100
Successful Decrypts 100
Failed Decrypts 0
Total Time 391.789315ms
Throughput 255.24 requests/second

TDF3 Benchmark Results:

Metric Value
Total Requests 5000
Successful Requests 5000
Failed Requests 0
Concurrent Requests 50
Total Time 42.83973038s
Average Latency 426.778089ms
Throughput 116.71 requests/second

@github-actions
Copy link
Copy Markdown
Contributor

⚠️ Govulncheck found vulnerabilities ⚠️

The following modules have known vulnerabilities:

  • examples
  • otdfctl
  • sdk
  • service
  • lib/fixtures
  • tests-bdd

See the workflow run for details.

@marythought
Copy link
Copy Markdown
Contributor Author

📝 Companion docs PR: opentdf/docs#308 — updates Go SDK code samples to use shorthand enum constants.

@marythought marythought added this pull request to the merge queue Apr 28, 2026
Merged via the queue into main with commit c6f18cb Apr 28, 2026
41 checks passed
@marythought marythought deleted the feat/DSPX-2959-shorthand-sdk-enums branch April 28, 2026 14:43
marythought added a commit to opentdf/docs that referenced this pull request Apr 28, 2026
Update Go code samples to use the new policy.OperatorIn, policy.BooleanAnd,
policy.RuleAnyOf shorthand constants instead of verbose proto enum names.

Companion to opentdf/platform#3408.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
marythought added a commit to opentdf/web-sdk that referenced this pull request Apr 28, 2026
…928)

Re-export SubjectMappingOperatorEnum, ConditionBooleanTypeEnum,
AttributeRuleTypeEnum, and ActiveStateEnum from the main @opentdf/sdk
entry point so users can import them directly instead of reaching into
generated proto paths.

Companion to opentdf/platform#3408 (Go SDK) and opentdf/java-sdk#357
(Java SDK).

Signed-off-by: Mary Dickson <mary.dickson@virtru.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
marythought added a commit to opentdf/java-sdk that referenced this pull request Apr 29, 2026
## Summary

- Add `PolicyEnums` utility class with readable constant aliases for
verbose protobuf enum names so developers can write
`PolicyEnums.OPERATOR_IN` instead of
`SubjectMappingOperatorEnum.SUBJECT_MAPPING_OPERATOR_ENUM_IN`
- Update all examples to use the new shorthand constants via static
imports
- Companion to opentdf/platform#3408 (Go SDK equivalent)

### Constants added

| Shorthand | Full proto name |
|---|---|
| `OPERATOR_IN` | `SUBJECT_MAPPING_OPERATOR_ENUM_IN` |
| `OPERATOR_NOT_IN` | `SUBJECT_MAPPING_OPERATOR_ENUM_NOT_IN` |
| `OPERATOR_IN_CONTAINS` | `SUBJECT_MAPPING_OPERATOR_ENUM_IN_CONTAINS` |
| `BOOLEAN_AND` | `CONDITION_BOOLEAN_TYPE_ENUM_AND` |
| `BOOLEAN_OR` | `CONDITION_BOOLEAN_TYPE_ENUM_OR` |
| `RULE_ALL_OF` | `ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF` |
| `RULE_ANY_OF` | `ATTRIBUTE_RULE_TYPE_ENUM_ANY_OF` |
| `RULE_HIERARCHY` | `ATTRIBUTE_RULE_TYPE_ENUM_HIERARCHY` |
| `STATE_ACTIVE` | `ACTIVE_STATE_ENUM_ACTIVE` |
| `STATE_INACTIVE` | `ACTIVE_STATE_ENUM_INACTIVE` |
| `STATE_ANY` | `ACTIVE_STATE_ENUM_ANY` |

### Before / After

```java
// Before
ConditionGroup.newBuilder()
    .setBooleanOperator(ConditionBooleanTypeEnum.CONDITION_BOOLEAN_TYPE_ENUM_AND)
    .addConditions(Condition.newBuilder()
        .setOperator(SubjectMappingOperatorEnum.SUBJECT_MAPPING_OPERATOR_ENUM_IN));

// After
import static io.opentdf.platform.sdk.PolicyEnums.*;

ConditionGroup.newBuilder()
    .setBooleanOperator(BOOLEAN_AND)
    .addConditions(Condition.newBuilder()
        .setOperator(OPERATOR_IN));
```

## Test plan

- [x] `mvn compile` passes (full project including examples)
- [x] `mvn test -Dtest=PolicyEnumsTest` — 4 tests, all pass
- [ ] CI passes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **New Features**
* Introduced simplified static constants for policy configuration,
making attribute rules, operators, and boolean conditions more readable
and intuitive to use.

* **Documentation**
* Updated example code to demonstrate the new simplified policy
constants.

* **Tests**
* Added test coverage validating the new policy configuration constants.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Signed-off-by: Mary Dickson <mary.dickson@virtru.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
marythought added a commit to opentdf/docs that referenced this pull request Apr 29, 2026
Update Go code samples to use the new policy.OperatorIn, policy.BooleanAnd,
policy.RuleAnyOf shorthand constants instead of verbose proto enum names.

Companion to opentdf/platform#3408.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
marythought added a commit to opentdf/docs that referenced this pull request Apr 30, 2026
## Summary

- Update Go SDK code samples to use the new shorthand enum constants
(`policy.OperatorIn`, `policy.BooleanAnd`, `policy.RuleAnyOf`) instead
of verbose proto enum names
- Update JS SDK imports to use barrel exports from `@opentdf/sdk`
instead of deep proto paths
(`@opentdf/sdk/platform/policy/objects_pb.js`)
- Update JS SDK examples to use enum values
(`ConditionBooleanTypeEnum.AND`) instead of string literals
(`'CONDITION_BOOLEAN_TYPE_ENUM_AND'`)
- Update Java SDK inline examples in policy.mdx to use `PolicyEnums`
shorthand constants
- Add `SdkVersion` badges with `NEXT` placeholders — replace with actual
versions once SDK releases ship

### Companion PRs

| SDK | PR | Status |
|---|---|---|
| Go | opentdf/platform#3408 | Open |
| Java | opentdf/java-sdk#357 | Open |
| JavaScript | opentdf/web-sdk#928 | Open |

### Note on Java code samples

The `code_samples/java/` files are pulled from the java-sdk repo at
build time via `docusaurus-plugin-remote-content`. They will update
automatically once `javaSdkVersion` is bumped in `docusaurus.config.ts`
after the java-sdk release containing opentdf/java-sdk#357.

## Test plan

- [x] `npm run build` passes
- [ ] Surge preview renders correctly
- [ ] Replace `NEXT` version placeholders after SDK releases

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Documentation**
* Updated policy and quickstart code examples for Go, Java, and
JavaScript to use simplified SDK enum constants for clearer, more
idiomatic samples.
* Switched JavaScript examples to use package entry-point imports
instead of generated-module paths.
* Added SDK version markers to code samples to show explicit
language/version context and improve consistency.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Mary Dickson <mary.dickson@virtru.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: accept shorthand operator names in Subject Mapping API

2 participants