From 110ed3a735229111ae2985925ef33933c6352ad4 Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Fri, 26 Jan 2024 10:52:42 +0100 Subject: [PATCH 01/11] #369 added json models and tests for validation --- .../blueprint/model/BlueprintDatatype.java | 9 +++ .../blueprint/model/BlueprintDatum.java | 9 ++- .../blueprint/model/BlueprintRedeemer.java | 12 ---- .../blueprint/model/BlueprintSchema.java | 57 +++++++++++++++++++ .../plutus/blueprint/model/Validator.java | 4 +- .../blueprint/PlutusBlueprintLoaderTest.java | 5 ++ 6 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatatype.java delete mode 100644 plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintRedeemer.java create mode 100644 plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintSchema.java diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatatype.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatatype.java new file mode 100644 index 00000000..45b8b5d4 --- /dev/null +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatatype.java @@ -0,0 +1,9 @@ +package com.bloxbean.cardano.client.plutus.blueprint.model; + +public enum BlueprintDatatype { + integer, + bytes, + list, + map, + constructor +} diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatum.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatum.java index 989e061d..58808ce6 100644 --- a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatum.java +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatum.java @@ -4,9 +4,16 @@ import lombok.Data; import lombok.NoArgsConstructor; +import java.util.List; +import java.util.Map; + @Data @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) public class BlueprintDatum { - //TODO -- datum fields + private String title; + private String description; + private Object purpose; + private BlueprintSchema schema; + } diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintRedeemer.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintRedeemer.java deleted file mode 100644 index 1070ae67..00000000 --- a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintRedeemer.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.bloxbean.cardano.client.plutus.blueprint.model; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@NoArgsConstructor -@JsonIgnoreProperties(ignoreUnknown = true) -public class BlueprintRedeemer { - //TODO -- redeemer fields -} diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintSchema.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintSchema.java new file mode 100644 index 00000000..345ede17 --- /dev/null +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintSchema.java @@ -0,0 +1,57 @@ +package com.bloxbean.cardano.client.plutus.blueprint.model; + +import com.fasterxml.jackson.annotation.JsonAlias; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; +import java.util.Map; + +@Data +@NoArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class BlueprintSchema { + // for nested schemas + private List anyOf; + private List oneOf; + private List allOf; + private List notOf; + + // base fields + private String title; + private String description; + private BlueprintDatatype dataType; + @JsonAlias({"$comment", "comment"}) + private String comment; + + // Datatype = "bytes" + @JsonAlias("enum") + private String[] enumLiterals; // hex-encoded Stringliterals + private int maxLength; + private int minLength; + + //Datatype = "integer" + private int multipleOf; + private int maximum; + private int exclusiveMaximum; + private int minimum; + private int exclusiveMinimum; + + //Datatype = "list" + private List items; + private int maxItems; + private int minItems; + private boolean uniqueItems; + + // Datatype = "map" + private BlueprintSchema keys; + private BlueprintSchema values; +// private int maxItems; // Field already defined in datatype list, leaving it here to avoid confusion to the CIP57 spec +// private int minItems; // Field already defined in datatype list, leaving it here to avoid confusion to the CIP57 spec + + // Datatype = "constructor" + private int index; + private List> fields; + +} diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/Validator.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/Validator.java index 4c6adfc1..01f01d0f 100644 --- a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/Validator.java +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/Validator.java @@ -14,8 +14,8 @@ public class Validator { private String title; private BlueprintDatum datum; - private BlueprintRedeemer redeemer; - private List parameters; + private BlueprintDatum redeemer; + private List parameters; private String compiledCode; private String hash; } diff --git a/plutus/src/test/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoaderTest.java b/plutus/src/test/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoaderTest.java index dfb7f2db..99b51a0e 100644 --- a/plutus/src/test/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoaderTest.java +++ b/plutus/src/test/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoaderTest.java @@ -1,5 +1,6 @@ package com.bloxbean.cardano.client.plutus.blueprint; +import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintDatatype; import com.bloxbean.cardano.client.plutus.blueprint.model.PlutusContractBlueprint; import com.bloxbean.cardano.client.plutus.blueprint.model.PlutusVersion; import com.bloxbean.cardano.client.plutus.spec.PlutusScript; @@ -27,6 +28,10 @@ void loadBlueprint_fromInputStream() { assertThat(blueprint.getValidators().get(0).getTitle()).isEqualTo("hello_world"); assertThat(blueprint.getValidators().get(0).getCompiledCode()).isEqualTo("58ad0100003232322225333004323253330063372e646e64004dd7198009801002240009210d48656c6c6f2c20576f726c64210013233300100137586600460066600460060089000240206eb8cc008c00c019200022253335573e004294054ccc024cdc79bae300a00200114a226660060066016004002294088c8ccc0040052000003222333300a3370e008004016466600800866e0000d2002300d001001235573c6ea8004526165734ae855d11"); assertThat(blueprint.getValidators().get(0).getHash()).isEqualTo("5e1e8fa84f2b557ddc362329413caa3fd89a1be26bfd24be05ce0a02"); + assertThat(blueprint.getValidators().get(0).getDatum().getTitle()).isEqualTo("Datum"); + assertThat(blueprint.getValidators().get(0).getDatum().getPurpose()).isEqualTo("spend"); + assertThat(blueprint.getValidators().get(0).getDatum().getSchema().getAnyOf().get(0).getTitle()).isEqualTo("Datum"); + assertThat(blueprint.getValidators().get(0).getDatum().getSchema().getAnyOf().get(0).getDataType()).isEqualTo(BlueprintDatatype.constructor); assertThat(blueprint.getValidators().get(1).getTitle()).isEqualTo("validator-2"); assertThat(blueprint.getValidators().get(1).getCompiledCode()).isEqualTo("581801000032223253330043370e00290020a4c2c6eb40095cd1"); From 06613903a314df7aeed31dd9755b4f766c469984 Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Tue, 30 Jan 2024 14:10:50 +0100 Subject: [PATCH 02/11] #369 added parsing of CIP57 Blueprints. Currenlty implemented datatypes: List, Constr, bytes, integer, String, boolean ToDo: Map, tuple, unit --- .../BlueprintAnnotationProcessor.java | 96 ++++++++++ .../DataTypeProcessUtil.java | 176 ++++++++++++++++++ .../FieldSpecProcessor.java | 141 ++++++++++++++ .../blueprint_processor/JavaFileUtil.java | 66 +++++++ .../ValidatorProcessor.java | 86 +++++++++ .../javax.annotation.processing.Processor | 1 + .../annotation/blueprint/BluePrintTest.java | 85 +++++++++ .../blueprint/model/BasicTypesBlueprint.java | 7 + .../model/ComplexTypesBlueprint.java | 7 + .../blueprint/model/HelloWorldBlueprint.java | 7 + .../blueprint/model/ListBlueprint.java | 7 + .../test/resources/BasicTypesBlueprint.json | 79 ++++++++ .../test/resources/ComplexTypeBlueprint.json | 101 ++++++++++ .../test/resources/Hello_world-Blueprint.json | 69 +++++++ .../src/test/resources/ListBlueprint.json | 75 ++++++++ .../client/plutus/annotation/Blueprint.java | 18 ++ .../blueprint/PlutusBlueprintLoader.java | 102 +++++++++- .../blueprint/model/BlueprintDatatype.java | 13 +- .../blueprint/model/BlueprintSchema.java | 34 +++- .../model/PlutusContractBlueprint.java | 2 + .../plutus/blueprint/model/Validator.java | 1 + .../resources/blueprint/list-blueprint.json | 75 ++++++++ 22 files changed, 1234 insertions(+), 14 deletions(-) create mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java create mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java create mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java create mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/JavaFileUtil.java create mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/ValidatorProcessor.java create mode 100644 annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java create mode 100644 annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java create mode 100644 annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java create mode 100644 annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java create mode 100644 annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java create mode 100644 annotation-processor/src/test/resources/BasicTypesBlueprint.json create mode 100644 annotation-processor/src/test/resources/ComplexTypeBlueprint.json create mode 100644 annotation-processor/src/test/resources/Hello_world-Blueprint.json create mode 100644 annotation-processor/src/test/resources/ListBlueprint.json create mode 100644 plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java create mode 100644 plutus/src/test/resources/blueprint/list-blueprint.json diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java new file mode 100644 index 00000000..8986bd3d --- /dev/null +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java @@ -0,0 +1,96 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; +import com.bloxbean.cardano.client.plutus.blueprint.PlutusBlueprintLoader; +import com.bloxbean.cardano.client.plutus.blueprint.model.*; +import com.google.auto.service.AutoService; +import lombok.extern.slf4j.Slf4j; + +import javax.annotation.processing.*; +import javax.lang.model.element.Element; +import javax.lang.model.element.TypeElement; +import javax.tools.Diagnostic; +import java.io.File; +import java.util.*; + +@AutoService(Processor.class) +@Slf4j +public class BlueprintAnnotationProcessor extends AbstractProcessor { + + private Messager messager; + private List typeElements = new ArrayList<>(); + private Blueprint annotation; + private ValidatorProcessor validatorProcessor; + + @Override + public synchronized void init(ProcessingEnvironment processingEnv) { + super.init(processingEnv); + messager = processingEnv.getMessager(); + } + + @Override + public Set getSupportedAnnotationTypes() { + Set annotataions = new LinkedHashSet(); + annotataions.add(Blueprint.class.getCanonicalName()); + return annotataions; + } + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + System.out.println("Processing Blueprint annotation"); + for (TypeElement annotation : annotations) { + Set elements = roundEnv.getElementsAnnotatedWith(annotation); + for (Element element : elements) { + if (element instanceof TypeElement) { + TypeElement typeElement = (TypeElement) element; + typeElements.add(typeElement); + + } + } + } + + for(TypeElement typeElement : typeElements) { + annotation = typeElement.getAnnotation(Blueprint.class); + if (annotation == null) { + log.error("Blueprint annotation not found for class {}", typeElement.getSimpleName()); + return false; + } else { + validatorProcessor = new ValidatorProcessor(annotation, processingEnv); + } + + File blueprintFile = getFile(); + + if (blueprintFile == null || !blueprintFile.exists()) { + log.error("Blueprint file {} not found", annotation.fileInRessources()); + return false; + } + PlutusContractBlueprint plutusContractBlueprint = PlutusBlueprintLoader.loadBlueprint(blueprintFile); + + for (Validator validator : plutusContractBlueprint.getValidators()) { + validatorProcessor.processValidator(validator); + } + } + return true; + } + + private File getFile() { + File blueprintFile = null; + if(!annotation.file().isEmpty()) + blueprintFile = new File(annotation.file()); + if(!annotation.fileInRessources().isEmpty()) + blueprintFile = JavaFileUtil.getFileFromRessourcers(annotation.fileInRessources()); + if(blueprintFile == null || !blueprintFile.exists()) { + log.error("Blueprint file {} not found", annotation.file()); + return null; + } + return blueprintFile; + } + + + private void error(Element e, String msg, Object... args) { + messager.printMessage( + Diagnostic.Kind.ERROR, + String.format(msg, args), + e); + } +} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java new file mode 100644 index 00000000..020b978b --- /dev/null +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java @@ -0,0 +1,176 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; +import com.bloxbean.cardano.client.plutus.annotation.Constr; +import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintSchema; +import com.squareup.javapoet.*; + +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.Modifier; +import java.util.ArrayList; +import java.util.List; + +import static com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintDatatype.*; + +public class DataTypeProcessUtil { + + private final FieldSpecProcessor fieldSpecProcessor; + private final Blueprint annotation; + private final ProcessingEnvironment processingEnv; + + public DataTypeProcessUtil(FieldSpecProcessor fieldSpecProcessor, Blueprint annotation, ProcessingEnvironment processingEnv) { + this.fieldSpecProcessor = fieldSpecProcessor; + this.annotation = annotation; + this.processingEnv = processingEnv; + } + + /** + * Creates a FieldSpec for a given integer schema + * @param javaDoc + * @param schema + * @return + */ + public FieldSpec processIntegerDataType(String javaDoc, BlueprintSchema schema) { + if(schema.getDataType() != integer) + throw new IllegalArgumentException("Schema is not of type integer"); + + return FieldSpec.builder(int.class, schema.getTitle()) + .addModifiers(Modifier.PRIVATE) + .addJavadoc(javaDoc) + .build(); + } + + /** + * Creates a FieldSpec for a given bytes schema + * @param javaDoc + * @param schema + * @return + */ + public FieldSpec processBytesDataType(String javaDoc, BlueprintSchema schema) { + if(schema.getDataType() != bytes) + throw new IllegalArgumentException("Schema is not of type bytes"); + + return FieldSpec.builder(byte[].class, schema.getTitle()) + .addModifiers(Modifier.PRIVATE) + .addJavadoc(javaDoc) + .build(); + + } + + /** + * Creates a FieldSpec for a given list schema + * @param javaDoc + * @param schema + * @return + */ + public FieldSpec processListDataType(String javaDoc, BlueprintSchema schema) { + if(schema.getDataType() != list) + throw new IllegalArgumentException("Schema is not of type list"); + + TypeName fieldClass = getListTypeName(schema); + return FieldSpec.builder(fieldClass, schema.getTitle()) + .addModifiers(Modifier.PRIVATE) + .addJavadoc(javaDoc) + .build(); + } + + private TypeName getListTypeName(BlueprintSchema field) { + return ParameterizedTypeName.get(ClassName.get("java.util","List"), getInnerType(field.getItems())); + + } + + private TypeName getInnerType(BlueprintSchema items) { + if(items.getDataType() == null) // Case for constructor data type + { + FieldSpec constr = fieldSpecProcessor.createDatumFieldSpec(items, "Constr", items.getTitle(), ""); + return constr.type; + } + switch (items.getDataType()) { + case bytes: + return TypeName.get(Byte[].class); + case integer: + return TypeName.get(Integer.class); + case string: + return TypeName.get(String.class); + case bool: + return TypeName.get(Boolean.class); + case list: + return getListTypeName(items); + default: + return TypeName.get(String.class); + } + } + + /** + * Creates a FieldSpec for a given boolean schema + * @param javaDoc + * @param schema + * @return + */ + public FieldSpec processBoolDataType(String javaDoc, BlueprintSchema schema) { + if(schema.getDataType() != bool) + throw new IllegalArgumentException("Schema is not of type boolean"); + + return FieldSpec.builder(boolean.class, schema.getTitle()) + .addModifiers(Modifier.PRIVATE) + .addJavadoc(javaDoc) + .build(); + } + + /** + * Creates a FieldSpec for a given string schema + * @param javaDoc + * @param schema + * @return + */ + public FieldSpec processStringDataType(String javaDoc, BlueprintSchema schema) { + if(schema.getDataType() != string) + throw new IllegalArgumentException("Schema is not of type string"); + + return FieldSpec.builder(String.class, schema.getTitle()) + .addModifiers(Modifier.PRIVATE) + .addJavadoc(javaDoc) + .build(); + } + + public List processConstructorDataType(String javaDoc, BlueprintSchema schema, String className) { + List specs = new ArrayList<>(); + for (BlueprintSchema field : schema.getFields()) { + if(field.getDataType() != null) { + javaDoc += " Index: " + field.getIndex() ; + specs.addAll(fieldSpecProcessor.CreateFieldSpecForDataTypes(javaDoc, List.of(field), className)); + } else { + specs.add(fieldSpecProcessor.createDatumFieldSpec(field, "", field.getTitle(), className)); + } + } + if(schema.getFields().isEmpty()) { // TODO Fields is empty that's why no mint or burn is generated Enums + specs.add(createEnumAndAddToFields(schema, className)); + } + return specs; + } + + /** + * TODO need to check how an enum has to be presented + * @param schema + * @param className + * @return + */ + private FieldSpec createEnumAndAddToFields(BlueprintSchema schema, String className) { + AnnotationSpec constrAnnotationBuilder = AnnotationSpec.builder(Constr.class).addMember("alternative", "$L", schema.getIndex()).build(); + String fieldName = className + String.valueOf(schema.getIndex()) + schema.getTitle(); + TypeSpec enumConstr = TypeSpec.classBuilder(fieldName) + .addAnnotation(constrAnnotationBuilder) + .addModifiers(Modifier.PUBLIC) + .build(); + JavaFileUtil.createJavaFile(annotation.packageName(), enumConstr, enumConstr.name, processingEnv); + ClassName classIdentifier = ClassName.get(annotation.packageName(), enumConstr.name); + return FieldSpec.builder(classIdentifier, fieldName) + .addModifiers(Modifier.PRIVATE) + .addJavadoc("Index: " + schema.getIndex()) + .build(); + } + + public FieldSpec processMapDataType(String javaDoc, BlueprintSchema schema, String className) { + return null; + } +} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java new file mode 100644 index 00000000..505d1fb4 --- /dev/null +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java @@ -0,0 +1,141 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; +import com.bloxbean.cardano.client.plutus.annotation.Constr; +import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintDatatype; +import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintSchema; +import com.bloxbean.cardano.client.util.Tuple; +import com.squareup.javapoet.AnnotationSpec; +import com.squareup.javapoet.ClassName; +import com.squareup.javapoet.FieldSpec; +import com.squareup.javapoet.TypeSpec; +import lombok.Data; + +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.Modifier; +import java.util.ArrayList; +import java.util.List; + +public class FieldSpecProcessor { + + private final ProcessingEnvironment processingEnv; + private final Blueprint annotation; + private final DataTypeProcessUtil dataTypeProcessUtil; + + public FieldSpecProcessor(Blueprint annotation, ProcessingEnvironment processingEnv) { + this.annotation = annotation; + this.processingEnv = processingEnv; + this.dataTypeProcessUtil = new DataTypeProcessUtil(this, annotation, processingEnv); + } + + /** + * Collects all fields from a schema including the ones from allOf, anyOf, oneOf, noneOf and returns them as a list + * @param schema schema to collect fields from + * @return list of all fields + */ + public static Tuple> collectAllFields(BlueprintSchema schema) { + List toFields = new ArrayList<>(); + String javaDoc = ""; + if(schema.getAllOf() != null) { + toFields.addAll(schema.getAllOf()); + javaDoc = "AllOf"; + } else if(schema.getAnyOf() != null) { + toFields.addAll(schema.getAnyOf()); + javaDoc = "AnyOf"; + } else if(schema.getOneOf() != null) { + toFields.addAll(schema.getOneOf()); + javaDoc = "OneOf"; + } else { + toFields.add(schema); + } + return new Tuple<>(javaDoc, toFields); + } + + /** + * Creates a list of FieldSpecs for a given schema + * @param javaDoc + * @param schema + * @param className + * @return + */ + public List CreateFieldSpecForDataTypes(String javaDoc, BlueprintSchema schema, String className) { + List specs = new ArrayList<>(); + switch (schema.getDataType()) { + case bytes: + specs.add(dataTypeProcessUtil.processBytesDataType(javaDoc, schema)); + break; + case integer: + specs.add(dataTypeProcessUtil.processIntegerDataType(javaDoc, schema)); + break; + case bool: + specs.add(dataTypeProcessUtil.processBoolDataType(javaDoc, schema)); + break; + case list: + specs.add(dataTypeProcessUtil.processListDataType(javaDoc, schema)); + break; + case map: + specs.add(dataTypeProcessUtil.processMapDataType(javaDoc, schema, className)); + break; + case constructor: + specs.addAll(dataTypeProcessUtil.processConstructorDataType(javaDoc, schema, className)); + break; + case string: + specs.add(dataTypeProcessUtil.processStringDataType(javaDoc, schema)); + break; + default: + } + return specs; + } + + /** + * Creates a list of FieldSpecs for a given list of schemas + * @param javaDoc + * @param schemas + * @param className + * @return + */ + public List CreateFieldSpecForDataTypes(String javaDoc, List schemas, String className) { + List specs = new ArrayList<>(); + for (BlueprintSchema schema : schemas) { + specs.addAll(CreateFieldSpecForDataTypes(javaDoc, schema, className)); + } + return specs; + } + + /** + * Creates a FieldSpec for a given schema + * @param schema + * @param suffix + * @param title + * @param prefix + * @return + */ + public FieldSpec createDatumFieldSpec(BlueprintSchema schema, String suffix, String title, String prefix) { + String classNameString = JavaFileUtil.buildClassName(schema, suffix, title, prefix); + TypeSpec redeemerJavaFile = createDatumTypeSpec(schema, classNameString); + + ClassName className = ClassName.get(annotation.packageName(), redeemerJavaFile.name); + String fieldName = title + (schema.getDataType() == BlueprintDatatype.constructor ? String.valueOf(schema.getIndex()) : "") + suffix; + + return FieldSpec.builder(className, fieldName) + .addModifiers(Modifier.PRIVATE) + .build(); + } + + private TypeSpec createDatumTypeSpec(BlueprintSchema schema, String className) { + Tuple> allInnerSchemas = FieldSpecProcessor.collectAllFields(schema); + + List fields = CreateFieldSpecForDataTypes(allInnerSchemas._1, allInnerSchemas._2, className); + AnnotationSpec constrAnnotationBuilder = AnnotationSpec.builder(Constr.class).addMember("alternative", "$L", schema.getIndex()).build(); + + TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className) + .addModifiers(Modifier.PUBLIC) + .addFields(fields) + .addAnnotation(constrAnnotationBuilder) + .addAnnotation(Data.class); + + TypeSpec build = classBuilder.build(); + JavaFileUtil.createJavaFile(annotation.packageName(), build, className, processingEnv); + return build; + } +} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/JavaFileUtil.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/JavaFileUtil.java new file mode 100644 index 00000000..811de3bd --- /dev/null +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/JavaFileUtil.java @@ -0,0 +1,66 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor; + +import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintDatatype; +import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintSchema; +import com.squareup.javapoet.JavaFile; +import com.squareup.javapoet.TypeSpec; +import lombok.extern.slf4j.Slf4j; + +import javax.annotation.processing.ProcessingEnvironment; +import javax.tools.JavaFileObject; +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.nio.file.Path; + +@Slf4j +public class JavaFileUtil { + + /** + * First character has to be upper case when creating a new class + * @param s + * @return + */ + public static String firstUpperCase(String s) { + if(s == null || s.isEmpty()) + return s; + return s.substring(0, 1).toUpperCase() + s.substring(1); + } + + /** + * Creates a Java file from a TypeSpec with a given classname and package + * + * @param packageName + * @param build + * @param className + * @param processingEnv + */ + public static void createJavaFile(String packageName, TypeSpec build, String className, ProcessingEnvironment processingEnv) { + JavaFile javaFile = JavaFile.builder(packageName, build) + .build(); + + JavaFileObject builderFile = null; + try { + builderFile = processingEnv.getFiler() + .createSourceFile(className); + Writer writer = builderFile.openWriter(); + javaFile.writeTo(writer); + writer.close(); + } catch (IOException e) { + log.error("Error creating validator class", e); + } + } + + public static String buildClassName(BlueprintSchema schema, String suffix, String title, String prefix) { + String className = firstUpperCase(prefix) + firstUpperCase(title); + if(schema.getDataType() == BlueprintDatatype.constructor) + className += String.valueOf(schema.getIndex()); + className += firstUpperCase(suffix); // ToDO need to check for valid names + return className; + } + + public static File getFileFromRessourcers(String s) { + Path resourceDirectory = Path.of("src", "test", "resources", s); + return resourceDirectory.toFile(); + } +} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/ValidatorProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/ValidatorProcessor.java new file mode 100644 index 00000000..292fdf90 --- /dev/null +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/ValidatorProcessor.java @@ -0,0 +1,86 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; +import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintDatum; +import com.bloxbean.cardano.client.plutus.blueprint.model.Validator; +import com.squareup.javapoet.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.Modifier; +import java.util.ArrayList; +import java.util.List; + +public class ValidatorProcessor { + + private final Blueprint annotation; + private final ProcessingEnvironment processingEnv; + private final FieldSpecProcessor fieldSpecProcessor; + + public ValidatorProcessor(Blueprint annotation, ProcessingEnvironment processingEnv) { + this.annotation = annotation; + this.processingEnv = processingEnv; + this.fieldSpecProcessor = new FieldSpecProcessor(annotation, processingEnv); + } + + /** + * Validator Definition will be converted to a Java class. + * All definitions within are fields and/or seperate classes + * @param validator + */ + public void processValidator(Validator validator) { + // preparation of standard fields + String packageName = annotation.packageName() + "." + validator.getTitle().split("\\.")[0]; + String title = validator.getTitle().split("\\.")[1]; + title = JavaFileUtil.firstUpperCase(title); + String validatorClassName = title + "Validator"; // ToDO need to check for valid names + List fields = ValidatorProcessor.getFieldSpecsForValidator(validator); + + // processing of fields + if(validator.getRedeemer() != null) + fields.add(fieldSpecProcessor.createDatumFieldSpec(validator.getRedeemer().getSchema(), "Redeemer", title, "")); + if(validator.getDatum() != null) + fields.add(fieldSpecProcessor.createDatumFieldSpec(validator.getDatum().getSchema(), "Datum", title, "")); + if(validator.getParameters() != null) { + for (BlueprintDatum parameter : validator.getParameters()) { + fields.add(fieldSpecProcessor.createDatumFieldSpec(parameter.getSchema(), "Parameter", title, "")); + } + } + + // building and saving of class + TypeSpec build = TypeSpec.classBuilder(validatorClassName) + .addModifiers(Modifier.PUBLIC) + .addJavadoc("Auto generated code. DO NOT MODIFY") + .addAnnotation(Data.class) + .addAnnotation(AllArgsConstructor.class) + .addAnnotation(NoArgsConstructor.class) + .addFields(fields) + .build(); + JavaFileUtil.createJavaFile(packageName, build, validatorClassName, processingEnv); + } + + + + public static List getFieldSpecsForValidator(Validator validator) { + List fields = new ArrayList<>(); + fields.add(FieldSpec.builder(String.class, "title") + .addModifiers(Modifier.PRIVATE) // need to fix AnnotationProcessor for final variables + .initializer("$S", validator.getTitle()) + .build()); + fields.add(FieldSpec.builder(String.class, "description") + .addModifiers(Modifier.PRIVATE) + .initializer("$S", validator.getDescription()) + .build()); + fields.add(FieldSpec.builder(String.class, "compiledCode") + .addModifiers(Modifier.PRIVATE) + .initializer("$S", validator.getCompiledCode()) + .build()); + fields.add(FieldSpec.builder(String.class, "hash") + .addModifiers(Modifier.PRIVATE) + .initializer("$S", validator.getHash()) + .build()); + return fields; + } +} diff --git a/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor index c5347670..f6e2a4a2 100644 --- a/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor +++ b/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -1 +1,2 @@ com.bloxbean.cardano.client.plutus.annotation.processor.ConstrAnnotationProcessor +com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.BlueprintAnnotationProcessor \ No newline at end of file diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java new file mode 100644 index 00000000..674b68d6 --- /dev/null +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java @@ -0,0 +1,85 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint; + +import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemerNestedA; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldDatum; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldDatumConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldRedeemerConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Basic_typesRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Basic_typesRedeemerConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemerConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemerNestedANestedB; +import com.bloxbean.cardano.client.plutus.spec.ConstrPlutusData; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class BluePrintTest { + + @Test + public void test() throws ClassNotFoundException { +// Para + + System.out.println(""); + } + + + @Test + public void fillDatum() { + Hello_worldDatum hello_worldDatum = new Hello_worldDatum(); + hello_worldDatum.setOwner(new byte[]{-88, -77, -72, 20, -90, 94, 94, 67, 119, 127, -119, -77, 12, -121, -22, -60, -29, 57, 106, -101, -100, 36, -51, 8, -42, 96, -108, -103}); + Hello_worldDatumConverter hello_worldDatumConverter = new Hello_worldDatumConverter(); + ConstrPlutusData plutusData = hello_worldDatumConverter.toPlutusData(hello_worldDatum); + + assertEquals("d8799f581ca8b3b814a65e5e43777f89b30c87eac4e3396a9b9c24cd08d6609499ff", plutusData.serializeToHex()); + } + + @Test + public void fillRedeemer() { + Hello_worldRedeemer hello_worldRedeemer = new Hello_worldRedeemer(); + hello_worldRedeemer.setMsg("Hello, World".getBytes(StandardCharsets.UTF_8)); + Hello_worldRedeemerConverter hello_worldRedeemerConverter = new Hello_worldRedeemerConverter(); + ConstrPlutusData plutusData = hello_worldRedeemerConverter.toPlutusData(hello_worldRedeemer); + + assertEquals("d8799f4c48656c6c6f2c20576f726c64ff", plutusData.serializeToHex()); + } + + @Test + public void integerTest() { + Basic_typesRedeemer basic_typesRedeemer = new Basic_typesRedeemer(); + basic_typesRedeemer.setMsg("Hello, World"); + basic_typesRedeemer.setCounter(10); + + Basic_typesRedeemerConverter basic_typesRedeemerConverter = new Basic_typesRedeemerConverter(); + ConstrPlutusData plutusData = basic_typesRedeemerConverter.toPlutusData(basic_typesRedeemer); + + assertEquals("d8799f4c48656c6c6f2c20576f726c640aff", plutusData.serializeToHex()); + } + + @Test + public void nestedTypeTest() { + Complex_structuresRedeemer complex_structuresRedeemer = new Complex_structuresRedeemer(); + complex_structuresRedeemer.setOwner("Hello, World".getBytes(StandardCharsets.UTF_8)); + Complex_structuresRedeemerNestedA a = new Complex_structuresRedeemerNestedA(); + a.setMsg("Hello, World from A"); + a.setCount(10); + + Complex_structuresRedeemerNestedANestedB b = new Complex_structuresRedeemerNestedANestedB(); + b.setCount2(20); + b.setMsg2("Hello, World from B"); + a.setNestedB(b); + complex_structuresRedeemer.setNestedA(a); + + Complex_structuresRedeemerConverter complex_structuresRedeemerConverter = new Complex_structuresRedeemerConverter(); + ConstrPlutusData plutusData = complex_structuresRedeemerConverter.toPlutusData(complex_structuresRedeemer); + String s = plutusData.serializeToHex(); + assertEquals("d8799f4c48656c6c6f2c20576f726c64d8799f0a5348656c6c6f2c20576f726c642066726f6d2041d8799f145348656c6c6f2c20576f726c642066726f6d2042ffffff", plutusData.serializeToHex()); + } + + +} diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java new file mode 100644 index 00000000..93fcca1c --- /dev/null +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface BasicTypesBlueprint { +} diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java new file mode 100644 index 00000000..1efbfb89 --- /dev/null +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint") +public interface ComplexTypesBlueprint { +} diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java new file mode 100644 index 00000000..982d738a --- /dev/null +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface HelloWorldBlueprint { +} diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java new file mode 100644 index 00000000..81c0893d --- /dev/null +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface ListBlueprint { +} diff --git a/annotation-processor/src/test/resources/BasicTypesBlueprint.json b/annotation-processor/src/test/resources/BasicTypesBlueprint.json new file mode 100644 index 00000000..4bd0ea0d --- /dev/null +++ b/annotation-processor/src/test/resources/BasicTypesBlueprint.json @@ -0,0 +1,79 @@ +{ + "preamble": { + "title": "kammer/simplecontract", + "description": "Aiken contracts for project 'kammer/simplecontract'", + "version": "0.0.0", + "plutusVersion": "v2", + "compiler": { + "name": "Aiken", + "version": "v1.0.21-alpha+4b04517" + }, + "license": "Apache-2.0" + }, + "validators": [ + { + "title": "basic_types.basic_types", + "datum": { + "title": "datum", + "schema": { + "$ref": "#/definitions/basic_types~1Datum" + } + }, + "redeemer": { + "title": "redeemer", + "schema": { + "$ref": "#/definitions/basic_types~1Redeemer" + } + }, + "compiledCode": "58860100003232323232323232222323253330084a229309b2b19299980419b87480000044c8c8c8c94ccc03cc04400852616375a601e002601e0046e64dd7180680098030020b180300199299980399b87480000044c8c94ccc030c03800852616375c6018002600a0082c600a0064600a6ea80048c00cdd5000ab9a5573aaae7955cfaba157441", + "hash": "edd09ef476d3954fc6a3ac8e220c1f29c2e2dbaa03940c6dbd5ac62c" + } + ], + "definitions": { + "ByteArray": { + "dataType": "bytes" + }, + "Int": { + "dataType": "integer" + }, + "String": { + "dataType": "#string" + }, + "basic_types/Datum": { + "title": "Datum", + "anyOf": [ + { + "title": "Datum", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "owner", + "$ref": "#/definitions/ByteArray" + } + ] + } + ] + }, + "basic_types/Redeemer": { + "title": "Redeemer", + "anyOf": [ + { + "title": "Redeemer", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "msg", + "$ref": "#/definitions/String" + }, + { + "title": "counter", + "$ref": "#/definitions/Int" + } + ] + } + ] + } + } +} \ No newline at end of file diff --git a/annotation-processor/src/test/resources/ComplexTypeBlueprint.json b/annotation-processor/src/test/resources/ComplexTypeBlueprint.json new file mode 100644 index 00000000..0b88f01f --- /dev/null +++ b/annotation-processor/src/test/resources/ComplexTypeBlueprint.json @@ -0,0 +1,101 @@ +{ + "preamble": { + "title": "kammerlo/complex_structures", + "description": "Aiken contracts for project 'kammerlo/complex_structures'", + "version": "0.0.0", + "plutusVersion": "v2", + "compiler": { + "name": "Aiken", + "version": "v1.0.21-alpha+4b04517" + }, + "license": "Apache-2.0" + }, + "validators": [ + { + "title": "complex_structures.complex_structures", + "redeemer": { + "title": "datum", + "schema": { + "$ref": "#/definitions/complex_structures~1Datum" + } + }, + "compiledCode": "58c90100003232323232323232223253330064a229309b2b19299980319b87480000044c8c8c8c94ccc034c03c0084c92632533300b3370e90000008991919191919299980a180b00109924c64a66602466e1d20000011323232325333019301b002149858dcc9bae30190013019002375a602e00260200042c60200022c602800260280046e64dd7180900098090011bad3010001300900216300900116300d001300d002375c601600260080062c60080044600a6ea80048c00cdd5000ab9a5573aaae7955cfaba15745", + "hash": "1f9b29b74f7ad4866ccb142d6fbfab8931abb7e8113c163c47f49d86" + } + ], + "definitions": { + "ByteArray": { + "dataType": "bytes" + }, + "Int": { + "dataType": "integer" + }, + "String": { + "dataType": "#string" + }, + "complex_structures/Datum": { + "title": "Datum", + "anyOf": [ + { + "title": "Datum", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "owner", + "$ref": "#/definitions/ByteArray" + }, + { + "title": "nestedA", + "$ref": "#/definitions/complex_structures~1NestedA" + } + ] + } + ] + }, + "complex_structures/NestedA": { + "title": "NestedA", + "anyOf": [ + { + "title": "NestedA", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "count", + "$ref": "#/definitions/Int" + }, + { + "title": "msg", + "$ref": "#/definitions/String" + }, + { + "title": "nestedB", + "$ref": "#/definitions/complex_structures~1NestedB" + } + ] + } + ] + }, + "complex_structures/NestedB": { + "title": "NestedB", + "anyOf": [ + { + "title": "NestedB", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "count2", + "$ref": "#/definitions/Int" + }, + { + "title": "msg2", + "$ref": "#/definitions/String" + } + ] + } + ] + } + } +} \ No newline at end of file diff --git a/annotation-processor/src/test/resources/Hello_world-Blueprint.json b/annotation-processor/src/test/resources/Hello_world-Blueprint.json new file mode 100644 index 00000000..84e0b21a --- /dev/null +++ b/annotation-processor/src/test/resources/Hello_world-Blueprint.json @@ -0,0 +1,69 @@ +{ + "preamble": { + "title": "aiken_lang/hello_word", + "description": "Aiken contracts for project 'aiken_lang/hello_word'", + "version": "0.0.0", + "plutusVersion": "v2", + "compiler": { + "name": "Aiken", + "version": "v1.0.21-alpha+4b04517" + }, + "license": "Apache-2.0" + }, + "validators": [ + { + "title": "hello_world.hello_world", + "datum": { + "title": "datum", + "schema": { + "$ref": "#/definitions/hello_world~1Datum" + } + }, + "redeemer": { + "title": "redeemer", + "schema": { + "$ref": "#/definitions/hello_world~1Redeemer" + } + }, + "compiledCode": "58f2010000323232323232323222232325333008323232533300b002100114a06644646600200200644a66602200229404c8c94ccc040cdc78010028a511330040040013014002375c60240026eb0c038c03cc03cc03cc03cc03cc03cc03cc03cc020c008c020014dd71801180400399b8f375c6002600e00a91010d48656c6c6f2c20576f726c6421002300d00114984d958c94ccc020cdc3a400000226464a66601a601e0042930b1bae300d00130060041630060033253330073370e900000089919299980618070010a4c2c6eb8c030004c01401058c01400c8c014dd5000918019baa0015734aae7555cf2ab9f5742ae881", + "hash": "6fb13cf9efdbe986e784d1983b21d3fb90231c1745925f536a820fb4" + } + ], + "definitions": { + "ByteArray": { + "dataType": "bytes" + }, + "hello_world/Datum": { + "title": "Datum", + "anyOf": [ + { + "title": "Datum", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "owner", + "$ref": "#/definitions/ByteArray" + } + ] + } + ] + }, + "hello_world/Redeemer": { + "title": "Redeemer", + "anyOf": [ + { + "title": "Redeemer", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "msg", + "$ref": "#/definitions/ByteArray" + } + ] + } + ] + } + } +} \ No newline at end of file diff --git a/annotation-processor/src/test/resources/ListBlueprint.json b/annotation-processor/src/test/resources/ListBlueprint.json new file mode 100644 index 00000000..5bc1e3ba --- /dev/null +++ b/annotation-processor/src/test/resources/ListBlueprint.json @@ -0,0 +1,75 @@ +{ + "preamble": { + "title": "kammerlo/list_Str", + "description": "Aiken contracts for project 'kammerlo/list_Str'", + "version": "0.0.0", + "plutusVersion": "v2", + "compiler": { + "name": "Aiken", + "version": "v1.0.21-alpha+4b04517" + }, + "license": "Apache-2.0" + }, + "validators": [ + { + "title": "list_Str.list_Str", + "redeemer": { + "title": "datum", + "schema": { + "$ref": "#/definitions/list_Str~1Datum" + } + }, + "compiledCode": "58a70100003232323232323232223253330064a229309b2b1919299980399b87480000044c8c94ccc030c0380084c9263300400123233006001232533300d3370e9000000899192999809180a0010a4c2c6e64dd7180900098058010b18058009bac0011637586018002600a0082c600a00644646600200200644a66601800229309919801801980780118019806800918029baa001230033754002ae6955ceaab9e5573eae855d101", + "hash": "3e3223a680da18336236e86cef31068b5df3b96c2bb0cb2d765f8af5" + } + ], + "definitions": { + "List$List$list_Str/NestedA": { + "dataType": "list", + "items": { + "$ref": "#/definitions/List$list_Str~1NestedA" + } + }, + "List$list_Str/NestedA": { + "dataType": "list", + "items": { + "$ref": "#/definitions/list_Str~1NestedA" + } + }, + "String": { + "dataType": "#string" + }, + "list_Str/Datum": { + "title": "Datum", + "anyOf": [ + { + "title": "Datum", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "simpleList", + "$ref": "#/definitions/List$List$list_Str~1NestedA" + } + ] + } + ] + }, + "list_Str/NestedA": { + "title": "NestedA", + "anyOf": [ + { + "title": "NestedA", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "msg", + "$ref": "#/definitions/String" + } + ] + } + ] + } + } +} \ No newline at end of file diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java new file mode 100644 index 00000000..4e3b51f5 --- /dev/null +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java @@ -0,0 +1,18 @@ +package com.bloxbean.cardano.client.plutus.annotation; + +import java.io.File; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface Blueprint { + + String file() default ""; + String fileInRessources() default ""; + String packageName() default ""; + String prefix() default ""; +} + diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoader.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoader.java index 5f1cc6ca..c583a791 100644 --- a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoader.java +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoader.java @@ -1,13 +1,13 @@ package com.bloxbean.cardano.client.plutus.blueprint; import com.bloxbean.cardano.client.plutus.blueprint.exception.PlutusBlueprintException; -import com.bloxbean.cardano.client.plutus.blueprint.model.PlutusContractBlueprint; +import com.bloxbean.cardano.client.plutus.blueprint.model.*; import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; /** * Loads Plutus contract blueprint from plutus.json file @@ -33,12 +33,94 @@ public static PlutusContractBlueprint loadBlueprint(File file) { * @return PlutusContractBlueprint */ public static PlutusContractBlueprint loadBlueprint(InputStream is) { - try { ObjectMapper objectMapper = new ObjectMapper(); - PlutusContractBlueprint plutusContractBlueprint = objectMapper.readValue(is, PlutusContractBlueprint.class); - return plutusContractBlueprint; - } catch (IOException ex) { - throw new PlutusBlueprintException(ex); + + PlutusContractBlueprint plutusContractBlueprint = null; + try { + plutusContractBlueprint = objectMapper.readValue(is, PlutusContractBlueprint.class); + } catch (IOException e) { + throw new RuntimeException(e); + } + plutusContractBlueprint = resolveReferences(plutusContractBlueprint); + return plutusContractBlueprint; + } + + /** + * Resolves the references in the blueprint + * @param plutusContractBlueprint + * @return + */ + private static PlutusContractBlueprint resolveReferences(PlutusContractBlueprint plutusContractBlueprint) { + Map definitions = plutusContractBlueprint.getDefinitions(); + List validators = plutusContractBlueprint.getValidators(); + for (Validator validator : validators) { + if(validator.getDatum() != null) { + BlueprintSchema schema = validator.getDatum().getSchema(); + validator.getDatum().setSchema(resolveDatum(definitions, schema)); + } + if(validator.getRedeemer() != null) { + validator.getRedeemer().setSchema(resolveDatum(definitions, validator.getRedeemer().getSchema())); + } + if(validator.getParameters() != null) { + for(int i = 0; i < validator.getParameters().size(); i++) { + BlueprintSchema parameterSchema = validator.getParameters().get(i).getSchema(); + validator.getParameters().get(i).setSchema(resolveDatum(definitions, parameterSchema)); + } + } + + } + return plutusContractBlueprint; + } + + + /** + * Resolves the schema from the definitions map + * @param definitions + * @param schema + * @return + */ + private static BlueprintSchema resolveDatum(Map definitions, BlueprintSchema schema) { + BlueprintSchema blueprintSchema = schema; + if(schema.getRef() != null) { + String ref = getAndPrepare(schema); + + blueprintSchema.copyFrom(definitions.get(ref)); + } + blueprintSchema.setFields(extracted(definitions, blueprintSchema.getFields())); + blueprintSchema.setAnyOf(extracted(definitions, blueprintSchema.getAnyOf())); + if(blueprintSchema.getItems() != null) + blueprintSchema.setItems(extracted(definitions, List.of(blueprintSchema.getItems())).get(0)); + + return blueprintSchema; + } + + /** + * Extracts the schema from the definitions map + * @param definitions + * @param listInSchema + * @return + */ + private static List extracted(Map definitions, List listInSchema) { + List list = null; + if(listInSchema != null) { + list = new ArrayList<>(listInSchema); + for(int i = 0; i < listInSchema.size(); i++) { + BlueprintSchema field = listInSchema.get(i); + list.set(i, resolveDatum(definitions, field)); + } } + return list; + } + + /** + * Prepares the ref string to be used as key in the definitions map + * @param schema + * @return + */ + private static String getAndPrepare(BlueprintSchema schema) { + String ref = schema.getRef(); + ref = ref.replace("#/definitions/", ""); + ref = ref.replace("~1", "/"); + return ref; } } diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatatype.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatatype.java index 45b8b5d4..2c5e41ad 100644 --- a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatatype.java +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintDatatype.java @@ -1,9 +1,20 @@ package com.bloxbean.cardano.client.plutus.blueprint.model; +import com.fasterxml.jackson.annotation.JsonAlias; + public enum BlueprintDatatype { + @JsonAlias({"integer", "#integer"}) integer, + @JsonAlias({"bytes", "#bytes"}) bytes, + @JsonAlias({"list", "#list"}) list, map, - constructor + constructor, + @JsonAlias({"string", "#string"}) + string, + @JsonAlias({"boolean", "#boolean", "bool", "#bool"}) + bool, + @JsonAlias({"unit", "#unit"}) + pair, } diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintSchema.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintSchema.java index 345ede17..25cc6811 100644 --- a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintSchema.java +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/BlueprintSchema.java @@ -10,8 +10,10 @@ @Data @NoArgsConstructor -@JsonIgnoreProperties(ignoreUnknown = true) public class BlueprintSchema { + + @JsonAlias({"$ref", "ref"}) + private String ref; // for nested schemas private List anyOf; private List oneOf; @@ -39,7 +41,7 @@ public class BlueprintSchema { private int exclusiveMinimum; //Datatype = "list" - private List items; + private BlueprintSchema items; private int maxItems; private int minItems; private boolean uniqueItems; @@ -52,6 +54,32 @@ public class BlueprintSchema { // Datatype = "constructor" private int index; - private List> fields; + private List fields; + public void copyFrom(BlueprintSchema blueprintSchema) { + this.anyOf = this.anyOf != null ? this.anyOf : blueprintSchema.anyOf; + this.oneOf = this.oneOf != null ? this.oneOf : blueprintSchema.oneOf; + this.allOf = this.allOf != null ? this.allOf : blueprintSchema.allOf; + this.notOf = this.notOf != null ? this.notOf : blueprintSchema.notOf; + this.title = this.title != null ? this.title : blueprintSchema.title; + this.description = this.description != null ? this.description : blueprintSchema.description; + this.dataType = this.dataType != null ? this.dataType : blueprintSchema.dataType; + this.comment = this.comment != null ? this.comment : blueprintSchema.comment; + this.enumLiterals = this.enumLiterals != null ? this.enumLiterals : blueprintSchema.enumLiterals; + this.maxLength = this.maxLength != 0 ? this.maxLength : blueprintSchema.maxLength; + this.minLength = this.minLength != 0 ? this.minLength : blueprintSchema.minLength; + this.multipleOf = this.multipleOf != 0 ? this.multipleOf : blueprintSchema.multipleOf; + this.maximum = this.maximum != 0 ? this.maximum : blueprintSchema.maximum; + this.exclusiveMaximum = this.exclusiveMaximum != 0 ? this.exclusiveMaximum : blueprintSchema.exclusiveMaximum; + this.minimum = this.minimum != 0 ? this.minimum : blueprintSchema.minimum; + this.exclusiveMinimum = this.exclusiveMinimum != 0 ? this.exclusiveMinimum : blueprintSchema.exclusiveMinimum; + this.items = this.items != null ? this.items : blueprintSchema.items; + this.maxItems = this.maxItems != 0 ? this.maxItems : blueprintSchema.maxItems; + this.minItems = this.minItems != 0 ? this.minItems : blueprintSchema.minItems; + this.uniqueItems = this.uniqueItems || blueprintSchema.uniqueItems; + this.keys = this.keys != null ? this.keys : blueprintSchema.keys; + this.values = this.values != null ? this.values : blueprintSchema.values; + this.index = this.index != 0 ? this.index : blueprintSchema.index; + this.fields = this.fields != null ? this.fields : blueprintSchema.fields; + } } diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/PlutusContractBlueprint.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/PlutusContractBlueprint.java index 78b2c517..699b9cf3 100644 --- a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/PlutusContractBlueprint.java +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/PlutusContractBlueprint.java @@ -9,6 +9,7 @@ import lombok.NoArgsConstructor; import java.util.List; +import java.util.Map; /** * Initial implementation of Plutus Contract Blueprint (CIP-57) @@ -21,6 +22,7 @@ public class PlutusContractBlueprint { private Preamble preamble; private List validators; + private Map definitions; @JsonIgnore public PlutusScript getPlutusScript(String title) { diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/Validator.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/Validator.java index 01f01d0f..8431ff6a 100644 --- a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/Validator.java +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/model/Validator.java @@ -13,6 +13,7 @@ @JsonIgnoreProperties(ignoreUnknown = true) public class Validator { private String title; + private String description; private BlueprintDatum datum; private BlueprintDatum redeemer; private List parameters; diff --git a/plutus/src/test/resources/blueprint/list-blueprint.json b/plutus/src/test/resources/blueprint/list-blueprint.json new file mode 100644 index 00000000..9c973bfc --- /dev/null +++ b/plutus/src/test/resources/blueprint/list-blueprint.json @@ -0,0 +1,75 @@ +{ + "preamble": { + "title": "kammerlo/complex_structures", + "description": "Aiken contracts for project 'kammerlo/complex_structures'", + "version": "0.0.0", + "plutusVersion": "v2", + "compiler": { + "name": "Aiken", + "version": "v1.0.21-alpha+4b04517" + }, + "license": "Apache-2.0" + }, + "validators": [ + { + "title": "complex_structures.hello_world", + "redeemer": { + "title": "datum", + "schema": { + "$ref": "#/definitions/complex_structures~1Datum" + } + }, + "compiledCode": "58a70100003232323232323232223253330064a229309b2b1919299980399b87480000044c8c94ccc030c0380084c9263300400123233006001232533300d3370e9000000899192999809180a0010a4c2c6e64dd7180900098058010b18058009bac0011637586018002600a0082c600a00644646600200200644a66601800229309919801801980780118019806800918029baa001230033754002ae6955ceaab9e5573eae855d101", + "hash": "3e3223a680da18336236e86cef31068b5df3b96c2bb0cb2d765f8af5" + } + ], + "definitions": { + "List$List$complex_structures/NestedA": { + "dataType": "list", + "items": { + "$ref": "#/definitions/List$complex_structures~1NestedA" + } + }, + "List$complex_structures/NestedA": { + "dataType": "list", + "items": { + "$ref": "#/definitions/complex_structures~1NestedA" + } + }, + "String": { + "dataType": "#string" + }, + "complex_structures/Datum": { + "title": "Datum", + "anyOf": [ + { + "title": "Datum", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "simpleList", + "$ref": "#/definitions/List$List$complex_structures~1NestedA" + } + ] + } + ] + }, + "complex_structures/NestedA": { + "title": "NestedA", + "anyOf": [ + { + "title": "NestedA", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "msg", + "$ref": "#/definitions/String" + } + ] + } + ] + } + } +} \ No newline at end of file From 00e1d8ed5553909c91fb41b8a219f9298fe94e00 Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Wed, 31 Jan 2024 08:20:29 +0100 Subject: [PATCH 03/11] #369 add Mapdatatype --- .../DataTypeProcessUtil.java | 22 ++++-- .../annotation/blueprint/BluePrintTest.java | 45 +++++++++-- .../blueprint/model/MapBlueprint.java | 7 ++ .../src/test/resources/MapBlueprint.json | 75 +++++++++++++++++++ .../blueprint/PlutusBlueprintLoader.java | 5 +- 5 files changed, 143 insertions(+), 11 deletions(-) create mode 100644 annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java create mode 100644 annotation-processor/src/test/resources/MapBlueprint.json diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java index 020b978b..1d274bd9 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java @@ -67,16 +67,19 @@ public FieldSpec processListDataType(String javaDoc, BlueprintSchema schema) { if(schema.getDataType() != list) throw new IllegalArgumentException("Schema is not of type list"); - TypeName fieldClass = getListTypeName(schema); + TypeName fieldClass = getTypeNameForListParametrizedType(schema); return FieldSpec.builder(fieldClass, schema.getTitle()) .addModifiers(Modifier.PRIVATE) .addJavadoc(javaDoc) .build(); } - private TypeName getListTypeName(BlueprintSchema field) { - return ParameterizedTypeName.get(ClassName.get("java.util","List"), getInnerType(field.getItems())); + private TypeName getTypeNameForListParametrizedType(BlueprintSchema field) { + return ParameterizedTypeName.get(ClassName.get("java.util", "List"), getInnerType(field.getItems())); + } + private TypeName getTypeNameForMapParametrizedType(BlueprintSchema field) { + return ParameterizedTypeName.get(ClassName.get("java.util", "Map"), getInnerType(field.getKeys()), getInnerType(field.getValues())); } private TypeName getInnerType(BlueprintSchema items) { @@ -95,7 +98,9 @@ private TypeName getInnerType(BlueprintSchema items) { case bool: return TypeName.get(Boolean.class); case list: - return getListTypeName(items); + return getTypeNameForListParametrizedType(items); + case map: + return getTypeNameForMapParametrizedType(items); default: return TypeName.get(String.class); } @@ -171,6 +176,13 @@ private FieldSpec createEnumAndAddToFields(BlueprintSchema schema, String classN } public FieldSpec processMapDataType(String javaDoc, BlueprintSchema schema, String className) { - return null; + if(schema.getDataType() != map) + throw new IllegalArgumentException("Schema is not of type map"); + + TypeName fieldClass = getTypeNameForMapParametrizedType(schema); + return FieldSpec.builder(fieldClass, schema.getTitle()) + .addModifiers(Modifier.PRIVATE) + .addJavadoc(javaDoc) + .build(); } } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java index 674b68d6..3ad3373a 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java @@ -1,21 +1,28 @@ package com.bloxbean.cardano.client.plutus.annotation.blueprint; import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemerNestedA; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.*; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemerConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemerNestedANestedB; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Basic_typesRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Basic_typesRedeemerConverter; import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldDatum; import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldDatumConverter; import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldRedeemer; import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldRedeemerConverter; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Basic_typesRedeemer; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Basic_typesRedeemerConverter; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemer; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemerConverter; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemerNestedANestedB; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.List_StrRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.List_StrRedeemerConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.MapBPRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.MapBPRedeemerConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.NestedAConstr; import com.bloxbean.cardano.client.plutus.spec.ConstrPlutusData; import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.Map; import static org.junit.Assert.assertEquals; @@ -81,5 +88,33 @@ public void nestedTypeTest() { assertEquals("d8799f4c48656c6c6f2c20576f726c64d8799f0a5348656c6c6f2c20576f726c642066726f6d2041d8799f145348656c6c6f2c20576f726c642066726f6d2042ffffff", plutusData.serializeToHex()); } + @Test + public void listTest() { + List_StrRedeemer list_strRedeemer = new List_StrRedeemer(); + NestedAConstr nestedAConstr = new NestedAConstr(); + nestedAConstr.setMsg("Hello, World"); + list_strRedeemer.setSimpleList(List.of(List.of(nestedAConstr))); + + List_StrRedeemerConverter list_strRedeemerConverter = new List_StrRedeemerConverter(); + ConstrPlutusData plutusData = list_strRedeemerConverter.toPlutusData(list_strRedeemer); + + assertEquals("d8799f9f9fd8799f4c48656c6c6f2c20576f726c64ffffffff", plutusData.serializeToHex()); + } + + @Test + public void mapTest() { + MapBPRedeemer mapBPRedeemer = new MapBPRedeemer(); + Map map = new java.util.HashMap<>(); + NestedAConstr nestedAConstr = new NestedAConstr(); + nestedAConstr.setMsg("Hello, World"); + map.put(nestedAConstr, 10); + mapBPRedeemer.setMap(map); + + MapBPRedeemerConverter mapBPRedeemerConverter = new MapBPRedeemerConverter(); + ConstrPlutusData plutusData = mapBPRedeemerConverter.toPlutusData(mapBPRedeemer); + + assertEquals("d8799fa1d8799f4c48656c6c6f2c20576f726c64ff0aff", plutusData.serializeToHex()); + } + } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java new file mode 100644 index 00000000..96a11285 --- /dev/null +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface MapBlueprint { +} diff --git a/annotation-processor/src/test/resources/MapBlueprint.json b/annotation-processor/src/test/resources/MapBlueprint.json new file mode 100644 index 00000000..bebc90c8 --- /dev/null +++ b/annotation-processor/src/test/resources/MapBlueprint.json @@ -0,0 +1,75 @@ +{ + "preamble": { + "title": "kammerlo/mapBP", + "description": "Aiken contracts for project 'kammerlo/mapBP'", + "version": "0.0.0", + "plutusVersion": "v2", + "compiler": { + "name": "Aiken", + "version": "v1.0.21-alpha+4b04517" + }, + "license": "Apache-2.0" + }, + "validators": [ + { + "title": "mapBP.mapBP", + "redeemer": { + "title": "datum", + "schema": { + "$ref": "#/definitions/mapBP~1Datum" + } + }, + "compiledCode": "58a10100003232323232323232223253330064a229309b2b19299980319b87480000044c8c8c8c94ccc034c03c0084c926323300100100422533300f00114984c8cc00c00cc048008c8c94ccc038cdc3a400000226464a666026602a0042930b1b99375c602600260180042c601800260200022c6eb4c034004c034008dd6180580098020018b1802001118029baa001230033754002ae6955ceaab9e5573eae855d11", + "hash": "e62ab7064350e2980fa9a066208ff9576f1760e847513a0f737802ca" + } + ], + "definitions": { + "Int": { + "dataType": "integer" + }, + "List$mapBP/NestedA": { + "dataType": "map", + "keys": { + "$ref": "#/definitions/mapBP~1NestedA" + }, + "values": { + "$ref": "#/definitions/Int" + } + }, + "String": { + "dataType": "#string" + }, + "mapBP/Datum": { + "title": "Datum", + "anyOf": [ + { + "title": "Datum", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "map", + "$ref": "#/definitions/List$mapBP~1NestedA" + } + ] + } + ] + }, + "mapBP/NestedA": { + "title": "NestedA", + "anyOf": [ + { + "title": "NestedA", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "msg", + "$ref": "#/definitions/String" + } + ] + } + ] + } + } +} \ No newline at end of file diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoader.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoader.java index c583a791..82051334 100644 --- a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoader.java +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/blueprint/PlutusBlueprintLoader.java @@ -90,7 +90,10 @@ private static BlueprintSchema resolveDatum(Map definit blueprintSchema.setAnyOf(extracted(definitions, blueprintSchema.getAnyOf())); if(blueprintSchema.getItems() != null) blueprintSchema.setItems(extracted(definitions, List.of(blueprintSchema.getItems())).get(0)); - + if(blueprintSchema.getKeys() != null) + blueprintSchema.setKeys(extracted(definitions, List.of(blueprintSchema.getKeys())).get(0)); + if(blueprintSchema.getValues() != null) + blueprintSchema.setValues(extracted(definitions, List.of(blueprintSchema.getValues())).get(0)); return blueprintSchema; } From 0b45c2fae0caa0078736bf9e545e5f9ce5bb57b9 Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Thu, 1 Feb 2024 11:11:41 +0100 Subject: [PATCH 04/11] #369 added scriptaddress method and alternatives will be set in converter according to used type --- annotation-processor/build.gradle | 2 + .../DataTypeProcessUtil.java | 42 +++++++++--------- .../FieldSpecProcessor.java | 28 ++++++------ .../ValidatorProcessor.java | 23 +++++++++- .../processor/ClassDefinitionGenerator.java | 11 +++++ .../processor/ConverterCodeGenerator.java | 8 ++-- .../annotation/processor/model/Field.java | 1 + .../javax.annotation.processing.Processor | 4 +- .../annotation/blueprint/BluePrintTest.java | 44 ++++++++----------- .../model/ComplexTypesBlueprint.java | 2 +- .../test/resources/BasicTypesBlueprint.json | 21 +++++++++ .../client/plutus/annotation/Blueprint.java | 15 ++++++- 12 files changed, 133 insertions(+), 68 deletions(-) diff --git a/annotation-processor/build.gradle b/annotation-processor/build.gradle index bdbfd4a1..6e82e0f3 100644 --- a/annotation-processor/build.gradle +++ b/annotation-processor/build.gradle @@ -1,5 +1,7 @@ dependencies { api project(':plutus') + api project(':address') + implementation(libs.google.auto.service) { exclude group: 'com.google.guava', module: 'guava' } diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java index 1d274bd9..97b6bbc8 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java @@ -30,11 +30,11 @@ public DataTypeProcessUtil(FieldSpecProcessor fieldSpecProcessor, Blueprint anno * @param schema * @return */ - public FieldSpec processIntegerDataType(String javaDoc, BlueprintSchema schema) { + public FieldSpec processIntegerDataType(String javaDoc, BlueprintSchema schema, String alternativeName) { if(schema.getDataType() != integer) throw new IllegalArgumentException("Schema is not of type integer"); - - return FieldSpec.builder(int.class, schema.getTitle()) + String title = schema.getTitle() == null ? alternativeName : schema.getTitle(); + return FieldSpec.builder(int.class, title) .addModifiers(Modifier.PRIVATE) .addJavadoc(javaDoc) .build(); @@ -46,11 +46,11 @@ public FieldSpec processIntegerDataType(String javaDoc, BlueprintSchema schema) * @param schema * @return */ - public FieldSpec processBytesDataType(String javaDoc, BlueprintSchema schema) { + public FieldSpec processBytesDataType(String javaDoc, BlueprintSchema schema, String alternativeName) { if(schema.getDataType() != bytes) throw new IllegalArgumentException("Schema is not of type bytes"); - - return FieldSpec.builder(byte[].class, schema.getTitle()) + String title = schema.getTitle() == null ? alternativeName : schema.getTitle(); + return FieldSpec.builder(byte[].class, title) .addModifiers(Modifier.PRIVATE) .addJavadoc(javaDoc) .build(); @@ -63,12 +63,12 @@ public FieldSpec processBytesDataType(String javaDoc, BlueprintSchema schema) { * @param schema * @return */ - public FieldSpec processListDataType(String javaDoc, BlueprintSchema schema) { + public FieldSpec processListDataType(String javaDoc, BlueprintSchema schema, String alternativeName) { if(schema.getDataType() != list) throw new IllegalArgumentException("Schema is not of type list"); - + String title = schema.getTitle() == null ? alternativeName : schema.getTitle(); TypeName fieldClass = getTypeNameForListParametrizedType(schema); - return FieldSpec.builder(fieldClass, schema.getTitle()) + return FieldSpec.builder(fieldClass, title) .addModifiers(Modifier.PRIVATE) .addJavadoc(javaDoc) .build(); @@ -90,7 +90,7 @@ private TypeName getInnerType(BlueprintSchema items) { } switch (items.getDataType()) { case bytes: - return TypeName.get(Byte[].class); + return TypeName.get(byte[].class); case integer: return TypeName.get(Integer.class); case string: @@ -112,11 +112,11 @@ private TypeName getInnerType(BlueprintSchema items) { * @param schema * @return */ - public FieldSpec processBoolDataType(String javaDoc, BlueprintSchema schema) { + public FieldSpec processBoolDataType(String javaDoc, BlueprintSchema schema, String alternativeName) { if(schema.getDataType() != bool) throw new IllegalArgumentException("Schema is not of type boolean"); - - return FieldSpec.builder(boolean.class, schema.getTitle()) + String title = schema.getTitle() == null ? alternativeName : schema.getTitle(); + return FieldSpec.builder(boolean.class, title) .addModifiers(Modifier.PRIVATE) .addJavadoc(javaDoc) .build(); @@ -128,22 +128,22 @@ public FieldSpec processBoolDataType(String javaDoc, BlueprintSchema schema) { * @param schema * @return */ - public FieldSpec processStringDataType(String javaDoc, BlueprintSchema schema) { + public FieldSpec processStringDataType(String javaDoc, BlueprintSchema schema, String alternativeName) { if(schema.getDataType() != string) throw new IllegalArgumentException("Schema is not of type string"); - - return FieldSpec.builder(String.class, schema.getTitle()) + String title = schema.getTitle() == null ? alternativeName : schema.getTitle(); + return FieldSpec.builder(String.class, title) .addModifiers(Modifier.PRIVATE) .addJavadoc(javaDoc) .build(); } - public List processConstructorDataType(String javaDoc, BlueprintSchema schema, String className) { + public List processConstructorDataType(String javaDoc, BlueprintSchema schema, String className, String alternativeName) { List specs = new ArrayList<>(); for (BlueprintSchema field : schema.getFields()) { if(field.getDataType() != null) { javaDoc += " Index: " + field.getIndex() ; - specs.addAll(fieldSpecProcessor.CreateFieldSpecForDataTypes(javaDoc, List.of(field), className)); + specs.addAll(fieldSpecProcessor.CreateFieldSpecForDataTypes(javaDoc, List.of(field), className, alternativeName)); } else { specs.add(fieldSpecProcessor.createDatumFieldSpec(field, "", field.getTitle(), className)); } @@ -175,12 +175,12 @@ private FieldSpec createEnumAndAddToFields(BlueprintSchema schema, String classN .build(); } - public FieldSpec processMapDataType(String javaDoc, BlueprintSchema schema, String className) { + public FieldSpec processMapDataType(String javaDoc, BlueprintSchema schema, String className, String alternativeName) { if(schema.getDataType() != map) throw new IllegalArgumentException("Schema is not of type map"); - + String title = schema.getTitle() == null ? alternativeName : schema.getTitle(); TypeName fieldClass = getTypeNameForMapParametrizedType(schema); - return FieldSpec.builder(fieldClass, schema.getTitle()) + return FieldSpec.builder(fieldClass, title) .addModifiers(Modifier.PRIVATE) .addJavadoc(javaDoc) .build(); diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java index 505d1fb4..162b5259 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java @@ -58,29 +58,29 @@ public static Tuple> collectAllFields(BlueprintSch * @param className * @return */ - public List CreateFieldSpecForDataTypes(String javaDoc, BlueprintSchema schema, String className) { + public List CreateFieldSpecForDataTypes(String javaDoc, BlueprintSchema schema, String className, String alternativeName) { List specs = new ArrayList<>(); switch (schema.getDataType()) { case bytes: - specs.add(dataTypeProcessUtil.processBytesDataType(javaDoc, schema)); + specs.add(dataTypeProcessUtil.processBytesDataType(javaDoc, schema, alternativeName)); break; case integer: - specs.add(dataTypeProcessUtil.processIntegerDataType(javaDoc, schema)); + specs.add(dataTypeProcessUtil.processIntegerDataType(javaDoc, schema, alternativeName)); break; case bool: - specs.add(dataTypeProcessUtil.processBoolDataType(javaDoc, schema)); + specs.add(dataTypeProcessUtil.processBoolDataType(javaDoc, schema, alternativeName)); break; case list: - specs.add(dataTypeProcessUtil.processListDataType(javaDoc, schema)); + specs.add(dataTypeProcessUtil.processListDataType(javaDoc, schema, alternativeName)); break; case map: - specs.add(dataTypeProcessUtil.processMapDataType(javaDoc, schema, className)); + specs.add(dataTypeProcessUtil.processMapDataType(javaDoc, schema, className, alternativeName)); break; case constructor: - specs.addAll(dataTypeProcessUtil.processConstructorDataType(javaDoc, schema, className)); + specs.addAll(dataTypeProcessUtil.processConstructorDataType(javaDoc, schema, className, alternativeName)); break; case string: - specs.add(dataTypeProcessUtil.processStringDataType(javaDoc, schema)); + specs.add(dataTypeProcessUtil.processStringDataType(javaDoc, schema, alternativeName)); break; default: } @@ -94,10 +94,10 @@ public List CreateFieldSpecForDataTypes(String javaDoc, BlueprintSche * @param className * @return */ - public List CreateFieldSpecForDataTypes(String javaDoc, List schemas, String className) { + public List CreateFieldSpecForDataTypes(String javaDoc, List schemas, String className, String alternativeName) { List specs = new ArrayList<>(); for (BlueprintSchema schema : schemas) { - specs.addAll(CreateFieldSpecForDataTypes(javaDoc, schema, className)); + specs.addAll(CreateFieldSpecForDataTypes(javaDoc, schema, className, alternativeName)); } return specs; } @@ -112,7 +112,7 @@ public List CreateFieldSpecForDataTypes(String javaDoc, List> allInnerSchemas = FieldSpecProcessor.collectAllFields(schema); - - List fields = CreateFieldSpecForDataTypes(allInnerSchemas._1, allInnerSchemas._2, className); + String title = schema.getTitle() == null ? alternativeName : schema.getTitle(); + List fields = CreateFieldSpecForDataTypes(allInnerSchemas._1, allInnerSchemas._2, className, title); AnnotationSpec constrAnnotationBuilder = AnnotationSpec.builder(Constr.class).addMember("alternative", "$L", schema.getIndex()).build(); TypeSpec.Builder classBuilder = TypeSpec.classBuilder(className) diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/ValidatorProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/ValidatorProcessor.java index 292fdf90..16648f18 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/ValidatorProcessor.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/ValidatorProcessor.java @@ -1,8 +1,14 @@ package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor; +import co.nstant.in.cbor.model.ByteString; +import com.bloxbean.cardano.client.address.AddressProvider; +import com.bloxbean.cardano.client.common.model.Network; +import com.bloxbean.cardano.client.exception.CborDeserializationException; import com.bloxbean.cardano.client.plutus.annotation.Blueprint; import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintDatum; import com.bloxbean.cardano.client.plutus.blueprint.model.Validator; +import com.bloxbean.cardano.client.plutus.spec.PlutusV2Script; +import com.bloxbean.cardano.client.util.HexUtil; import com.squareup.javapoet.*; import lombok.AllArgsConstructor; import lombok.Data; @@ -45,9 +51,11 @@ public void processValidator(Validator validator) { fields.add(fieldSpecProcessor.createDatumFieldSpec(validator.getDatum().getSchema(), "Datum", title, "")); if(validator.getParameters() != null) { for (BlueprintDatum parameter : validator.getParameters()) { - fields.add(fieldSpecProcessor.createDatumFieldSpec(parameter.getSchema(), "Parameter", title, "")); + fields.add(fieldSpecProcessor.createDatumFieldSpec(parameter.getSchema(), "Parameter", title + parameter.getTitle(), "")); } } + List methods = new ArrayList<>(); + methods.add(getScriptAddressMethodSpec()); // building and saving of class TypeSpec build = TypeSpec.classBuilder(validatorClassName) @@ -57,10 +65,23 @@ public void processValidator(Validator validator) { .addAnnotation(AllArgsConstructor.class) .addAnnotation(NoArgsConstructor.class) .addFields(fields) + .addMethods(methods) .build(); JavaFileUtil.createJavaFile(packageName, build, validatorClassName, processingEnv); } + private MethodSpec getScriptAddressMethodSpec() { + MethodSpec getScriptAddress = MethodSpec.methodBuilder("getScriptAddress") + .addModifiers(Modifier.PUBLIC) + .returns(String.class) + .addParameter(Network.class, "network") + .addException(CborDeserializationException.class) + .addStatement("$T compiledCodeAsByteString = new $T($T.decodeHexString(this.compiledCode))", ByteString.class, ByteString.class, HexUtil.class) + .addStatement("$T script = $T.deserialize(compiledCodeAsByteString)", PlutusV2Script.class, PlutusV2Script.class) + .addStatement("return $T.getEntAddress(script, network).toBech32()", AddressProvider.class) + .build(); + return getScriptAddress; + } public static List getFieldSpecsForValidator(Validator validator) { diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ClassDefinitionGenerator.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ClassDefinitionGenerator.java index d37d856c..4924d03f 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ClassDefinitionGenerator.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ClassDefinitionGenerator.java @@ -75,6 +75,7 @@ public ClassDefinition getClassDefinition(TypeElement typeElement) { VariableElement variableElement = (VariableElement) enclosedElement; String fieldName = variableElement.getSimpleName().toString(); field.setName(fieldName); + field.setAlternative(getAlternative(fieldName)); ExecutableElement getter = findGetter(typeElement, variableElement); ExecutableElement setter = findSetter(typeElement, variableElement); @@ -123,6 +124,16 @@ public ClassDefinition getClassDefinition(TypeElement typeElement) { return classDefinition; } + private int getAlternative(String fieldName) { + Optional first = typeElements.stream().filter(typeElement -> typeElement.getSimpleName().toString().toLowerCase().equals(fieldName.toLowerCase())).findFirst(); + if(first.isPresent()) { + TypeElement typeElement = first.get(); + return typeElement.getAnnotation(Constr.class).alternative(); + } else { + return 0; + } + } + private FieldType detectFieldType(TypeName typeName, TypeMirror typeMirror) throws NotSupportedException { FieldType fieldType = new FieldType(); fieldType.setFqTypeName(typeName.toString()); diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConverterCodeGenerator.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConverterCodeGenerator.java index b51329b6..3bdb7acf 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConverterCodeGenerator.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConverterCodeGenerator.java @@ -196,9 +196,11 @@ private MethodSpec generateToPlutusDataMethod(ClassDefinition classDef) { case CONSTRUCTOR: codeBlock = CodeBlock.builder() .add("//Field $L\n", field.getName()) - .add(nullCheckStatement(field, fieldOrGetterName(field))) - .addStatement("constr.getData().add(new $LConverter().toPlutusData(obj.$L))", field.getFieldType().getJavaType().getName(), fieldOrGetterName(field)) - .add("\n") + .beginControlFlow("if(obj.$L != null)", fieldOrGetterName(field)) + .addStatement("constr.getData().add(new $LConverter().toPlutusData(obj.$L))", field.getFieldType().getJavaType().getName(), fieldOrGetterName(field)) + .addStatement("// Setting the alternative to the childs alternative to use this constructor") + .addStatement("constr = $T.builder().alternative($L).data(constr.getData()).build()", ConstrPlutusData.class, field.getAlternative()) + .endControlFlow() .build(); break; case BOOL: diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/Field.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/Field.java index ae3dcfd8..7f1e1d10 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/Field.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/Field.java @@ -15,4 +15,5 @@ public class Field { private FieldType fieldType; private boolean hashGetter; private String getterName; + private int alternative; } diff --git a/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor index f6e2a4a2..f9558d0f 100644 --- a/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor +++ b/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -1,2 +1,2 @@ -com.bloxbean.cardano.client.plutus.annotation.processor.ConstrAnnotationProcessor -com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.BlueprintAnnotationProcessor \ No newline at end of file +com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.BlueprintAnnotationProcessor +com.bloxbean.cardano.client.plutus.annotation.processor.ConstrAnnotationProcessor \ No newline at end of file diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java index 3ad3373a..e296c571 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java @@ -1,43 +1,22 @@ package com.bloxbean.cardano.client.plutus.annotation.blueprint; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemerNestedA; import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.*; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemer; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemerConverter; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.Complex_structuresRedeemerNestedANestedB; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Basic_typesRedeemer; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Basic_typesRedeemerConverter; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldDatum; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldDatumConverter; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldRedeemer; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.Hello_worldRedeemerConverter; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.List_StrRedeemer; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.List_StrRedeemerConverter; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.MapBPRedeemer; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.MapBPRedeemerConverter; -import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.NestedAConstr; import com.bloxbean.cardano.client.plutus.spec.ConstrPlutusData; import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; + import static org.junit.Assert.assertEquals; public class BluePrintTest { @Test - public void test() throws ClassNotFoundException { -// Para - - System.out.println(""); - } - - - @Test - public void fillDatum() { + public void bytesTest() { Hello_worldDatum hello_worldDatum = new Hello_worldDatum(); hello_worldDatum.setOwner(new byte[]{-88, -77, -72, 20, -90, 94, 94, 67, 119, 127, -119, -77, 12, -121, -22, -60, -29, 57, 106, -101, -100, 36, -51, 8, -42, 96, -108, -103}); Hello_worldDatumConverter hello_worldDatumConverter = new Hello_worldDatumConverter(); @@ -46,10 +25,25 @@ public void fillDatum() { assertEquals("d8799f581ca8b3b814a65e5e43777f89b30c87eac4e3396a9b9c24cd08d6609499ff", plutusData.serializeToHex()); } + @Test + public void enumTest() { + Basic_typesDatum basic_typesDatum = new Basic_typesDatum(); + Basic_typesDatumAction basic_typesDatumAction = new Basic_typesDatumAction(); + basic_typesDatumAction.setBasic_typesDatumAction1Burn(new Basic_typesDatumAction1Burn()); + basic_typesDatum.setAction(basic_typesDatumAction); + basic_typesDatum.setOwner("Hello, World".getBytes(StandardCharsets.UTF_8)); + + Basic_typesDatumConverter basic_typesDatumConverter = new Basic_typesDatumConverter(); + ConstrPlutusData plutusData = basic_typesDatumConverter.toPlutusData(basic_typesDatum); + + assertEquals("d8799f4c48656c6c6f2c20576f726c64d87a9fd87a80ffff", plutusData.serializeToHex()); + } + @Test public void fillRedeemer() { Hello_worldRedeemer hello_worldRedeemer = new Hello_worldRedeemer(); hello_worldRedeemer.setMsg("Hello, World".getBytes(StandardCharsets.UTF_8)); + Hello_worldRedeemerConverter hello_worldRedeemerConverter = new Hello_worldRedeemerConverter(); ConstrPlutusData plutusData = hello_worldRedeemerConverter.toPlutusData(hello_worldRedeemer); @@ -104,7 +98,7 @@ public void listTest() { @Test public void mapTest() { MapBPRedeemer mapBPRedeemer = new MapBPRedeemer(); - Map map = new java.util.HashMap<>(); + Map map = new HashMap<>(); NestedAConstr nestedAConstr = new NestedAConstr(); nestedAConstr.setMsg("Hello, World"); map.put(nestedAConstr, 10); diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java index 1efbfb89..d7756307 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint") +@Blueprint(fileInRessources = "ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface ComplexTypesBlueprint { } diff --git a/annotation-processor/src/test/resources/BasicTypesBlueprint.json b/annotation-processor/src/test/resources/BasicTypesBlueprint.json index 4bd0ea0d..f26dcc49 100644 --- a/annotation-processor/src/test/resources/BasicTypesBlueprint.json +++ b/annotation-processor/src/test/resources/BasicTypesBlueprint.json @@ -50,6 +50,10 @@ { "title": "owner", "$ref": "#/definitions/ByteArray" + }, + { + "title": "action", + "$ref": "#/definitions/basic_types~1Enum" } ] } @@ -74,6 +78,23 @@ ] } ] + }, + "basic_types/Enum": { + "title": "Enum", + "anyOf": [ + { + "title": "Mint", + "dataType": "constructor", + "index": 0, + "fields": [] + }, + { + "title": "Burn", + "dataType": "constructor", + "index": 1, + "fields": [] + } + ] } } } \ No newline at end of file diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java index 4e3b51f5..0f00deda 100644 --- a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java @@ -10,9 +10,22 @@ @Target(ElementType.TYPE) public @interface Blueprint { + /** + * Absolute file path + * @return + */ String file() default ""; + + /** + * File in ressources + * @return + */ String fileInRessources() default ""; + + /** + * Name of package the generated classes should be in + * @return + */ String packageName() default ""; - String prefix() default ""; } From 3e46b2bfc2818f805cb45568b96c63efe1121a65 Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Thu, 1 Feb 2024 15:04:14 +0100 Subject: [PATCH 05/11] #369 fixed test deployment --- .../BlueprintAnnotationProcessor.java | 6 +++ .../blueprint/BlueprintCompileTest.java | 42 +++++++++++++++++++ .../blueprint/model/BasicTypesBlueprint.java | 2 +- .../model/ComplexTypesBlueprint.java | 2 +- .../blueprint/model/HelloWorldBlueprint.java | 2 +- .../blueprint/model/ListBlueprint.java | 2 +- .../blueprint/model/MapBlueprint.java | 2 +- .../blueprint/BasicTypesBlueprint.java | 7 ++++ .../{ => blueprint}/BasicTypesBlueprint.json | 0 .../{ => blueprint}/ComplexTypeBlueprint.json | 0 .../blueprint/ComplexTypesBlueprint.java | 7 ++++ .../blueprint/HelloWorldBlueprint.java | 7 ++++ .../Hello_world-Blueprint.json | 0 .../resources/blueprint/ListBlueprint.java | 7 ++++ .../{ => blueprint}/ListBlueprint.json | 0 .../resources/blueprint/MapBlueprint.java | 7 ++++ .../{ => blueprint}/MapBlueprint.json | 0 17 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java create mode 100644 annotation-processor/src/test/resources/blueprint/BasicTypesBlueprint.java rename annotation-processor/src/test/resources/{ => blueprint}/BasicTypesBlueprint.json (100%) rename annotation-processor/src/test/resources/{ => blueprint}/ComplexTypeBlueprint.json (100%) create mode 100644 annotation-processor/src/test/resources/blueprint/ComplexTypesBlueprint.java create mode 100644 annotation-processor/src/test/resources/blueprint/HelloWorldBlueprint.java rename annotation-processor/src/test/resources/{ => blueprint}/Hello_world-Blueprint.json (100%) create mode 100644 annotation-processor/src/test/resources/blueprint/ListBlueprint.java rename annotation-processor/src/test/resources/{ => blueprint}/ListBlueprint.json (100%) create mode 100644 annotation-processor/src/test/resources/blueprint/MapBlueprint.java rename annotation-processor/src/test/resources/{ => blueprint}/MapBlueprint.json (100%) diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java index 8986bd3d..99899e25 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java @@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j; import javax.annotation.processing.*; +import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic; @@ -35,6 +36,11 @@ public Set getSupportedAnnotationTypes() { return annotataions; } + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latestSupported(); + } + @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { System.out.println("Processing Blueprint annotation"); diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java new file mode 100644 index 00000000..384ed5ac --- /dev/null +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java @@ -0,0 +1,42 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint; + +import com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.BlueprintAnnotationProcessor; +import com.google.testing.compile.Compilation; +import com.google.testing.compile.JavaFileObjects; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static com.google.testing.compile.CompilationSubject.assertThat; +import static com.google.testing.compile.Compiler.javac; + +public class BlueprintCompileTest { + + @Test + void nestedListMapCompile() throws Exception { + Compilation compilation = + javac() + .withProcessors(new BlueprintAnnotationProcessor()) + .compile( + JavaFileObjects.forResource("blueprint/BasicTypesBlueprint.java"), + JavaFileObjects.forResource("blueprint/ComplexTypesBlueprint.java"), + JavaFileObjects.forResource("blueprint/HelloWorldBlueprint.java"), + JavaFileObjects.forResource("blueprint/ListBlueprint.java"), + JavaFileObjects.forResource("blueprint/MapBlueprint.java") + ); + + System.out.println(compilation.diagnostics()); + compilation.generatedFiles().forEach(javaFileObject -> { + if (javaFileObject.getName().endsWith("class")) + return;; + System.out.println(javaFileObject.getName()); + try { + System.out.println(javaFileObject.getCharContent(true)); + } catch (IOException e) { + throw new RuntimeException(e); + } + + }); + assertThat(compilation).succeeded(); + } +} diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java index 93fcca1c..c8ee7e8d 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInRessources = "blueprint/BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface BasicTypesBlueprint { } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java index d7756307..3f138eeb 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInRessources = "blueprint/ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface ComplexTypesBlueprint { } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java index 982d738a..f4f5d859 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInRessources = "blueprint/Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface HelloWorldBlueprint { } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java index 81c0893d..ee59454c 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInRessources = "blueprint/ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface ListBlueprint { } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java index 96a11285..4326a013 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInRessources = "blueprint/MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface MapBlueprint { } diff --git a/annotation-processor/src/test/resources/blueprint/BasicTypesBlueprint.java b/annotation-processor/src/test/resources/blueprint/BasicTypesBlueprint.java new file mode 100644 index 00000000..c8ee7e8d --- /dev/null +++ b/annotation-processor/src/test/resources/blueprint/BasicTypesBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface BasicTypesBlueprint { +} diff --git a/annotation-processor/src/test/resources/BasicTypesBlueprint.json b/annotation-processor/src/test/resources/blueprint/BasicTypesBlueprint.json similarity index 100% rename from annotation-processor/src/test/resources/BasicTypesBlueprint.json rename to annotation-processor/src/test/resources/blueprint/BasicTypesBlueprint.json diff --git a/annotation-processor/src/test/resources/ComplexTypeBlueprint.json b/annotation-processor/src/test/resources/blueprint/ComplexTypeBlueprint.json similarity index 100% rename from annotation-processor/src/test/resources/ComplexTypeBlueprint.json rename to annotation-processor/src/test/resources/blueprint/ComplexTypeBlueprint.json diff --git a/annotation-processor/src/test/resources/blueprint/ComplexTypesBlueprint.java b/annotation-processor/src/test/resources/blueprint/ComplexTypesBlueprint.java new file mode 100644 index 00000000..3f138eeb --- /dev/null +++ b/annotation-processor/src/test/resources/blueprint/ComplexTypesBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface ComplexTypesBlueprint { +} diff --git a/annotation-processor/src/test/resources/blueprint/HelloWorldBlueprint.java b/annotation-processor/src/test/resources/blueprint/HelloWorldBlueprint.java new file mode 100644 index 00000000..f4f5d859 --- /dev/null +++ b/annotation-processor/src/test/resources/blueprint/HelloWorldBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface HelloWorldBlueprint { +} diff --git a/annotation-processor/src/test/resources/Hello_world-Blueprint.json b/annotation-processor/src/test/resources/blueprint/Hello_world-Blueprint.json similarity index 100% rename from annotation-processor/src/test/resources/Hello_world-Blueprint.json rename to annotation-processor/src/test/resources/blueprint/Hello_world-Blueprint.json diff --git a/annotation-processor/src/test/resources/blueprint/ListBlueprint.java b/annotation-processor/src/test/resources/blueprint/ListBlueprint.java new file mode 100644 index 00000000..ee59454c --- /dev/null +++ b/annotation-processor/src/test/resources/blueprint/ListBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface ListBlueprint { +} diff --git a/annotation-processor/src/test/resources/ListBlueprint.json b/annotation-processor/src/test/resources/blueprint/ListBlueprint.json similarity index 100% rename from annotation-processor/src/test/resources/ListBlueprint.json rename to annotation-processor/src/test/resources/blueprint/ListBlueprint.json diff --git a/annotation-processor/src/test/resources/blueprint/MapBlueprint.java b/annotation-processor/src/test/resources/blueprint/MapBlueprint.java new file mode 100644 index 00000000..4326a013 --- /dev/null +++ b/annotation-processor/src/test/resources/blueprint/MapBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface MapBlueprint { +} diff --git a/annotation-processor/src/test/resources/MapBlueprint.json b/annotation-processor/src/test/resources/blueprint/MapBlueprint.json similarity index 100% rename from annotation-processor/src/test/resources/MapBlueprint.json rename to annotation-processor/src/test/resources/blueprint/MapBlueprint.json From 7fcf249eb551a289d3b4ff5d0d4c0ad94b5e24e1 Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Fri, 2 Feb 2024 11:09:43 +0100 Subject: [PATCH 06/11] #369 had to add resources to sourceSets to enable the filer to find the files used in the tests --- annotation-processor/build.gradle | 15 ++++- .../BlueprintAnnotationProcessor.java | 58 +++++++++++++------ .../FieldSpecProcessor.java | 11 ++++ .../blueprint_processor/JavaFileUtil.java | 7 --- .../ValidatorProcessor.java | 13 +++-- .../model/BasicTypesBlueprint.java | 7 +++ .../model/ComplexTypesBlueprint.java | 7 +++ .../model/HelloWorldBlueprint.java | 7 +++ .../model/ListBlueprint.java | 7 +++ .../model/MapBlueprint.java | 7 +++ .../blueprint/BlueprintCompileTest.java | 4 +- 11 files changed, 107 insertions(+), 36 deletions(-) create mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java create mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java create mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java create mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java create mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java diff --git a/annotation-processor/build.gradle b/annotation-processor/build.gradle index 6e82e0f3..59763991 100644 --- a/annotation-processor/build.gradle +++ b/annotation-processor/build.gradle @@ -2,9 +2,7 @@ dependencies { api project(':plutus') api project(':address') - implementation(libs.google.auto.service) { - exclude group: 'com.google.guava', module: 'guava' - } + implementation(libs.google.auto.service) api libs.javapoet testImplementation libs.google.testing.compile @@ -13,6 +11,17 @@ dependencies { testAnnotationProcessor project(':annotation-processor') } +sourceSets { + json { + resources { + srcDir('src/test/resources/') + } + } + test { + compileClasspath += sourceSets.json.output + } +} + publishing { publications { mavenJava(MavenPublication) { diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java index 99899e25..85b76bd1 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java @@ -11,6 +11,8 @@ import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic; +import javax.tools.FileObject; +import javax.tools.StandardLocation; import java.io.File; import java.util.*; @@ -20,7 +22,6 @@ public class BlueprintAnnotationProcessor extends AbstractProcessor { private Messager messager; private List typeElements = new ArrayList<>(); - private Blueprint annotation; private ValidatorProcessor validatorProcessor; @Override @@ -43,35 +44,30 @@ public SourceVersion getSupportedSourceVersion() { @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { - System.out.println("Processing Blueprint annotation"); - for (TypeElement annotation : annotations) { - Set elements = roundEnv.getElementsAnnotatedWith(annotation); - for (Element element : elements) { - if (element instanceof TypeElement) { - TypeElement typeElement = (TypeElement) element; - typeElements.add(typeElement); + log.debug("Processing Blueprint annotation"); - } - } - } + typeElements = getTypeElementsWithAnnotations(annotations, roundEnv); for(TypeElement typeElement : typeElements) { - annotation = typeElement.getAnnotation(Blueprint.class); + Blueprint annotation = typeElement.getAnnotation(Blueprint.class); if (annotation == null) { log.error("Blueprint annotation not found for class {}", typeElement.getSimpleName()); return false; } else { validatorProcessor = new ValidatorProcessor(annotation, processingEnv); } - - File blueprintFile = getFile(); - + File blueprintFile = getFileFromAnnotation(annotation); if (blueprintFile == null || !blueprintFile.exists()) { log.error("Blueprint file {} not found", annotation.fileInRessources()); return false; } - PlutusContractBlueprint plutusContractBlueprint = PlutusBlueprintLoader.loadBlueprint(blueprintFile); - + PlutusContractBlueprint plutusContractBlueprint; + try { + plutusContractBlueprint = PlutusBlueprintLoader.loadBlueprint(blueprintFile); + } catch (Exception e) { + log.error("Error processing blueprint file {}", blueprintFile.getAbsolutePath(), e); + return false; + } for (Validator validator : plutusContractBlueprint.getValidators()) { validatorProcessor.processValidator(validator); } @@ -79,12 +75,27 @@ public boolean process(Set annotations, RoundEnvironment return true; } - private File getFile() { + private List getTypeElementsWithAnnotations(Set annotations, RoundEnvironment roundEnv) { + List elementsList = new ArrayList<>(); + for (TypeElement annotation : annotations) { + Set elements = roundEnv.getElementsAnnotatedWith(annotation); + for (Element element : elements) { + if (element instanceof TypeElement) { + TypeElement typeElement = (TypeElement) element; + elementsList.add(typeElement); + + } + } + } + return elementsList; + } + + private File getFileFromAnnotation(Blueprint annotation) { File blueprintFile = null; if(!annotation.file().isEmpty()) blueprintFile = new File(annotation.file()); if(!annotation.fileInRessources().isEmpty()) - blueprintFile = JavaFileUtil.getFileFromRessourcers(annotation.fileInRessources()); + blueprintFile = getFileFromRessourcers(annotation.fileInRessources()); if(blueprintFile == null || !blueprintFile.exists()) { log.error("Blueprint file {} not found", annotation.file()); return null; @@ -92,6 +103,15 @@ private File getFile() { return blueprintFile; } + public File getFileFromRessourcers(String s) { + try { + FileObject resource = processingEnv.getFiler().getResource(StandardLocation.CLASS_PATH, "", s); + return new File(resource.toUri()); + } catch (Exception e) { + return null; + } + } + private void error(Element e, String msg, Object... args) { messager.printMessage( diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java index 162b5259..83d6b7e8 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java @@ -102,6 +102,17 @@ public List CreateFieldSpecForDataTypes(String javaDoc, List fields = ValidatorProcessor.getFieldSpecsForValidator(validator); // processing of fields if(validator.getRedeemer() != null) - fields.add(fieldSpecProcessor.createDatumFieldSpec(validator.getRedeemer().getSchema(), "Redeemer", title, "")); + fields.add(fieldSpecProcessor.createDatumFieldSpec(validator.getRedeemer().getSchema(), "Redeemer", title)); if(validator.getDatum() != null) - fields.add(fieldSpecProcessor.createDatumFieldSpec(validator.getDatum().getSchema(), "Datum", title, "")); + fields.add(fieldSpecProcessor.createDatumFieldSpec(validator.getDatum().getSchema(), "Datum", title)); if(validator.getParameters() != null) { for (BlueprintDatum parameter : validator.getParameters()) { - fields.add(fieldSpecProcessor.createDatumFieldSpec(parameter.getSchema(), "Parameter", title + parameter.getTitle(), "")); + fields.add(fieldSpecProcessor.createDatumFieldSpec(parameter.getSchema(), "Parameter", title + parameter.getTitle())); } } List methods = new ArrayList<>(); methods.add(getScriptAddressMethodSpec()); + String validatorClassName = title + VALIDATOR_CLASS_SUFFIX; // building and saving of class TypeSpec build = TypeSpec.classBuilder(validatorClassName) .addModifiers(Modifier.PUBLIC) @@ -76,6 +78,7 @@ private MethodSpec getScriptAddressMethodSpec() { .returns(String.class) .addParameter(Network.class, "network") .addException(CborDeserializationException.class) + .addJavadoc("Returns the address of the validator script") .addStatement("$T compiledCodeAsByteString = new $T($T.decodeHexString(this.compiledCode))", ByteString.class, ByteString.class, HexUtil.class) .addStatement("$T script = $T.deserialize(compiledCodeAsByteString)", PlutusV2Script.class, PlutusV2Script.class) .addStatement("return $T.getEntAddress(script, network).toBech32()", AddressProvider.class) diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java new file mode 100644 index 00000000..cba678af --- /dev/null +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface BasicTypesBlueprint { +} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java new file mode 100644 index 00000000..a4ef4487 --- /dev/null +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface ComplexTypesBlueprint { +} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java new file mode 100644 index 00000000..e80a337d --- /dev/null +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface HelloWorldBlueprint { +} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java new file mode 100644 index 00000000..dcd33870 --- /dev/null +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface ListBlueprint { +} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java new file mode 100644 index 00000000..f76d3cbc --- /dev/null +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface MapBlueprint { +} diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java index 384ed5ac..1600e08c 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java @@ -13,7 +13,7 @@ public class BlueprintCompileTest { @Test - void nestedListMapCompile() throws Exception { + void nestedListMapCompile() { Compilation compilation = javac() .withProcessors(new BlueprintAnnotationProcessor()) @@ -28,7 +28,7 @@ void nestedListMapCompile() throws Exception { System.out.println(compilation.diagnostics()); compilation.generatedFiles().forEach(javaFileObject -> { if (javaFileObject.getName().endsWith("class")) - return;; + return; System.out.println(javaFileObject.getName()); try { System.out.println(javaFileObject.getCharContent(true)); From a718a2d668163cdf6ffd64c49a7184820ca8945d Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Fri, 2 Feb 2024 11:13:17 +0100 Subject: [PATCH 07/11] #369 still excluding guava --- annotation-processor/build.gradle | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/annotation-processor/build.gradle b/annotation-processor/build.gradle index 59763991..2c20c54c 100644 --- a/annotation-processor/build.gradle +++ b/annotation-processor/build.gradle @@ -2,7 +2,9 @@ dependencies { api project(':plutus') api project(':address') - implementation(libs.google.auto.service) + implementation(libs.google.auto.service){ + exclude group: 'com.google.guava', module: 'guava' + } api libs.javapoet testImplementation libs.google.testing.compile From 54b04fe42e4cabb1476d99792f850387b7c8c6f0 Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Mon, 12 Feb 2024 08:25:12 +0100 Subject: [PATCH 08/11] #369 added AnyPlutusData datatype. This field is identified through missing datatype description it can be anything. The correctness will be checked by the smartcontract itself. So it can't be distinguished within the blueprint. --- .../DataTypeProcessUtil.java | 11 ++++ .../FieldSpecProcessor.java | 51 +++++++++-------- .../processor/ClassDefinitionGenerator.java | 4 ++ .../processor/ConverterCodeGenerator.java | 14 +++++ .../annotation/processor/model/JavaType.java | 1 + .../annotation/processor/model/Type.java | 1 + .../blueprint/BlueprintCompileTest.java | 3 +- .../model/AnyPlutusDataBlueprint.java | 7 +++ .../blueprint/AnyPlutusDataBlueprint.java | 7 +++ .../blueprint/AnyPlutusDataBlueprint.json | 57 +++++++++++++++++++ 10 files changed, 132 insertions(+), 24 deletions(-) create mode 100644 annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/AnyPlutusDataBlueprint.java create mode 100644 annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.java create mode 100644 annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.json diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java index 97b6bbc8..878e3d54 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java @@ -3,6 +3,7 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; import com.bloxbean.cardano.client.plutus.annotation.Constr; import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintSchema; +import com.bloxbean.cardano.client.plutus.spec.PlutusData; import com.squareup.javapoet.*; import javax.annotation.processing.ProcessingEnvironment; @@ -185,4 +186,14 @@ public FieldSpec processMapDataType(String javaDoc, BlueprintSchema schema, Stri .addJavadoc(javaDoc) .build(); } + + public FieldSpec processPlutusDataType(String javaDoc, BlueprintSchema schema, String alternativeName) { + if(schema.getDataType() != null) + throw new IllegalArgumentException("Schema is not of type plutusdata"); + String title = schema.getTitle() == null ? alternativeName : schema.getTitle(); + return FieldSpec.builder(PlutusData.class, title) + .addModifiers(Modifier.PRIVATE) + .addJavadoc(javaDoc) + .build(); + } } diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java index 83d6b7e8..b216dca5 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java @@ -60,29 +60,34 @@ public static Tuple> collectAllFields(BlueprintSch */ public List CreateFieldSpecForDataTypes(String javaDoc, BlueprintSchema schema, String className, String alternativeName) { List specs = new ArrayList<>(); - switch (schema.getDataType()) { - case bytes: - specs.add(dataTypeProcessUtil.processBytesDataType(javaDoc, schema, alternativeName)); - break; - case integer: - specs.add(dataTypeProcessUtil.processIntegerDataType(javaDoc, schema, alternativeName)); - break; - case bool: - specs.add(dataTypeProcessUtil.processBoolDataType(javaDoc, schema, alternativeName)); - break; - case list: - specs.add(dataTypeProcessUtil.processListDataType(javaDoc, schema, alternativeName)); - break; - case map: - specs.add(dataTypeProcessUtil.processMapDataType(javaDoc, schema, className, alternativeName)); - break; - case constructor: - specs.addAll(dataTypeProcessUtil.processConstructorDataType(javaDoc, schema, className, alternativeName)); - break; - case string: - specs.add(dataTypeProcessUtil.processStringDataType(javaDoc, schema, alternativeName)); - break; - default: + if(schema.getDataType() == null) { // Processing Anyplutusdata + specs.add(dataTypeProcessUtil.processPlutusDataType(javaDoc, schema, alternativeName)); + + } else { + switch (schema.getDataType()) { + case bytes: + specs.add(dataTypeProcessUtil.processBytesDataType(javaDoc, schema, alternativeName)); + break; + case integer: + specs.add(dataTypeProcessUtil.processIntegerDataType(javaDoc, schema, alternativeName)); + break; + case bool: + specs.add(dataTypeProcessUtil.processBoolDataType(javaDoc, schema, alternativeName)); + break; + case list: + specs.add(dataTypeProcessUtil.processListDataType(javaDoc, schema, alternativeName)); + break; + case map: + specs.add(dataTypeProcessUtil.processMapDataType(javaDoc, schema, className, alternativeName)); + break; + case constructor: + specs.addAll(dataTypeProcessUtil.processConstructorDataType(javaDoc, schema, className, alternativeName)); + break; + case string: + specs.add(dataTypeProcessUtil.processStringDataType(javaDoc, schema, alternativeName)); + break; + default: + } } return specs; } diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ClassDefinitionGenerator.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ClassDefinitionGenerator.java index 4924d03f..87c6af85 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ClassDefinitionGenerator.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ClassDefinitionGenerator.java @@ -5,6 +5,7 @@ import com.bloxbean.cardano.client.plutus.annotation.PlutusIgnore; import com.bloxbean.cardano.client.plutus.annotation.processor.exception.NotSupportedException; import com.bloxbean.cardano.client.plutus.annotation.processor.model.*; +import com.bloxbean.cardano.client.plutus.spec.PlutusData; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.ParameterizedTypeName; import com.squareup.javapoet.TypeName; @@ -164,6 +165,9 @@ private FieldType detectFieldType(TypeName typeName, TypeMirror typeMirror) thro } else if (typeName.equals(TypeName.BOOLEAN)) { fieldType.setType(Type.BOOL); fieldType.setJavaType(JavaType.BOOLEAN); + } else if (typeName.equals(TypeName.get(PlutusData.class))) { + fieldType.setType(Type.PLUTUSDATA); + fieldType.setJavaType(JavaType.PLUTUSDATA); } else if (typeName instanceof ParameterizedTypeName && (((ParameterizedTypeName) typeName).rawType.equals(ClassName.get(List.class)) || isAssignableToList(typeMirror))) { diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConverterCodeGenerator.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConverterCodeGenerator.java index 3bdb7acf..346ee94a 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConverterCodeGenerator.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConverterCodeGenerator.java @@ -178,6 +178,14 @@ private MethodSpec generateToPlutusDataMethod(ClassDefinition classDef) { .add("\n") .build(); break; + case PLUTUSDATA: + codeBlock = CodeBlock.builder() + .add("//Field $L\n", field.getName()) + .add(nullCheckStatement(field, fieldOrGetterName(field))) + .addStatement("constr.getData().add(obj.$L)", fieldOrGetterName(field)) + .add("\n") + .build(); + break; case OPTIONAL: /*** Sample Optional Code if (obj.isEmpty()) @@ -555,6 +563,12 @@ private CodeBlock getDeserializeCodeBlockForField(Field field) { .add(generateMapDeserializeCode(field.getFieldType(), field.getName(), field.getName() + "Map", "entry")) .build(); break; + case PLUTUSDATA: + codeBlock = CodeBlock.builder() + .add("//Field $L\n", field.getName()) + .add("var $L = data.getPlutusDataList().get($L);\n", field.getName(), field.getIndex()) + .build(); + break; case OPTIONAL: // var optConstr = (ConstrPlutusData)data.getPlutusDataList().get(2); // if (optConstr.getAlternative() == 1) { diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/JavaType.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/JavaType.java index 6607470b..bbba919a 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/JavaType.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/JavaType.java @@ -19,6 +19,7 @@ public class JavaType { public final static JavaType BOOLEAN_OBJ = new JavaType("java.lang.Boolean", false); public final static JavaType BOOLEAN = new JavaType("boolean", false); + public final static JavaType PLUTUSDATA = new JavaType("plutusdata", false); private String name; private boolean userDefined; diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/Type.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/Type.java index 1de25c26..79995397 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/Type.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/model/Type.java @@ -8,6 +8,7 @@ public enum Type { MAP("map"), BOOL("bool"), CONSTRUCTOR("constructor"), + PLUTUSDATA("plutusdata"), OPTIONAL("optional"); private String type; diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java index 1600e08c..12d7cb30 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java @@ -22,7 +22,8 @@ void nestedListMapCompile() { JavaFileObjects.forResource("blueprint/ComplexTypesBlueprint.java"), JavaFileObjects.forResource("blueprint/HelloWorldBlueprint.java"), JavaFileObjects.forResource("blueprint/ListBlueprint.java"), - JavaFileObjects.forResource("blueprint/MapBlueprint.java") + JavaFileObjects.forResource("blueprint/MapBlueprint.java"), + JavaFileObjects.forResource("blueprint/AnyPlutusDataBlueprint.java") ); System.out.println(compilation.diagnostics()); diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/AnyPlutusDataBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/AnyPlutusDataBlueprint.java new file mode 100644 index 00000000..4e96b7ee --- /dev/null +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/AnyPlutusDataBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/AnyPlutusDataBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface AnyPlutusDataBlueprint { +} diff --git a/annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.java b/annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.java new file mode 100644 index 00000000..4e96b7ee --- /dev/null +++ b/annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.java @@ -0,0 +1,7 @@ +package com.bloxbean.cardano.client.plutus.annotation.blueprint.model; + +import com.bloxbean.cardano.client.plutus.annotation.Blueprint; + +@Blueprint(fileInRessources = "blueprint/AnyPlutusDataBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +public interface AnyPlutusDataBlueprint { +} diff --git a/annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.json b/annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.json new file mode 100644 index 00000000..46290841 --- /dev/null +++ b/annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.json @@ -0,0 +1,57 @@ +{ + "preamble": { + "title": "kammer/anyplutusdata", + "description": "Aiken contracts for project 'kammer/anyplutusdata'", + "version": "0.0.0", + "plutusVersion": "v2", + "compiler": { + "name": "Aiken", + "version": "v1.0.21-alpha+4b04517" + }, + "license": "Apache-2.0" + }, + "validators": [ + { + "title": "anyplutusdata.anyplutusdata", + "datum": { + "title": "datum", + "schema": { + "$ref": "#/definitions/test_module~1Foo" + } + }, + "redeemer": { + "title": "redeemer", + "schema": { + "$ref": "#/definitions/Data" + } + }, + "compiledCode": "5833010000323222253330044a22930a99802a491856616c696461746f722072657475726e65642066616c736500136565734ae701", + "hash": "52a21f2b4f282074cb6c5aefef20d18c25f3657ca348c73875810c37" + } + ], + "definitions": { + "Data": { + "title": "anyPlutusData", + "description": "Any Plutus data." + }, + "Int": { + "dataType": "integer" + }, + "test_module/Foo": { + "title": "Foo", + "anyOf": [ + { + "title": "Foo", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "foo", + "$ref": "#/definitions/Int" + } + ] + } + ] + } + } + } \ No newline at end of file From 0acec8567b1da391b46a5d5413d7b5e5b94e6a0d Mon Sep 17 00:00:00 2001 From: Satya Date: Sun, 3 Mar 2024 21:47:33 +0800 Subject: [PATCH 09/11] chore: #369 Fixed typo --- .../blueprint_processor/BlueprintAnnotationProcessor.java | 6 +++--- .../blueprint_processor/model/BasicTypesBlueprint.java | 2 +- .../blueprint_processor/model/ComplexTypesBlueprint.java | 2 +- .../blueprint_processor/model/HelloWorldBlueprint.java | 2 +- .../annotation/blueprint_processor/model/ListBlueprint.java | 2 +- .../annotation/blueprint_processor/model/MapBlueprint.java | 2 +- .../annotation/blueprint/model/AnyPlutusDataBlueprint.java | 2 +- .../annotation/blueprint/model/BasicTypesBlueprint.java | 2 +- .../annotation/blueprint/model/ComplexTypesBlueprint.java | 2 +- .../annotation/blueprint/model/HelloWorldBlueprint.java | 2 +- .../plutus/annotation/blueprint/model/ListBlueprint.java | 2 +- .../plutus/annotation/blueprint/model/MapBlueprint.java | 2 +- .../test/resources/blueprint/AnyPlutusDataBlueprint.java | 2 +- .../src/test/resources/blueprint/BasicTypesBlueprint.java | 2 +- .../src/test/resources/blueprint/ComplexTypesBlueprint.java | 2 +- .../src/test/resources/blueprint/HelloWorldBlueprint.java | 2 +- .../src/test/resources/blueprint/ListBlueprint.java | 2 +- .../src/test/resources/blueprint/MapBlueprint.java | 2 +- .../cardano/client/plutus/annotation/Blueprint.java | 3 +-- 19 files changed, 21 insertions(+), 22 deletions(-) diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java index 85b76bd1..583938c5 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java @@ -58,7 +58,7 @@ public boolean process(Set annotations, RoundEnvironment } File blueprintFile = getFileFromAnnotation(annotation); if (blueprintFile == null || !blueprintFile.exists()) { - log.error("Blueprint file {} not found", annotation.fileInRessources()); + log.error("Blueprint file {} not found", annotation.fileInResources()); return false; } PlutusContractBlueprint plutusContractBlueprint; @@ -94,8 +94,8 @@ private File getFileFromAnnotation(Blueprint annotation) { File blueprintFile = null; if(!annotation.file().isEmpty()) blueprintFile = new File(annotation.file()); - if(!annotation.fileInRessources().isEmpty()) - blueprintFile = getFileFromRessourcers(annotation.fileInRessources()); + if(!annotation.fileInResources().isEmpty()) + blueprintFile = getFileFromRessourcers(annotation.fileInResources()); if(blueprintFile == null || !blueprintFile.exists()) { log.error("Blueprint file {} not found", annotation.file()); return null; diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java index cba678af..ba644186 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface BasicTypesBlueprint { } diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java index a4ef4487..f3598a88 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface ComplexTypesBlueprint { } diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java index e80a337d..f9ef37c5 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface HelloWorldBlueprint { } diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java index dcd33870..b060deb7 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface ListBlueprint { } diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java index f76d3cbc..8e9dd344 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface MapBlueprint { } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/AnyPlutusDataBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/AnyPlutusDataBlueprint.java index 4e96b7ee..8ec1a184 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/AnyPlutusDataBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/AnyPlutusDataBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/AnyPlutusDataBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/AnyPlutusDataBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface AnyPlutusDataBlueprint { } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java index c8ee7e8d..97d6c740 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/BasicTypesBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface BasicTypesBlueprint { } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java index 3f138eeb..1361eb43 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ComplexTypesBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface ComplexTypesBlueprint { } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java index f4f5d859..7a99e797 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/HelloWorldBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface HelloWorldBlueprint { } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java index ee59454c..e5d8217e 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/ListBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface ListBlueprint { } diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java index 4326a013..e40f154e 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/model/MapBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface MapBlueprint { } diff --git a/annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.java b/annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.java index 4e96b7ee..8ec1a184 100644 --- a/annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.java +++ b/annotation-processor/src/test/resources/blueprint/AnyPlutusDataBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/AnyPlutusDataBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/AnyPlutusDataBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface AnyPlutusDataBlueprint { } diff --git a/annotation-processor/src/test/resources/blueprint/BasicTypesBlueprint.java b/annotation-processor/src/test/resources/blueprint/BasicTypesBlueprint.java index c8ee7e8d..97d6c740 100644 --- a/annotation-processor/src/test/resources/blueprint/BasicTypesBlueprint.java +++ b/annotation-processor/src/test/resources/blueprint/BasicTypesBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface BasicTypesBlueprint { } diff --git a/annotation-processor/src/test/resources/blueprint/ComplexTypesBlueprint.java b/annotation-processor/src/test/resources/blueprint/ComplexTypesBlueprint.java index 3f138eeb..1361eb43 100644 --- a/annotation-processor/src/test/resources/blueprint/ComplexTypesBlueprint.java +++ b/annotation-processor/src/test/resources/blueprint/ComplexTypesBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface ComplexTypesBlueprint { } diff --git a/annotation-processor/src/test/resources/blueprint/HelloWorldBlueprint.java b/annotation-processor/src/test/resources/blueprint/HelloWorldBlueprint.java index f4f5d859..7a99e797 100644 --- a/annotation-processor/src/test/resources/blueprint/HelloWorldBlueprint.java +++ b/annotation-processor/src/test/resources/blueprint/HelloWorldBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface HelloWorldBlueprint { } diff --git a/annotation-processor/src/test/resources/blueprint/ListBlueprint.java b/annotation-processor/src/test/resources/blueprint/ListBlueprint.java index ee59454c..e5d8217e 100644 --- a/annotation-processor/src/test/resources/blueprint/ListBlueprint.java +++ b/annotation-processor/src/test/resources/blueprint/ListBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface ListBlueprint { } diff --git a/annotation-processor/src/test/resources/blueprint/MapBlueprint.java b/annotation-processor/src/test/resources/blueprint/MapBlueprint.java index 4326a013..e40f154e 100644 --- a/annotation-processor/src/test/resources/blueprint/MapBlueprint.java +++ b/annotation-processor/src/test/resources/blueprint/MapBlueprint.java @@ -2,6 +2,6 @@ import com.bloxbean.cardano.client.plutus.annotation.Blueprint; -@Blueprint(fileInRessources = "blueprint/MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") +@Blueprint(fileInResources = "blueprint/MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") public interface MapBlueprint { } diff --git a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java index 0f00deda..6a62e2b5 100644 --- a/plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java +++ b/plutus/src/main/java/com/bloxbean/cardano/client/plutus/annotation/Blueprint.java @@ -1,6 +1,5 @@ package com.bloxbean.cardano.client.plutus.annotation; -import java.io.File; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -20,7 +19,7 @@ * File in ressources * @return */ - String fileInRessources() default ""; + String fileInResources() default ""; /** * Name of package the generated classes should be in From 7a2b41d294196ece92ac6acc3852eb6bbf63eff0 Mon Sep 17 00:00:00 2001 From: Satya Date: Sun, 3 Mar 2024 22:24:37 +0800 Subject: [PATCH 10/11] chore: #369 Remove test classes --- .../blueprint_processor/model/BasicTypesBlueprint.java | 7 ------- .../blueprint_processor/model/ComplexTypesBlueprint.java | 7 ------- .../blueprint_processor/model/HelloWorldBlueprint.java | 7 ------- .../blueprint_processor/model/ListBlueprint.java | 7 ------- .../annotation/blueprint_processor/model/MapBlueprint.java | 7 ------- 5 files changed, 35 deletions(-) delete mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java delete mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java delete mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java delete mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java delete mode 100644 annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java deleted file mode 100644 index ba644186..00000000 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/BasicTypesBlueprint.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.model; - -import com.bloxbean.cardano.client.plutus.annotation.Blueprint; - -@Blueprint(fileInResources = "blueprint/BasicTypesBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") -public interface BasicTypesBlueprint { -} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java deleted file mode 100644 index f3598a88..00000000 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ComplexTypesBlueprint.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.model; - -import com.bloxbean.cardano.client.plutus.annotation.Blueprint; - -@Blueprint(fileInResources = "blueprint/ComplexTypeBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") -public interface ComplexTypesBlueprint { -} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java deleted file mode 100644 index f9ef37c5..00000000 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/HelloWorldBlueprint.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.model; - -import com.bloxbean.cardano.client.plutus.annotation.Blueprint; - -@Blueprint(fileInResources = "blueprint/Hello_world-Blueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") -public interface HelloWorldBlueprint { -} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java deleted file mode 100644 index b060deb7..00000000 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/ListBlueprint.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.model; - -import com.bloxbean.cardano.client.plutus.annotation.Blueprint; - -@Blueprint(fileInResources = "blueprint/ListBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") -public interface ListBlueprint { -} diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java deleted file mode 100644 index 8e9dd344..00000000 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/model/MapBlueprint.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.model; - -import com.bloxbean.cardano.client.plutus.annotation.Blueprint; - -@Blueprint(fileInResources = "blueprint/MapBlueprint.json", packageName = "com.bloxbean.cardano.client.plutus.annotation.blueprint.model") -public interface MapBlueprint { -} From 481e3683d49745c0fcddab24cc9aae49b93c88e4 Mon Sep 17 00:00:00 2001 From: Satya Date: Wed, 17 Jul 2024 23:34:21 +0800 Subject: [PATCH 11/11] Fix: Fix camecase class name, package refactoring and target package folder --- annotation-processor/build.gradle | 1 + .../processor/ConstrAnnotationProcessor.java | 4 +- .../BlueprintAnnotationProcessor.java | 2 +- .../blueprint}/DataTypeProcessUtil.java | 3 +- .../blueprint}/FieldSpecProcessor.java | 3 +- .../blueprint}/ValidatorProcessor.java | 7 ++- .../util}/JavaFileUtil.java | 19 +++++- .../javax.annotation.processing.Processor | 4 +- .../annotation/blueprint/BluePrintTest.java | 59 ++++++++++++------- .../blueprint/BlueprintCompileTest.java | 2 +- gradle/libs.versions.toml | 1 + 11 files changed, 72 insertions(+), 33 deletions(-) rename annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/{blueprint_processor => processor/blueprint}/BlueprintAnnotationProcessor.java (98%) rename annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/{blueprint_processor => processor/blueprint}/DataTypeProcessUtil.java (98%) rename annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/{blueprint_processor => processor/blueprint}/FieldSpecProcessor.java (97%) rename annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/{blueprint_processor => processor/blueprint}/ValidatorProcessor.java (96%) rename annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/{blueprint_processor => processor/util}/JavaFileUtil.java (79%) diff --git a/annotation-processor/build.gradle b/annotation-processor/build.gradle index 2c20c54c..284d825b 100644 --- a/annotation-processor/build.gradle +++ b/annotation-processor/build.gradle @@ -6,6 +6,7 @@ dependencies { exclude group: 'com.google.guava', module: 'guava' } api libs.javapoet + api libs.apache.common.text testImplementation libs.google.testing.compile testImplementation libs.lombok diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConstrAnnotationProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConstrAnnotationProcessor.java index 31dab2b8..5d749804 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConstrAnnotationProcessor.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/ConstrAnnotationProcessor.java @@ -80,8 +80,10 @@ public boolean process(Set annotations, RoundEnvironment JavaFile javaFile = JavaFile.builder(classDefinition.getPackageName(), typeSpec) .build(); + String fullClassName = classDefinition.getPackageName() + "." + classDefinition.getName(); + JavaFileObject builderFile = processingEnv.getFiler() - .createSourceFile(classDefinition.getName()); + .createSourceFile(fullClassName); Writer writer = builderFile.openWriter(); javaFile.writeTo(writer); writer.close(); diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/BlueprintAnnotationProcessor.java similarity index 98% rename from annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java rename to annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/BlueprintAnnotationProcessor.java index 583938c5..a602fea4 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/BlueprintAnnotationProcessor.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/BlueprintAnnotationProcessor.java @@ -1,4 +1,4 @@ -package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor; +package com.bloxbean.cardano.client.plutus.annotation.processor.blueprint; import com.bloxbean.cardano.client.plutus.annotation.Blueprint; import com.bloxbean.cardano.client.plutus.blueprint.PlutusBlueprintLoader; diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/DataTypeProcessUtil.java similarity index 98% rename from annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java rename to annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/DataTypeProcessUtil.java index 878e3d54..60c498df 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/DataTypeProcessUtil.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/DataTypeProcessUtil.java @@ -1,7 +1,8 @@ -package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor; +package com.bloxbean.cardano.client.plutus.annotation.processor.blueprint; import com.bloxbean.cardano.client.plutus.annotation.Blueprint; import com.bloxbean.cardano.client.plutus.annotation.Constr; +import com.bloxbean.cardano.client.plutus.annotation.processor.util.JavaFileUtil; import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintSchema; import com.bloxbean.cardano.client.plutus.spec.PlutusData; import com.squareup.javapoet.*; diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/FieldSpecProcessor.java similarity index 97% rename from annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java rename to annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/FieldSpecProcessor.java index b216dca5..35d994ee 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/FieldSpecProcessor.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/FieldSpecProcessor.java @@ -1,7 +1,8 @@ -package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor; +package com.bloxbean.cardano.client.plutus.annotation.processor.blueprint; import com.bloxbean.cardano.client.plutus.annotation.Blueprint; import com.bloxbean.cardano.client.plutus.annotation.Constr; +import com.bloxbean.cardano.client.plutus.annotation.processor.util.JavaFileUtil; import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintDatatype; import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintSchema; import com.bloxbean.cardano.client.util.Tuple; diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/ValidatorProcessor.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/ValidatorProcessor.java similarity index 96% rename from annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/ValidatorProcessor.java rename to annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/ValidatorProcessor.java index 628fa95e..4fab64dd 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/ValidatorProcessor.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/blueprint/ValidatorProcessor.java @@ -1,10 +1,11 @@ -package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor; +package com.bloxbean.cardano.client.plutus.annotation.processor.blueprint; import co.nstant.in.cbor.model.ByteString; import com.bloxbean.cardano.client.address.AddressProvider; import com.bloxbean.cardano.client.common.model.Network; import com.bloxbean.cardano.client.exception.CborDeserializationException; import com.bloxbean.cardano.client.plutus.annotation.Blueprint; +import com.bloxbean.cardano.client.plutus.annotation.processor.util.JavaFileUtil; import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintDatum; import com.bloxbean.cardano.client.plutus.blueprint.model.Validator; import com.bloxbean.cardano.client.plutus.spec.PlutusV2Script; @@ -41,8 +42,8 @@ public void processValidator(Validator validator) { // preparation of standard fields String packageName = annotation.packageName() + "." + validator.getTitle().split("\\.")[0]; String title = validator.getTitle().split("\\.")[1]; - title = JavaFileUtil.firstUpperCase(title); - + title = JavaFileUtil.toCamelCase(title); + List fields = ValidatorProcessor.getFieldSpecsForValidator(validator); // processing of fields diff --git a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/JavaFileUtil.java b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/util/JavaFileUtil.java similarity index 79% rename from annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/JavaFileUtil.java rename to annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/util/JavaFileUtil.java index 536a99be..834e13e8 100644 --- a/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/blueprint_processor/JavaFileUtil.java +++ b/annotation-processor/src/main/java/com/bloxbean/cardano/client/plutus/annotation/processor/util/JavaFileUtil.java @@ -1,10 +1,11 @@ -package com.bloxbean.cardano.client.plutus.annotation.blueprint_processor; +package com.bloxbean.cardano.client.plutus.annotation.processor.util; import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintDatatype; import com.bloxbean.cardano.client.plutus.blueprint.model.BlueprintSchema; import com.squareup.javapoet.JavaFile; import com.squareup.javapoet.TypeSpec; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.text.CaseUtils; import javax.annotation.processing.ProcessingEnvironment; import javax.tools.JavaFileObject; @@ -25,6 +26,18 @@ public static String firstUpperCase(String s) { return s.substring(0, 1).toUpperCase() + s.substring(1); } + /** + * Converts a string to camel case + * @param s + * @return + */ + public static String toCamelCase(String s) { + if (s == null || s.isEmpty()) + return s; + + return CaseUtils.toCamelCase(s, true, '_', ' ', '-'); + } + /** * Creates a Java file from a TypeSpec with a given classname and package * @@ -39,8 +52,10 @@ public static void createJavaFile(String packageName, TypeSpec build, String cla JavaFileObject builderFile = null; try { + + String fullClassName = packageName + "." + className; builderFile = processingEnv.getFiler() - .createSourceFile(className); + .createSourceFile(fullClassName); Writer writer = builderFile.openWriter(); javaFile.writeTo(writer); writer.close(); diff --git a/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor index f9558d0f..bad2b215 100644 --- a/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor +++ b/annotation-processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -1,2 +1,2 @@ -com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.BlueprintAnnotationProcessor -com.bloxbean.cardano.client.plutus.annotation.processor.ConstrAnnotationProcessor \ No newline at end of file +com.bloxbean.cardano.client.plutus.annotation.processor.blueprint.BlueprintAnnotationProcessor +com.bloxbean.cardano.client.plutus.annotation.processor.ConstrAnnotationProcessor diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java index e296c571..98c18af4 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BluePrintTest.java @@ -1,11 +1,28 @@ package com.bloxbean.cardano.client.plutus.annotation.blueprint; import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.*; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.BasicTypesDatum; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.BasicTypesDatumAction; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.BasicTypesDatumAction1Burn; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.BasicTypesDatumConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.BasicTypesRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.BasicTypesRedeemerConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.ComplexStructuresRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.ComplexStructuresRedeemerConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.ComplexStructuresRedeemerNestedA; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.ComplexStructuresRedeemerNestedANestedB; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.HelloWorldDatum; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.HelloWorldDatumConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.HelloWorldRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.HelloWorldRedeemerConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.ListStrRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.MapbpRedeemer; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.MapbpRedeemerConverter; +import com.bloxbean.cardano.client.plutus.annotation.blueprint.model.NestedAConstr; import com.bloxbean.cardano.client.plutus.spec.ConstrPlutusData; import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -17,23 +34,23 @@ public class BluePrintTest { @Test public void bytesTest() { - Hello_worldDatum hello_worldDatum = new Hello_worldDatum(); - hello_worldDatum.setOwner(new byte[]{-88, -77, -72, 20, -90, 94, 94, 67, 119, 127, -119, -77, 12, -121, -22, -60, -29, 57, 106, -101, -100, 36, -51, 8, -42, 96, -108, -103}); - Hello_worldDatumConverter hello_worldDatumConverter = new Hello_worldDatumConverter(); - ConstrPlutusData plutusData = hello_worldDatumConverter.toPlutusData(hello_worldDatum); + HelloWorldDatum helloWorldDatum = new HelloWorldDatum(); + helloWorldDatum.setOwner(new byte[]{-88, -77, -72, 20, -90, 94, 94, 67, 119, 127, -119, -77, 12, -121, -22, -60, -29, 57, 106, -101, -100, 36, -51, 8, -42, 96, -108, -103}); + HelloWorldDatumConverter hello_worldDatumConverter = new HelloWorldDatumConverter(); + ConstrPlutusData plutusData = hello_worldDatumConverter.toPlutusData(helloWorldDatum); assertEquals("d8799f581ca8b3b814a65e5e43777f89b30c87eac4e3396a9b9c24cd08d6609499ff", plutusData.serializeToHex()); } @Test public void enumTest() { - Basic_typesDatum basic_typesDatum = new Basic_typesDatum(); - Basic_typesDatumAction basic_typesDatumAction = new Basic_typesDatumAction(); - basic_typesDatumAction.setBasic_typesDatumAction1Burn(new Basic_typesDatumAction1Burn()); + BasicTypesDatum basic_typesDatum = new BasicTypesDatum(); + BasicTypesDatumAction basic_typesDatumAction = new BasicTypesDatumAction(); + basic_typesDatumAction.setBasicTypesDatumAction1Burn(new BasicTypesDatumAction1Burn()); basic_typesDatum.setAction(basic_typesDatumAction); basic_typesDatum.setOwner("Hello, World".getBytes(StandardCharsets.UTF_8)); - Basic_typesDatumConverter basic_typesDatumConverter = new Basic_typesDatumConverter(); + BasicTypesDatumConverter basic_typesDatumConverter = new BasicTypesDatumConverter(); ConstrPlutusData plutusData = basic_typesDatumConverter.toPlutusData(basic_typesDatum); assertEquals("d8799f4c48656c6c6f2c20576f726c64d87a9fd87a80ffff", plutusData.serializeToHex()); @@ -41,10 +58,10 @@ public void enumTest() { @Test public void fillRedeemer() { - Hello_worldRedeemer hello_worldRedeemer = new Hello_worldRedeemer(); + HelloWorldRedeemer hello_worldRedeemer = new HelloWorldRedeemer(); hello_worldRedeemer.setMsg("Hello, World".getBytes(StandardCharsets.UTF_8)); - Hello_worldRedeemerConverter hello_worldRedeemerConverter = new Hello_worldRedeemerConverter(); + HelloWorldRedeemerConverter hello_worldRedeemerConverter = new HelloWorldRedeemerConverter(); ConstrPlutusData plutusData = hello_worldRedeemerConverter.toPlutusData(hello_worldRedeemer); assertEquals("d8799f4c48656c6c6f2c20576f726c64ff", plutusData.serializeToHex()); @@ -52,11 +69,11 @@ public void fillRedeemer() { @Test public void integerTest() { - Basic_typesRedeemer basic_typesRedeemer = new Basic_typesRedeemer(); + BasicTypesRedeemer basic_typesRedeemer = new BasicTypesRedeemer(); basic_typesRedeemer.setMsg("Hello, World"); basic_typesRedeemer.setCounter(10); - Basic_typesRedeemerConverter basic_typesRedeemerConverter = new Basic_typesRedeemerConverter(); + BasicTypesRedeemerConverter basic_typesRedeemerConverter = new BasicTypesRedeemerConverter(); ConstrPlutusData plutusData = basic_typesRedeemerConverter.toPlutusData(basic_typesRedeemer); assertEquals("d8799f4c48656c6c6f2c20576f726c640aff", plutusData.serializeToHex()); @@ -64,19 +81,19 @@ public void integerTest() { @Test public void nestedTypeTest() { - Complex_structuresRedeemer complex_structuresRedeemer = new Complex_structuresRedeemer(); + ComplexStructuresRedeemer complex_structuresRedeemer = new ComplexStructuresRedeemer(); complex_structuresRedeemer.setOwner("Hello, World".getBytes(StandardCharsets.UTF_8)); - Complex_structuresRedeemerNestedA a = new Complex_structuresRedeemerNestedA(); + ComplexStructuresRedeemerNestedA a = new ComplexStructuresRedeemerNestedA(); a.setMsg("Hello, World from A"); a.setCount(10); - Complex_structuresRedeemerNestedANestedB b = new Complex_structuresRedeemerNestedANestedB(); + ComplexStructuresRedeemerNestedANestedB b = new ComplexStructuresRedeemerNestedANestedB(); b.setCount2(20); b.setMsg2("Hello, World from B"); a.setNestedB(b); complex_structuresRedeemer.setNestedA(a); - Complex_structuresRedeemerConverter complex_structuresRedeemerConverter = new Complex_structuresRedeemerConverter(); + ComplexStructuresRedeemerConverter complex_structuresRedeemerConverter = new ComplexStructuresRedeemerConverter(); ConstrPlutusData plutusData = complex_structuresRedeemerConverter.toPlutusData(complex_structuresRedeemer); String s = plutusData.serializeToHex(); assertEquals("d8799f4c48656c6c6f2c20576f726c64d8799f0a5348656c6c6f2c20576f726c642066726f6d2041d8799f145348656c6c6f2c20576f726c642066726f6d2042ffffff", plutusData.serializeToHex()); @@ -84,12 +101,12 @@ public void nestedTypeTest() { @Test public void listTest() { - List_StrRedeemer list_strRedeemer = new List_StrRedeemer(); + ListStrRedeemer list_strRedeemer = new ListStrRedeemer(); NestedAConstr nestedAConstr = new NestedAConstr(); nestedAConstr.setMsg("Hello, World"); list_strRedeemer.setSimpleList(List.of(List.of(nestedAConstr))); - List_StrRedeemerConverter list_strRedeemerConverter = new List_StrRedeemerConverter(); + ListStrRedeemerConverter list_strRedeemerConverter = new ListStrRedeemerConverter(); ConstrPlutusData plutusData = list_strRedeemerConverter.toPlutusData(list_strRedeemer); assertEquals("d8799f9f9fd8799f4c48656c6c6f2c20576f726c64ffffffff", plutusData.serializeToHex()); @@ -97,14 +114,14 @@ public void listTest() { @Test public void mapTest() { - MapBPRedeemer mapBPRedeemer = new MapBPRedeemer(); + MapbpRedeemer mapBPRedeemer = new MapbpRedeemer(); Map map = new HashMap<>(); NestedAConstr nestedAConstr = new NestedAConstr(); nestedAConstr.setMsg("Hello, World"); map.put(nestedAConstr, 10); mapBPRedeemer.setMap(map); - MapBPRedeemerConverter mapBPRedeemerConverter = new MapBPRedeemerConverter(); + MapbpRedeemerConverter mapBPRedeemerConverter = new MapbpRedeemerConverter(); ConstrPlutusData plutusData = mapBPRedeemerConverter.toPlutusData(mapBPRedeemer); assertEquals("d8799fa1d8799f4c48656c6c6f2c20576f726c64ff0aff", plutusData.serializeToHex()); diff --git a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java index 12d7cb30..b80868cf 100644 --- a/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java +++ b/annotation-processor/src/test/java/com/bloxbean/cardano/client/plutus/annotation/blueprint/BlueprintCompileTest.java @@ -1,6 +1,6 @@ package com.bloxbean.cardano.client.plutus.annotation.blueprint; -import com.bloxbean.cardano.client.plutus.annotation.blueprint_processor.BlueprintAnnotationProcessor; +import com.bloxbean.cardano.client.plutus.annotation.processor.blueprint.BlueprintAnnotationProcessor; import com.google.testing.compile.Compilation; import com.google.testing.compile.JavaFileObjects; import org.junit.jupiter.api.Test; diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index aea6a758..429a7f8b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -22,3 +22,4 @@ javapoet="com.squareup:javapoet:1.13.0" google-testing-compile="com.google.testing.compile:compile-testing:0.21.0" lombok = "org.projectlombok:lombok:1.18.30" +apache-common-text="org.apache.commons:commons-text:1.12.0"