Skip to content

Commit ded3e63

Browse files
chore: update meta and model to be immutable
Co-Authored-By: [email protected] <[email protected]>
1 parent 90f34b9 commit ded3e63

File tree

3 files changed

+49
-60
lines changed

3 files changed

+49
-60
lines changed

lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/LDAiClient.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ public LDAiClient(LDClientInterface client) {
4444
/**
4545
* Method to convert the JSON variable into the AiConfig object
4646
*
47-
* Currently, I am doing this in a mutable way, just to verify the logic convert
48-
* LDValue into AiConfig.
49-
* It is possible we need a builder to create immutable version of this for ease
50-
* of use in a later PR.
51-
*
5247
* @param value
5348
* @param key
5449
*/
@@ -67,13 +62,12 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
6762
throw new AiConfigParseException("_ldMeta must be a JSON object");
6863
}
6964

70-
Meta meta = new Meta();
71-
// TODO: Do we expect customer calling this to build default value?
72-
// If we do, then some of the values would be null
73-
meta.setEnabled(ldValueNullCheck(valueMeta.get("enabled")).booleanValue());
74-
meta.setVariationKey(ldValueNullCheck(valueMeta.get("variationKey")).stringValue());
75-
Optional<Integer> version = Optional.of(valueMeta.get("version").intValue());
76-
meta.setVersion(version);
65+
// Create Meta using constructor
66+
Meta meta = new Meta(
67+
ldValueNullCheck(valueMeta.get("variationKey")).stringValue(),
68+
Optional.of(valueMeta.get("version").intValue()),
69+
ldValueNullCheck(valueMeta.get("enabled")).booleanValue()
70+
);
7771
result.setMeta(meta);
7872

7973
// Convert the optional messages from an JSON array of JSON objects into Message
@@ -97,25 +91,21 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
9791
throw new AiConfigParseException("model must be a JSON object");
9892
}
9993

100-
Model model = new Model();
101-
model.setName(ldValueNullCheck(valueModel.get("name")).stringValue());
94+
// Prepare parameters and custom maps for Model
95+
String modelName = ldValueNullCheck(valueModel.get("name")).stringValue();
96+
HashMap<String, LDValue> parameters = null;
97+
HashMap<String, LDValue> custom = null;
10298

10399
LDValue valueParameters = valueModel.get("parameters");
104100
if (valueParameters.getType() != LDValueType.NULL) {
105101
if (valueParameters.getType() != LDValueType.OBJECT) {
106102
throw new AiConfigParseException("non-null parameters must be a JSON object");
107103
}
108104

109-
HashMap<String, LDValue> parameters = new HashMap<>();
105+
parameters = new HashMap<>();
110106
for (String k : valueParameters.keys()) {
111107
parameters.put(k, valueParameters.get(k));
112108
}
113-
model.setParameters(parameters);
114-
} else {
115-
// Parameters is optional - so we can just set null and proceed
116-
117-
// TODO: Mustash validation somewhere
118-
model.setParameters(null);
119109
}
120110

121111
LDValue valueCustom = valueModel.get("custom");
@@ -124,15 +114,14 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
124114
throw new AiConfigParseException("non-null custom must be a JSON object");
125115
}
126116

127-
HashMap<String, LDValue> custom = new HashMap<>();
117+
custom = new HashMap<>();
128118
for (String k : valueCustom.keys()) {
129119
custom.put(k, valueCustom.get(k));
130120
}
131-
model.setCustom(custom);
132-
} else {
133-
// Custom is optional - we can just set null and proceed
134-
model.setCustom(null);
135121
}
122+
123+
// Create Model using constructor
124+
Model model = new Model(modelName, parameters, custom);
136125
result.setModel(model);
137126

138127
// Convert the optional provider from an JSON object of with name into Provider

lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/datamodel/Meta.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,40 @@ public final class Meta {
66
/**
77
* The variation key.
88
*/
9-
private String variationKey;
9+
private final String variationKey;
1010

1111
/**
1212
* The variation version.
1313
*/
14-
private Optional<Integer> version;
14+
private final Optional<Integer> version;
1515

1616
/**
1717
* If the config is enabled.
1818
*/
19-
private boolean enabled;
19+
private final boolean enabled;
2020

21-
// Getters and Setters
21+
/**
22+
* Constructor for Meta with all required fields.
23+
*
24+
* @param variationKey the variation key
25+
* @param version the version
26+
* @param enabled if the config is enabled
27+
*/
28+
public Meta(String variationKey, Optional<Integer> version, boolean enabled) {
29+
this.variationKey = variationKey;
30+
this.version = version != null ? version : Optional.empty();
31+
this.enabled = enabled;
32+
}
2233

2334
public String getVariationKey() {
2435
return variationKey;
2536
}
2637

27-
public void setVariationKey(String variationKey) {
28-
this.variationKey = variationKey;
29-
}
30-
3138
public Optional<Integer> getVersion() {
3239
return version;
3340
}
3441

35-
public void setVersion(Optional<Integer> version) {
36-
this.version = version;
37-
}
38-
3942
public boolean isEnabled() {
4043
return enabled;
4144
}
42-
43-
public void setEnabled(boolean enabled) {
44-
this.enabled = enabled;
45-
}
4645
}
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
package com.launchdarkly.sdk.server.ai.datamodel;
22

3+
import java.util.Collections;
34
import java.util.HashMap;
5+
import java.util.Map;
46

57
import com.launchdarkly.sdk.LDValue;
68

79
public final class Model {
8-
private String name;
10+
private final String name;
911

10-
private HashMap<String, LDValue> parameters;
12+
private final Map<String, LDValue> parameters;
1113

12-
private HashMap<String, LDValue> custom;
14+
private final Map<String, LDValue> custom;
1315

14-
// Getters and Setters
16+
/**
17+
* Constructor for Model with all required fields.
18+
*
19+
* @param name the model name
20+
* @param parameters the parameters map
21+
* @param custom the custom map
22+
*/
23+
public Model(String name, Map<String, LDValue> parameters, Map<String, LDValue> custom) {
24+
this.name = name;
25+
this.parameters = parameters != null ? Collections.unmodifiableMap(new HashMap<>(parameters)) : Collections.emptyMap();
26+
this.custom = custom != null ? Collections.unmodifiableMap(new HashMap<>(custom)) : Collections.emptyMap();
27+
}
1528

1629
public String getName() {
1730
return name;
1831
}
1932

20-
public void setName(String name) {
21-
this.name = name;
22-
}
23-
24-
public HashMap<String, LDValue> getParameters() {
33+
public Map<String, LDValue> getParameters() {
2534
return parameters;
2635
}
2736

28-
public void setParameters(HashMap<String, LDValue> parameters) {
29-
this.parameters = parameters;
30-
}
31-
32-
public HashMap<String, LDValue> getCustom() {
37+
public Map<String, LDValue> getCustom() {
3338
return custom;
3439
}
35-
36-
public void setCustom(HashMap<String, LDValue> custom) {
37-
this.custom = custom;
38-
}
3940
}

0 commit comments

Comments
 (0)