use of com.palantir.conjure.spec.FieldName in project conjure-postman by palantir.
the class TemplateTypeVisitor method visitReference.
@SuppressWarnings("PreferSafeLoggingPreconditions")
@Override
public JsonNode visitReference(TypeName value) {
TypeDefinition definition = types.get(value);
TemplateTypeVisitor visitor = this;
return definition.accept(new TypeDefinition.Visitor<JsonNode>() {
@Override
public JsonNode visitAlias(AliasDefinition value) {
JsonNode wrapped = value.getAlias().accept(visitor);
if (wrapped instanceof TextNode) {
return new TextNode(String.format("{{ %s(%s) }}", value.getTypeName().getName(), wrapped.toString().replaceAll("[\"{}]", "")));
}
return wrapped;
}
@Override
public JsonNode visitEnum(EnumDefinition value) {
return new TextNode(value.getValues().stream().map(EnumValueDefinition::getValue).collect(Collectors.joining("|")));
}
@Override
public JsonNode visitObject(ObjectDefinition value) {
if (seenTypeStack.contains(value.getTypeName())) {
return new TextNode(String.format("{{%s}}", value.getTypeName().getName()));
}
seenTypeStack.push(value.getTypeName());
ObjectNode node = objectMapper.createObjectNode();
value.getFields().forEach(fieldDefinition -> node.set(fieldDefinition.getFieldName().get(), fieldDefinition.getType().accept(visitor)));
Preconditions.checkState(seenTypeStack.pop().equals(value.getTypeName()));
return node;
}
@Override
public JsonNode visitUnion(UnionDefinition value) {
if (value.getUnion().isEmpty()) {
return null;
} else {
if (seenTypeStack.contains(value.getTypeName())) {
return new TextNode(String.format("{{%s}}", value.getTypeName().getName()));
}
seenTypeStack.push(value.getTypeName());
String unionTypes = value.getUnion().stream().map(FieldDefinition::getFieldName).map(FieldName::get).collect(Collectors.joining("|"));
ObjectNode templates = objectMapper.createObjectNode();
value.getUnion().forEach(field -> templates.set(field.getFieldName().get(), field.getType().accept(visitor)));
JsonNode union = objectMapper.createObjectNode().put("type", unionTypes).set("oneOf", templates);
Preconditions.checkState(seenTypeStack.pop().equals(value.getTypeName()));
return union;
}
}
@Override
public JsonNode visitUnknown(String _unknownType) {
return new TextNode("{{UNKNOWN}}");
}
});
}
use of com.palantir.conjure.spec.FieldName in project conjure-java by palantir.
the class BeanBuilderGenerator method createField.
private EnrichedField createField(FieldName fieldName, FieldDefinition field) {
FieldSpec.Builder spec = FieldSpec.builder(typeMapper.getClassName(field.getType()), JavaNameSanitizer.sanitize(fieldName), Modifier.PRIVATE);
if (field.getType().accept(TypeVisitor.IS_LIST)) {
spec.initializer("new $T<>()", ArrayList.class);
} else if (field.getType().accept(TypeVisitor.IS_SET)) {
spec.initializer("new $T<>()", LinkedHashSet.class);
} else if (field.getType().accept(TypeVisitor.IS_MAP)) {
spec.initializer("new $T<>()", LinkedHashMap.class);
} else if (field.getType().accept(TypeVisitor.IS_OPTIONAL)) {
spec.initializer("$T.empty()", asRawType(typeMapper.getClassName(field.getType())));
} else if (field.getType().accept(MoreVisitors.IS_INTERNAL_REFERENCE)) {
com.palantir.conjure.spec.TypeName name = field.getType().accept(TypeVisitor.REFERENCE);
typeMapper.getType(name).filter(definition -> definition.accept(TypeDefinitionVisitor.IS_ALIAS)).map(definition -> definition.accept(TypeDefinitionVisitor.ALIAS)).ifPresent(aliasDefinition -> {
Type aliasType = aliasDefinition.getAlias();
if (aliasType.accept(MoreVisitors.IS_COLLECTION) || aliasType.accept(TypeVisitor.IS_OPTIONAL)) {
spec.initializer("$T.empty()", typeMapper.getClassName(field.getType()));
}
});
}
return EnrichedField.of(fieldName, field, spec.build());
}
Aggregations