diff options
author | Valerij Fredriksen <freva@users.noreply.github.com> | 2024-05-21 14:14:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-21 14:14:12 +0200 |
commit | 6f9339428d7fbadb2692fc38ff00e3aa51aa978b (patch) | |
tree | 9843774bc5222726751e056bf231e7acd154d30c /flags | |
parent | cac19f61ef200dc06736c5d40d38dc770e87c67f (diff) | |
parent | f74add7754f01e98e4966d31219c05fb799c1147 (diff) |
Merge pull request #31257 from vespa-engine/hakonhall/demonstrate-type-safe-setting-of-flag-dimensions
Demonstrate type-safe setting of flag dimensions
Diffstat (limited to 'flags')
-rw-r--r-- | flags/src/main/java/com/yahoo/vespa/flags/Flag.java | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/flags/src/main/java/com/yahoo/vespa/flags/Flag.java b/flags/src/main/java/com/yahoo/vespa/flags/Flag.java index 7ca5066969f..e4a8a1cd439 100644 --- a/flags/src/main/java/com/yahoo/vespa/flags/Flag.java +++ b/flags/src/main/java/com/yahoo/vespa/flags/Flag.java @@ -1,6 +1,19 @@ // Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.flags; +import com.yahoo.component.Version; +import com.yahoo.config.provision.ApplicationId; +import com.yahoo.config.provision.CloudName; +import com.yahoo.config.provision.ClusterSpec; +import com.yahoo.config.provision.Environment; +import com.yahoo.config.provision.NodeResources.Architecture; +import com.yahoo.config.provision.NodeType; +import com.yahoo.config.provision.SystemName; +import com.yahoo.config.provision.TenantName; +import com.yahoo.config.provision.Zone; +import com.yahoo.config.provision.zone.ZoneApi; +import com.yahoo.config.provision.zone.ZoneId; + import java.util.Optional; /** @@ -10,7 +23,7 @@ import java.util.Optional; * @param <F> The concrete subclass type of the flag * @author hakonhall */ -public interface Flag<T, F> { +public interface Flag<T, F extends Flag<T, F>> { /** The flag ID. */ FlagId id(); @@ -28,6 +41,43 @@ public interface Flag<T, F> { return dimensionValue.map(value -> with(dimension, value)).orElse(self()); } + /** Sets the tenant, application, and instance dimensions. */ + default F with(ApplicationId applicationId) { + return with(Dimension.TENANT_ID, applicationId.tenant().value()) + .with(Dimension.APPLICATION, applicationId.toSerializedFormWithoutInstance()) + .with(Dimension.INSTANCE_ID, applicationId.serializedForm()); + } + + /** architecture MUST NOT be 'any'. */ + default F with(Architecture architecture) { return with(Dimension.ARCHITECTURE, architecture.name()); } + default F with(CloudName cloud) { return with(Dimension.CLOUD, cloud.value()); } + default F with(ClusterSpec.Id clusterId) { return with(Dimension.CLUSTER_ID, clusterId.value()); } + default F with(ClusterSpec.Type clusterType) { return with(Dimension.CLUSTER_TYPE, clusterType.name()); } + default F with(Environment environment) { return with(Dimension.ENVIRONMENT, environment.value()); } + default F with(NodeType nodeType) { return with(Dimension.NODE_TYPE, nodeType.name()); } + default F with(SystemName systemName) { return with(Dimension.SYSTEM, systemName.value()); } + default F with(TenantName tenantName) { return with(Dimension.TENANT_ID, tenantName.value()); } + default F with(Version vespaVersion) { return with(Dimension.VESPA_VERSION, vespaVersion.toFullString()); } + default F with(ZoneId zoneId) { return with(Dimension.ZONE_ID, zoneId.value()); } + default F with(Zone zone) { return with(Dimension.ZONE_ID, zone.systemLocalValue()); } + default F with(ZoneApi zoneApi) { return with(zoneApi.getVirtualId()); } + + /** Sets the tenant, application, and instance dimensions. */ + default F withApplicationId(Optional<ApplicationId> applicationId) { return applicationId.map(this::with).orElse(self()); } + /** architecture MUST NOT be 'any'. */ + default F withArchitecture(Optional<Architecture> architecture) { return architecture.map(this::with).orElse(self()); } + default F withCloudName(Optional<CloudName> cloud) { return cloud.map(this::with).orElse(self()); } + default F withClusterId(Optional<ClusterSpec.Id> clusterId) { return clusterId.map(this::with).orElse(self()); } + default F withClusterType(Optional<ClusterSpec.Type> clusterType) { return clusterType.map(this::with).orElse(self()); } + default F withEnvironment(Optional<Environment> environment) { return environment.map(this::with).orElse(self()); } + default F withNodeType(Optional<NodeType> nodeType) { return nodeType.map(this::with).orElse(self()); } + default F withSystemName(Optional<SystemName> systemName) { return systemName.map(this::with).orElse(self()); } + default F withTenantName(Optional<TenantName> tenantName) { return tenantName.map(this::with).orElse(self()); } + default F withVersion(Optional<Version> vespaVersion) { return vespaVersion.map(this::with).orElse(self()); } + default F withZoneId(Optional<ZoneId> zoneId) { return zoneId.map(this::with).orElse(self()); } + default F withZone(Optional<Zone> zone) { return zone.map(this::with).orElse(self()); } + default F withZoneApi(Optional<ZoneApi> zoneApi) { return zoneApi.map(this::with).orElse(self()); } + /** Returns the value, boxed if the flag wraps a primitive type. */ T boxedValue(); } |