@Override public String getUnionMemberKey() { return getFullName(); }
public void markEncountered(DataSchema schema) { if (schema instanceof NamedDataSchema) { _alreadyEncountered.add(((NamedDataSchema) schema).getFullName()); } }
private void checkName(NamedDataSchema older, NamedDataSchema newer) { if (_options.isCheckNames() && older.getFullName().equals(newer.getFullName()) == false) { appendMessage(CompatibilityMessage.Impact.BREAKS_NEW_AND_OLD_READERS, "name changed from %s to %s", older.getFullName(), newer.getFullName()); } }
/** * Return the {@link DataSchema}'s language binding name. * This is the fully qualified name for the generated data model to resolve potential name conflict. * * @return the {@link DataSchema}'s language binding name. */ @Override public String getBindingName() { return (_package == null || _package.isEmpty()) ? getFullName() : _package + "." + getName(); }
@Override public boolean evaluate(DataElement element) { DataSchema schema = element.getSchema(); return (schema != null && schema instanceof NamedDataSchema && ((NamedDataSchema) schema).getFullName().equals(_name)); }
/** * Determines how a type from the original schema should be encoded. * * @param originallyInlined identifies if the provided type was originally inlined. * @return the {@link TypeRepresentation} to use when encoding a type. */ protected TypeRepresentation selectTypeRepresentation(DataSchema schema, boolean originallyInlined) { boolean firstEncounter = true; if (schema instanceof NamedDataSchema) { String fullName = ((NamedDataSchema) schema).getFullName(); firstEncounter = !_alreadyEncountered.contains(fullName); } else if (schema instanceof PrimitiveDataSchema) { return TypeRepresentation.DECLARED_INLINE; } switch (_typeReferenceFormat) { case PRESERVE: return originallyInlined ? TypeRepresentation.DECLARED_INLINE : TypeRepresentation.REFERENCED_BY_NAME; case DENORMALIZE: return firstEncounter ? TypeRepresentation.DECLARED_INLINE : TypeRepresentation.REFERENCED_BY_NAME; case MINIMIZE: return TypeRepresentation.REFERENCED_BY_NAME; default: throw new IllegalArgumentException("Unrecognized enum symbol: " + _typeReferenceFormat); } }
/** * Coerces a schema property value to a DataMap or, if it cannot be coerced, throws an exception. * If the value is a DataMap, return it. If the value is null, return an empty DataMap. * @param schema provides the schema this property belongs to, for error reporting purposes. * @param name provides the schema's property path to this value as a string, for error reporting purposes. * @param value provides the property value to coerce. * @return the property value, coerced to a DataMap. * @throws IllegalArgumentException if the property value cannot be coerced to a DataMap. */ private DataMap coercePropertyToDataMapOrFail(NamedDataSchema schema, String name, Object value) { if (value == null) { return new DataMap(); } if (!(value instanceof DataMap)) { throw new IllegalArgumentException("'" + name + "' in " + schema.getFullName() + " must be of type DataMap, but is: " + value.getClass()); } return (DataMap) value; }
/** * Checks that the schema name and namespace match the file name and path. These must match for * FileDataSchemaResolver to find a schema pdscs by fully qualified name. * */ private void validateSchemaWithFilepath(File schemaSourceFile, DataSchema schema) { if(schemaSourceFile != null && schemaSourceFile.isFile() && schema instanceof NamedDataSchema) { NamedDataSchema namedDataSchema = (NamedDataSchema)schema; String namespace = namedDataSchema.getNamespace(); if(!FileUtil.removeFileExtension(schemaSourceFile.getName()).equalsIgnoreCase(namedDataSchema.getName())) { throw new IllegalArgumentException(namedDataSchema.getFullName() + " has name that does not match filename '" + schemaSourceFile.getAbsolutePath() + "'"); } String directory = schemaSourceFile.getParentFile().getAbsolutePath(); if(!directory.endsWith(namespace.replace('.', File.separatorChar))) { throw new IllegalArgumentException(namedDataSchema.getFullName() + " has namespace that does not match " + "file path '" + schemaSourceFile.getAbsolutePath() + "'"); } } }
/** * Gets the data schema name for a given java type. * @param type to get a schema for. Has to be a named data type. */ public static String getSchemaName(Class<?> type) { DataSchema schema = getSchema(type); if (! (schema instanceof NamedDataSchema)) { throw new TemplateRuntimeException("Schema is unnamed in class: " + type.getName()); } return ((NamedDataSchema) schema).getFullName(); }
/** * Checks that the schema name and namespace match the file name and path. These must match for FileDataSchemaResolver to find a schema pdscs by fully qualified name. */ private void validateSchemaWithFilePath(File schemaSourceFile, DataSchema schema) { if (schemaSourceFile != null && schemaSourceFile.isFile() && schema instanceof NamedDataSchema) { final NamedDataSchema namedDataSchema = (NamedDataSchema) schema; final String namespace = namedDataSchema.getNamespace(); if (!FileUtil.removeFileExtension(schemaSourceFile.getName()).equalsIgnoreCase(namedDataSchema.getName())) { throw new IllegalArgumentException(namedDataSchema.getFullName() + " has name that does not match filename '" + schemaSourceFile.getAbsolutePath() + "'"); } final String directory = schemaSourceFile.getParentFile().getAbsolutePath(); if (!directory.endsWith(namespace.replace('.', File.separatorChar))) { throw new IllegalArgumentException(namedDataSchema.getFullName() + " has namespace that does not match " + "file path '" + schemaSourceFile.getAbsolutePath() + "'"); } } }
sb.append(((NamedDataSchema) key).getFullName()).append(" (named schema)");
protected void writeSchemaName(NamedDataSchema schema) throws IOException { _builder.writeString(_currentNamespace.equals(schema.getNamespace()) ? schema.getName() : schema.getFullName()); }
protected Map<String, NamedDataSchema> parseModels(DataList models) throws IOException { final Map<String, NamedDataSchema> parsedModels = new HashMap<String, NamedDataSchema>(); for (Object modelObj : models) { NamedDataSchema dataSchema; if (modelObj instanceof DataMap) { DataMap model = (DataMap)modelObj; dataSchema = (NamedDataSchema) RestSpecCodec.textToSchema(_dataCodec.mapToString(model), _dataSchemaResolver); } else if (modelObj instanceof String) { String str = (String)modelObj; dataSchema = (NamedDataSchema) RestSpecCodec.textToSchema(str, _dataSchemaResolver); } else { throw new IOException("Found " + modelObj.getClass() + " in models list; Models must be strings or DataMaps."); } parsedModels.put(dataSchema.getFullName(), dataSchema); } return parsedModels; }
private void addRelatedModels(ResourceSchema resourceSchema, DataMap models) throws IOException { Map<String, DataMap> relatedSchemas; synchronized (this) { relatedSchemas = _relatedSchemaCache.get(resourceSchema); if (relatedSchemas == null) { relatedSchemas = new HashMap<String, DataMap>(); final Node<?> node = _relationships.getRelationships(resourceSchema); final Iterator<Node<NamedDataSchema>> schemaItr = node.getAdjacency(NamedDataSchema.class).iterator(); while (schemaItr.hasNext()) { final NamedDataSchema currResource = (NamedDataSchema) schemaItr.next().getObject(); relatedSchemas.put(currResource.getFullName(), _codec.stringToMap(currResource.toString())); } _relatedSchemaCache.put(resourceSchema, relatedSchemas); } } models.putAll(relatedSchemas); }
private void renderDataModel(NamedDataSchema schema, DataMap outputMap) throws IOException { final DataMap models = outputMap.getDataMap("models"); final DataMap schemaData = _codec.stringToMap(schema.toString()); models.put(schema.getFullName(), schemaData); }
/** * Get the .pdl escaped source identifier for the given named type. * If the type is imported, it's simple name will be returned, else it's fully qualified name will be returned. * * @param schema provides the named schema to get a .pdl escaped source identifier for. * @return a escaped source identifier. */ private String toTypeIdentifier(NamedDataSchema schema) { if (schema.getNamespace().equals(_namespace) || (_importsByLocalName.containsKey(schema.getName()) && _importsByLocalName.get(schema.getName()).getNamespace().equals(schema.getNamespace()))) { return escapeIdentifier(schema.getName()); } else { return escapeIdentifier(schema.getFullName()); } }
private void connectSchemaToResource(VisitContext visitContext, final NamedDataSchema schema) { final Node<NamedDataSchema> schemaNode = _relationships.get(schema); _dataModels.put(schema.getFullName(), schema); final DataSchemaTraverse traveler = new DataSchemaTraverse(); traveler.traverse(schema, new DataSchemaTraverse.Callback() { @Override public void callback(List<String> path, DataSchema nestedSchema) { if (nestedSchema instanceof RecordDataSchema && nestedSchema != schema) { final RecordDataSchema nestedRecordSchema = (RecordDataSchema) nestedSchema; _dataModels.put(nestedRecordSchema.getFullName(), nestedRecordSchema); final Node<RecordDataSchema> node = _relationships.get(nestedRecordSchema); schemaNode.addAdjacentNode(node); } } }); final Node<ResourceSchema> resourceNode = _relationships.get(visitContext.getParentSchema()); resourceNode.addAdjacentNode(schemaNode); schemaNode.addAdjacentNode(resourceNode); } };
if (!foundTypes.containsKey(namedDataSchema.getFullName())) foundTypes.put(namedDataSchema.getFullName(), namedDataSchema);
/** * Parse list of Data objects. * * The {{DataSchema}'s parsed are in {{#topLevelDataSchemas}. * Parse errors are in {{#errorMessageBuilder} and indicated * by {{#hasError()}. * * @param document provides the source code in AST form */ private String parse(DocumentContext document) throws ParseException { if (document.namespaceDeclaration() != null) { setCurrentNamespace( document.namespaceDeclaration().qualifiedIdentifier().value); } else { setCurrentNamespace(""); } setCurrentImports(document.importDeclarations()); NamedDataSchema schema = parseNamedType(document.namedTypeDeclaration()); topLevelSchemas.add(schema); return schema.getFullName(); }
private ClassTemplateSpec generateNamedSchema(NamedDataSchema schema) { pushCurrentLocation(_schemaResolver.nameToDataSchemaLocations().get(schema.getFullName())); final String className = classNameForNamedSchema(schema); checkForClassNameConflict(className, schema); final ClassTemplateSpec templateClass; switch (schema.getType()) { case RECORD: templateClass = generateRecord((RecordDataSchema) schema); break; case ENUM: templateClass = generateEnum((EnumDataSchema) schema); break; case FIXED: templateClass = generateFixed((FixedDataSchema) schema); break; default: throw unrecognizedSchemaType(null, null, schema); } popCurrentLocation(); return templateClass; }