-
Notifications
You must be signed in to change notification settings - Fork 796
/
SdkField.java
288 lines (258 loc) · 10.2 KB
/
SdkField.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.awssdk.core;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import software.amazon.awssdk.annotations.SdkProtectedApi;
import software.amazon.awssdk.core.protocol.MarshallLocation;
import software.amazon.awssdk.core.protocol.MarshallingType;
import software.amazon.awssdk.core.traits.DataTypeConversionFailureHandlingTrait;
import software.amazon.awssdk.core.traits.DefaultValueTrait;
import software.amazon.awssdk.core.traits.LocationTrait;
import software.amazon.awssdk.core.traits.Trait;
/**
* Metadata about a member in an {@link SdkPojo}. Contains information about how to marshall/unmarshall.
*
* @param <TypeT> Java Type of member.
*/
@SdkProtectedApi
public final class SdkField<TypeT> {
private final String memberName;
private final MarshallingType<? super TypeT> marshallingType;
private final MarshallLocation location;
private final String locationName;
private final String unmarshallLocationName;
private final Supplier<SdkPojo> constructor;
private final BiConsumer<Object, TypeT> setter;
private final Function<Object, TypeT> getter;
private final Map<Class<? extends Trait>, Trait> traits;
private SdkField(Builder<TypeT> builder) {
this.memberName = builder.memberName;
this.marshallingType = builder.marshallingType;
this.traits = new HashMap<>(builder.traits);
this.constructor = builder.constructor;
this.setter = builder.setter;
this.getter = builder.getter;
// Eagerly dereference location trait since it's so commonly used.
LocationTrait locationTrait = getTrait(LocationTrait.class);
this.location = locationTrait.location();
this.locationName = locationTrait.locationName();
this.unmarshallLocationName = locationTrait.unmarshallLocationName();
}
public String memberName() {
return memberName;
}
/**
* @return MarshallingType of member. Used primarily for marshaller/unmarshaller lookups.
*/
public MarshallingType<? super TypeT> marshallingType() {
return marshallingType;
}
/**
* @return Location the member should be marshalled into (i.e. headers/query/path/payload).
*/
public MarshallLocation location() {
return location;
}
/**
* @return The location name to use when marshalling. I.E. the field name of the JSON document, or the header name, etc.
*/
public String locationName() {
return locationName;
}
/**
* @return whether data-type conversion errors are to be ignored
*/
public boolean ignoreDataTypeConversionFailures() {
DataTypeConversionFailureHandlingTrait dataTypeConversionFailureHandlingTrait =
getTrait(DataTypeConversionFailureHandlingTrait.class);
return dataTypeConversionFailureHandlingTrait != null;
}
/**
* @return The location name to use when unmarshalling. This is only needed for AWS/Query or EC2 services. All
* other services should use {@link #locationName} for both marshalling and unmarshalling.
*/
public String unmarshallLocationName() {
return unmarshallLocationName;
}
public Supplier<SdkPojo> constructor() {
return constructor;
}
/**
* Gets the trait of the specified class if available.
*
* @param clzz Trait class to get.
* @param <T> Type of trait.
* @return Trait instance or null if trait is not present.
*/
@SuppressWarnings("unchecked")
public <T extends Trait> T getTrait(Class<T> clzz) {
return (T) traits.get(clzz);
}
/**
* Gets the trait of the specified class if available.
*
* @param clzz Trait class to get.
* @param <T> Type of trait.
* @return Optional of trait instance.
*/
@SuppressWarnings("unchecked")
public <T extends Trait> Optional<T> getOptionalTrait(Class<T> clzz) {
return Optional.ofNullable((T) traits.get(clzz));
}
/**
* Gets the trait of the specified class, or throw {@link IllegalStateException} if not available.
*
* @param clzz Trait class to get.
* @param <T> Type of trait.
* @return Trait instance.
* @throws IllegalStateException if trait is not present.
*/
@SuppressWarnings("unchecked")
public <T extends Trait> T getRequiredTrait(Class<T> clzz) throws IllegalStateException {
T trait = (T) traits.get(clzz);
if (trait == null) {
throw new IllegalStateException(memberName + " member is missing " + clzz.getSimpleName());
}
return trait;
}
/**
* Checks if a given {@link Trait} is present on the field.
*
* @param clzz Trait class to check.
* @return True if trait is present, false if not.
*/
public boolean containsTrait(Class<? extends Trait> clzz) {
return traits.containsKey(clzz);
}
/**
* Retrieves the current value of 'this' field from the given POJO. Uses the getter passed into the {@link Builder}.
*
* @param pojo POJO to retrieve value from.
* @return Current value of 'this' field in the POJO.
*/
private TypeT get(Object pojo) {
return getter.apply(pojo);
}
/**
* Retrieves the current value of 'this' field from the given POJO. Uses the getter passed into the {@link Builder}. If the
* current value is null this method will look for the {@link DefaultValueTrait} on the field and attempt to resolve a default
* value. If the {@link DefaultValueTrait} is not present this just returns null.
*
* @param pojo POJO to retrieve value from.
* @return Current value of 'this' field in the POJO or default value if current value is null.
*/
public TypeT getValueOrDefault(Object pojo) {
TypeT val = this.get(pojo);
DefaultValueTrait trait = getTrait(DefaultValueTrait.class);
return (trait == null ? val : (TypeT) trait.resolveValue(val));
}
/**
* Sets the given value on the POJO via the setter passed into the {@link Builder}.
*
* @param pojo POJO containing field to set.
* @param val Value of field.
*/
@SuppressWarnings("unchecked")
public void set(Object pojo, Object val) {
setter.accept(pojo, (TypeT) val);
}
/**
* Creates a new instance of {@link Builder} bound to the specified type.
*
* @param marshallingType Type of field.
* @param <TypeT> Type of field. Must be a subtype of the {@link MarshallingType} type param.
* @return New builder instance.
*/
public static <TypeT> Builder<TypeT> builder(MarshallingType<? super TypeT> marshallingType) {
return new Builder<>(marshallingType);
}
/**
* Builder for {@link SdkField}.
*
* @param <TypeT> Java type of field.
*/
public static final class Builder<TypeT> {
private final MarshallingType<? super TypeT> marshallingType;
private String memberName;
private Supplier<SdkPojo> constructor;
private BiConsumer<Object, TypeT> setter;
private Function<Object, TypeT> getter;
private final Map<Class<? extends Trait>, Trait> traits = new HashMap<>();
private Builder(MarshallingType<? super TypeT> marshallingType) {
this.marshallingType = marshallingType;
}
public Builder<TypeT> memberName(String memberName) {
this.memberName = memberName;
return this;
}
/**
* Sets a {@link Supplier} which will create a new <b>MUTABLE</b> instance of the POJO. I.E. this will
* create the Builder for a given POJO and not the immutable POJO itself.
*
* @param constructor Supplier method to create the mutable POJO.
* @return This object for method chaining.
*/
public Builder<TypeT> constructor(Supplier<SdkPojo> constructor) {
this.constructor = constructor;
return this;
}
/**
* Sets the {@link BiConsumer} which will accept an object and a value and set that value on the appropriate
* member of the object. This requires a <b>MUTABLE</b> pojo so thus this setter will be on the Builder
* for the given POJO.
*
* @param setter Setter method.
* @return This object for method chaining.
*/
public Builder<TypeT> setter(BiConsumer<Object, TypeT> setter) {
this.setter = setter;
return this;
}
/**
* Sets the {@link Function} that will accept an object and return the current value of 'this' field on that object.
* This will typically be a getter on the immutable representation of the POJO and is used mostly during marshalling.
*
* @param getter Getter method.
* @return This object for method chaining.
*/
public Builder<TypeT> getter(Function<Object, TypeT> getter) {
this.getter = getter;
return this;
}
/**
* Attaches one or more traits to the {@link SdkField}. Traits can have additional metadata and behavior that
* influence how a field is marshalled/unmarshalled.
*
* @param traits Traits to attach.
* @return This object for method chaining.
*/
public Builder<TypeT> traits(Trait... traits) {
Arrays.stream(traits).forEach(t -> this.traits.put(t.getClass(), t));
return this;
}
/**
* @return An immutable {@link SdkField}.
*/
public SdkField<TypeT> build() {
return new SdkField<>(this);
}
}
}