31
31
import java .util .List ;
32
32
import java .util .Map ;
33
33
import java .util .Objects ;
34
+ import java .util .ArrayList ;
34
35
35
36
import javax .xml .bind .annotation .XmlAccessType ;
36
37
import javax .xml .bind .annotation .XmlAccessorType ;
39
40
40
41
import org .apache .atlas .model .PList ;
41
42
import org .apache .atlas .model .SearchFilter .SortType ;
43
+ import org .apache .atlas .model .TypeCategory ;
42
44
import org .apache .atlas .model .typedef .AtlasBaseTypeDef ;
45
+ import org .apache .atlas .type .AtlasStructType ;
43
46
import org .apache .commons .collections .CollectionUtils ;
44
47
import org .apache .commons .collections .MapUtils ;
45
48
@@ -65,6 +68,14 @@ public class AtlasStruct implements Serializable {
65
68
@ Deprecated
66
69
public static final DateFormat DATE_FORMATTER = new SimpleDateFormat (SERIALIZED_DATE_FORMAT_STR );
67
70
71
+ private static final List <String > ALLOWED_DATATYPES_FOR_DEFAULT_NULL = new ArrayList () {
72
+ {
73
+ add ("int" );
74
+ add ("long" );
75
+ add ("float" );
76
+ }
77
+ };
78
+
68
79
private String typeName ;
69
80
private Map <String , Object > attributes ;
70
81
@@ -99,6 +110,22 @@ public AtlasStruct(Map map) {
99
110
}
100
111
}
101
112
113
+ public AtlasStruct (Map map , Map <String , AtlasStructType .AtlasAttribute > attributeTypes , boolean setDefaultValues ) {
114
+ if (map != null ) {
115
+ Object typeName = map .get (KEY_TYPENAME );
116
+ Map attributes = (map .get (KEY_ATTRIBUTES ) instanceof Map ) ? (Map ) map .get (KEY_ATTRIBUTES ) : map ;
117
+
118
+ if (typeName != null ) {
119
+ setTypeName (typeName .toString ());
120
+ }
121
+ if (setDefaultValues ) {
122
+ setAttributes (new HashMap <>(attributes ), attributeTypes );
123
+ } else {
124
+ setAttributes (new HashMap <>(attributes ));
125
+ }
126
+ }
127
+ }
128
+
102
129
public AtlasStruct (AtlasStruct other ) {
103
130
if (other != null ) {
104
131
setTypeName (other .getTypeName ());
@@ -122,6 +149,35 @@ public void setAttributes(Map<String, Object> attributes) {
122
149
this .attributes = attributes ;
123
150
}
124
151
152
+ public void setAttributes (Map <String , Object > attributes ,
153
+ Map <String , AtlasStructType .AtlasAttribute > attributeTypes ) {
154
+ if (MapUtils .isNotEmpty (attributeTypes ) && MapUtils .isNotEmpty (attributes )) {
155
+ this .attributes = new HashMap <>();
156
+ for (Map .Entry <String , AtlasStructType .AtlasAttribute > entry : attributeTypes .entrySet ()) {
157
+ String attrName = entry .getKey ();
158
+ AtlasStructType .AtlasAttribute attrType = entry .getValue ();
159
+ Object attrValue = attributes .get (attrName );
160
+
161
+ if (attrType .getAttributeType ().getTypeCategory () == TypeCategory .PRIMITIVE ) {
162
+ if (attrValue == null ) {
163
+ if (attrType .getAttributeDef ().getDefaultValue () != null ) {
164
+ attrValue = attrType .getAttributeType ().createDefaultValue ();
165
+ } else if (attrType .getAttributeDef ().getIsDefaultValueNull ()
166
+ && ALLOWED_DATATYPES_FOR_DEFAULT_NULL .contains (attrType .getAttributeType ().getTypeName ())) {
167
+ attrValue = null ;
168
+ } else if (attrType .getAttributeDef ().getIsOptional ()) {
169
+ attrValue = attrType .getAttributeType ().createOptionalDefaultValue ();
170
+ } else {
171
+ attrValue = attrType .getAttributeType ().createDefaultValue ();
172
+ }
173
+ }
174
+ }
175
+
176
+ this .attributes .put (attrName , attrValue );
177
+ }
178
+ }
179
+ }
180
+
125
181
public boolean hasAttribute (String name ) {
126
182
Map <String , Object > a = this .attributes ;
127
183
0 commit comments