Mappings.jrag 13.5 KB
Newer Older
1
2
aspect DefaultMappings {

René Schöne's avatar
René Schöne committed
3
  private String RagConnect.baseDefaultMappingTypeNamePart(String typeName) {
René Schöne's avatar
René Schöne committed
4
    return capitalize(typeName).replace("[]", "s");
René Schöne's avatar
René Schöne committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  }

  private MappingDefinitionType RagConnect.baseDefaultMappingTypeFromName(String typeName) {
    return typeName.endsWith("[]") ?
        new JavaArrayMappingDefinitionType(new SimpleJavaTypeUse(typeName.replace("[]", ""))) :
        new JavaMappingDefinitionType(new SimpleJavaTypeUse(typeName));
  }

  private DefaultMappingDefinition RagConnect.baseDefaultMappingDefinition(String fromTypeName, String toTypeName, String content) {
    DefaultMappingDefinition result = new DefaultMappingDefinition();
    result.setID("_Default" + baseDefaultMappingTypeNamePart(fromTypeName) + "To" + baseDefaultMappingTypeNamePart(toTypeName) + "Mapping");
    result.setFromType(baseDefaultMappingTypeFromName(fromTypeName));
    result.setFromVariableName("input");
    result.setToType(baseDefaultMappingTypeFromName(toTypeName));
    result.setContent(content);
    return result;
  }

René Schöne's avatar
René Schöne committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToIntMapping() = baseDefaultMappingDefinition(
      "byte[]", "int", "return java.nio.ByteBuffer.wrap(input).getInt();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToShortMapping() = baseDefaultMappingDefinition(
      "byte[]", "short", "return java.nio.ByteBuffer.wrap(input).getShort();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToLongMapping() = baseDefaultMappingDefinition(
      "byte[]", "long", "return java.nio.ByteBuffer.wrap(input).getLong();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToFloatMapping() = baseDefaultMappingDefinition(
      "byte[]", "float", "return java.nio.ByteBuffer.wrap(input).getFloat();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToDoubleMapping() = baseDefaultMappingDefinition(
      "byte[]", "double", "return java.nio.ByteBuffer.wrap(input).getDouble();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToCharMapping() = baseDefaultMappingDefinition(
      "byte[]", "char", "return java.nio.ByteBuffer.wrap(input).getChar();");
  syn nta DefaultMappingDefinition RagConnect.defaultBytesToStringMapping() = baseDefaultMappingDefinition(
      "byte[]", "String", "return new String(input);");
37

René Schöne's avatar
René Schöne committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  syn nta DefaultMappingDefinition RagConnect.defaultIntToBytesMapping() = baseDefaultMappingDefinition(
      "int", "byte[]", "return java.nio.ByteBuffer.allocate(Integer.BYTES).putInt(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultShortToBytesMapping() = baseDefaultMappingDefinition(
      "short", "byte[]", "return java.nio.ByteBuffer.allocate(Short.BYTES).putShort(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultLongToBytesMapping() = baseDefaultMappingDefinition(
      "long", "byte[]", "return java.nio.ByteBuffer.allocate(Long.BYTES).putLong(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultFloatToBytesMapping() = baseDefaultMappingDefinition(
      "float", "byte[]", "return java.nio.ByteBuffer.allocate(Float.BYTES).putFloat(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultDoubleToBytesMapping() = baseDefaultMappingDefinition(
      "double", "byte[]", "return java.nio.ByteBuffer.allocate(Double.BYTES).putDouble(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultCharToBytesMapping() = baseDefaultMappingDefinition(
      "char", "byte[]", "return java.nio.ByteBuffer.allocate(Character.BYTES).putChar(input).array();");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToBytesMapping() = baseDefaultMappingDefinition(
      "String", "byte[]", "return input.getBytes();");
René Schöne's avatar
René Schöne committed
52

René Schöne's avatar
René Schöne committed
53
54
55
56
57
58
59
60
61
62
63
64
  syn nta DefaultMappingDefinition RagConnect.defaultStringToIntMapping() = baseDefaultMappingDefinition(
      "String", "int", "return Integer.parseInt(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToShortMapping() = baseDefaultMappingDefinition(
      "String", "short", "return Short.parseShort(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToLongMapping() = baseDefaultMappingDefinition(
      "String", "long", "return Long.parseLong(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToFloatMapping() = baseDefaultMappingDefinition(
      "String", "float", "return Float.parseFloat(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToDoubleMapping() = baseDefaultMappingDefinition(
      "String", "double", "return Double.parseDouble(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultStringToCharMapping() = baseDefaultMappingDefinition(
      "String", "char", "return input.charAt(0);");
René Schöne's avatar
René Schöne committed
65

René Schöne's avatar
René Schöne committed
66
67
68
69
70
71
72
73
74
75
76
77
  syn nta DefaultMappingDefinition RagConnect.defaultIntToStringMapping() = baseDefaultMappingDefinition(
      "int", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultShortToStringMapping() = baseDefaultMappingDefinition(
      "short", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultLongToStringMapping() = baseDefaultMappingDefinition(
      "long", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultFloatToStringMapping() = baseDefaultMappingDefinition(
      "float", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultDoubleToStringMapping() = baseDefaultMappingDefinition(
      "double", "String", "return String.valueOf(input);");
  syn nta DefaultMappingDefinition RagConnect.defaultCharToStringMapping() = baseDefaultMappingDefinition(
      "char", "String", "return String.valueOf(input);");
78
79
80
81
}

aspect Mappings {
  // --- effectiveMappings ---
82
  syn java.util.List<MappingDefinition> EndpointDefinition.effectiveMappings() {
83
    java.util.List<MappingDefinition> result;
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    if (isReceiveTokenEndpointDefinition() || isReceiveTypeEndpointDefinition()) {
      // if no mappings are specified, or if first mapping is not suitable.
      // then prepend the suitable default mapping
      if (getMappingList().isEmpty() || !getMappingList().get(0).getFromType().isByteArray()) {
        result = new java.util.ArrayList();
        result.add(suitableReceiveDefaultMapping());
        result.addAll(getMappingList());
      } else {
        result = getMappingList();
      }
    } else if (isSendTokenEndpointDefinition() || isSendTypeEndpointDefinition()) {
      // if no mappings are specified, or if last mapping is not suitable
      // then append the suitable default mapping
      if (getMappingList().isEmpty() || !getMappingList().get(getMappingList().size() - 1).getToType().isByteArray()) {
        result = new java.util.ArrayList(getMappingList());
        result.add(suitableSendDefaultMapping());
      } else {
        result = getMappingList();
      }
103
    } else {
104
      throw new RuntimeException("Unknown endpoint definition: " + this);
105
106
107
    }
    return result;
  }
René Schöne's avatar
René Schöne committed
108

René Schöne's avatar
René Schöne committed
109
  // --- isPrimitiveType ---
René Schöne's avatar
René Schöne committed
110
  syn boolean TokenComponent.isPrimitiveType() = effectiveJavaTypeUse().isPrimitiveType();
René Schöne's avatar
René Schöne committed
111
112
113
114
115
116
117
118
119
120
121
122
123
  syn boolean JavaTypeUse.isPrimitiveType() = false;
  eq SimpleJavaTypeUse.isPrimitiveType() {
    switch(getName()) {
      case "int":
      case "short":
      case "long":
      case "float":
      case "double":
      case "char":
      case "byte": return true;
      default: return false;
    }
  }
René Schöne's avatar
René Schöne committed
124
125
126
127
128
129
  syn boolean MappingDefinitionType.isPrimitiveType() = false;
  eq JavaMappingDefinitionType.isPrimitiveType() = getType().isPrimitiveType();

  // --- isArray ---
  syn boolean MappingDefinitionType.isArray() = false;
  eq JavaArrayMappingDefinitionType.isArray() = true;
René Schöne's avatar
René Schöne committed
130

131
132
133
  // --- suitableReceiveDefaultMapping ---
  syn DefaultMappingDefinition EndpointDefinition.suitableReceiveDefaultMapping() {
    switch (targetTypeName()) {
134
      case "int":
135
      case "Integer": return ragconnect().defaultBytesToIntMapping();
136
      case "short":
137
      case "Short": return ragconnect().defaultBytesToShortMapping();
138
      case "long":
139
      case "Long": return ragconnect().defaultBytesToLongMapping();
140
      case "float":
141
      case "Float": return ragconnect().defaultBytesToFloatMapping();
142
      case "double":
143
      case "Double": return ragconnect().defaultBytesToDoubleMapping();
144
      case "char":
145
146
      case "Character": return ragconnect().defaultBytesToCharMapping();
      case "String": return ragconnect().defaultBytesToStringMapping();
147
148
149
      default: return null;
    }
  }
150
151
152
  // --- suitableSendDefaultMapping ---
  syn DefaultMappingDefinition EndpointDefinition.suitableSendDefaultMapping() {
    switch (targetTypeName()) {
153
      case "int":
154
      case "Integer": return ragconnect().defaultIntToBytesMapping();
155
      case "short":
156
      case "Short": return ragconnect().defaultShortToBytesMapping();
157
      case "long":
158
      case "Long": return ragconnect().defaultLongToBytesMapping();
159
      case "float":
160
      case "Float": return ragconnect().defaultFloatToBytesMapping();
161
      case "double":
162
      case "Double": return ragconnect().defaultDoubleToBytesMapping();
163
      case "char":
164
165
      case "Character": return ragconnect().defaultCharToBytesMapping();
      case "String": return ragconnect().defaultStringToBytesMapping();
166
167
168
      default: return null;
    }
  }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

  // --- targetTypeName ---
  syn String EndpointDefinition.targetTypeName();
  eq ReceiveTokenEndpointDefinition.targetTypeName() {
    return getMappingList().isEmpty() ?
           getToken().effectiveJavaTypeUse().getName() :
           getMappingList().get(0).getFromType().prettyPrint();
  }
  eq ReceiveTypeEndpointDefinition.targetTypeName() {
    return getMappingList().get(0).getFromType().prettyPrint();
  }
  eq SendTokenEndpointDefinition.targetTypeName() {
    return getMappingList().isEmpty() ?
           getToken().effectiveJavaTypeUse().getName() :
           getMappingList().get(getMappingList().size() - 1).getToType().prettyPrint();
  }
  eq SendTypeEndpointDefinition.targetTypeName() {
    return getMappingList().get(getMappingList().size() - 1).getToType().prettyPrint();
  }

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
//  eq ReceiveFromRestDefinition.suitableDefaultMapping() {
//    String typeName = getMappingList().isEmpty() ?
//        getToken().getJavaTypeUse().getName() :
//        getMappingList().get(0).getFromType().prettyPrint();
//    switch(typeName) {
//      case "int":
//      case "Integer": return ragconnect().defaultStringToIntMapping();
//      case "short":
//      case "Short": return ragconnect().defaultStringToShortMapping();
//      case "long":
//      case "Long": return ragconnect().defaultStringToLongMapping();
//      case "float":
//      case "Float": return ragconnect().defaultStringToFloatMapping();
//      case "double":
//      case "Double": return ragconnect().defaultStringToDoubleMapping();
//      case "char":
//      case "Character": return ragconnect().defaultStringToCharMapping();
//      default: return null;
//    }
//  }
//  eq SendToRestDefinition.suitableDefaultMapping() {
//    String typeName = getMappingList().isEmpty() ?
//        getToken().getJavaTypeUse().getName() :
//        getMappingList().get(getMappingList().size() - 1).getFromType().prettyPrint();
//    switch(typeName) {
//      case "int":
//      case "Integer": return ragconnect().defaultIntToStringMapping();
//      case "short":
//      case "Short": return ragconnect().defaultShortToStringMapping();
//      case "long":
//      case "Long": return ragconnect().defaultLongToStringMapping();
//      case "float":
//      case "Float": return ragconnect().defaultFloatToStringMapping();
//      case "double":
//      case "Double": return ragconnect().defaultDoubleToStringMapping();
//      case "char":
//      case "Character": return ragconnect().defaultCharToStringMapping();
//      default: return null;
//    }
//  }
229
230
231
232
233

  // --- isByteArray ---
  syn boolean MappingDefinitionType.isByteArray() = false;
  eq JavaArrayMappingDefinitionType.isByteArray() = getType().getName().equals("byte");

234
235
236
//  // --- isString ---
//  syn boolean MappingDefinitionType.isString() = false;
//  eq JavaMappingDefinitionType.isString() = getType().getName().equals("String");
René Schöne's avatar
René Schöne committed
237

238
  // --- allMappingDefinitions ---
239
  syn java.util.List<MappingDefinition> RagConnect.allMappingDefinitions() {
240
241
    java.util.List<MappingDefinition> result = new java.util.ArrayList<>();
    getMappingDefinitionList().iterator().forEachRemaining(result::add);
René Schöne's avatar
René Schöne committed
242
    // byte[] conversion
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    result.add(defaultBytesToIntMapping());
    result.add(defaultBytesToShortMapping());
    result.add(defaultBytesToLongMapping());
    result.add(defaultBytesToFloatMapping());
    result.add(defaultBytesToDoubleMapping());
    result.add(defaultBytesToCharMapping());
    result.add(defaultBytesToStringMapping());
    result.add(defaultIntToBytesMapping());
    result.add(defaultShortToBytesMapping());
    result.add(defaultLongToBytesMapping());
    result.add(defaultFloatToBytesMapping());
    result.add(defaultDoubleToBytesMapping());
    result.add(defaultCharToBytesMapping());
    result.add(defaultStringToBytesMapping());
257
258
259
260
261
262
263
264
265
266
267
268
269
//    // string conversion
//    result.add(defaultStringToIntMapping());
//    result.add(defaultStringToShortMapping());
//    result.add(defaultStringToLongMapping());
//    result.add(defaultStringToFloatMapping());
//    result.add(defaultStringToDoubleMapping());
//    result.add(defaultStringToCharMapping());
//    result.add(defaultIntToStringMapping());
//    result.add(defaultShortToStringMapping());
//    result.add(defaultLongToStringMapping());
//    result.add(defaultFloatToStringMapping());
//    result.add(defaultDoubleToStringMapping());
//    result.add(defaultCharToStringMapping());
270
271
272
    return result;
  }
}