in sources/src/main/java/com/google/solutions/jitaccess/catalog/policy/PolicyDocument.java [842:918]
public record CelVariableElement(
@JsonProperty("type") String type,
@JsonProperty("name") String name,
@JsonProperty("displayName") String displayName,
@JsonProperty("min") Integer min,
@JsonProperty("max") Integer max
) {
static CelVariableElement toYaml(@NotNull CelConstraint.Variable variable) {
if (variable instanceof CelConstraint.StringVariable stringVariable) {
return new CelVariableElement(
"string",
stringVariable.name(),
stringVariable.displayName(),
stringVariable.minLength(),
stringVariable.maxLength());
}
else if (variable instanceof CelConstraint.LongVariable longVariable) {
return new CelVariableElement(
"int",
longVariable.name(),
longVariable.displayName(),
(int)(long)longVariable.minInclusive(),
(int)(long)longVariable.maxInclusive());
}
else if (variable instanceof CelConstraint.BooleanVariable booleanVariable) {
return new CelVariableElement(
"boolean",
booleanVariable.name(),
booleanVariable.displayName(),
null,
null);
}
else {
throw new UnsupportedOperationException("The variable type is not supported");
}
}
@NotNull Optional<CelConstraint.Variable> toPolicy(@NotNull IssueCollection issues) {
try {
return Optional
.ofNullable(switch (Strings.nullToEmpty(this.type).trim().toLowerCase()) {
case "string" -> new CelConstraint.StringVariable(
this.name,
this.displayName,
this.min != null ? this.min : 0,
this.max != null ? this.max : 256);
case "int", "integer" -> new CelConstraint.LongVariable(
this.name,
this.displayName,
this.min != null ? (long) this.min : 0,
this.max != null ? (long) this.max : Integer.MAX_VALUE);
case "bool", "boolean" -> new CelConstraint.BooleanVariable(
this.name,
this.displayName);
default -> {
issues.error(
Issue.Code.CONSTRAINT_INVALID_VARIABLE_DECLARATION,
"The variable declaration '%s' uses an unknown type: %s",
this.name,
this.type);
yield null;
}
});
}
catch (Exception e) {
issues.error(
Issue.Code.CONSTRAINT_INVALID_VARIABLE_DECLARATION,
"The variable declaration '%s' is invalid: %s",
this.name,
e.getMessage());
return Optional.empty();
}
}
}