[go: up one dir, main page]

Skip to content

GoodforGod/DummyMapper

Repository files navigation

DummyMapper 🗎

Java CI Quality Gate Status Maintainability Rating Reliability Rating

Intellij IDEA plugin for mapping Java or Kotlin Classes to format like JSON/AVRO/GraphQL/etc.

Content

Installation

  • Install using Intellij Plugin marketplace:
    • Navigate to site and click install button there.
  • Using IDE built-in plugin system on:
    • [Windows] File > Settings > Plugins > Marketplace > Search for "DummyMapper" > Install
    • [MacOS] Preferences > Settings > Plugins > Marketplace > Search for "DummyMapper" > Install
  • Manually:
    • Download the latest release, compile with ./gradlew buildPlugin and install it manually using File > Settings > Plugins > ⚙️ > Install Plugin from Disk...

Format Support

Plugin allow mapping Java or Kotlin Classes to different formats, this section describes information about supported formats and their options.

All format examples will be showed according to this class as example. Keep in mind that mapping is based on class fields, not getters\setters.

public class User {

    private class Credential {
        private String id;
        private long issued;
    }

    private UUID id;
    private String name;
    private List<String> roles;
    private Credential credential;
}

Supports also Kotlin classes:

data class ExampleData(val surname: String, val number: Int, val simple: ExampleSimple)

class ExampleSimple {

    var en: ExampleEnum? = null
    var name: String? = null
    var data: ExampleData? = null
    var surnames: List<String>? = null
    var numbers: Map<String, Int>? = null
    var names: Set<String>? = null
}

Json

Plugin uses DummyMaker library for generating classes and then converting to JSON format.

Allow mapping class as JSON example with fields filled with random values as if class example was serialized to JSON format.

  • Mapping options.. > Map as JSON
{
  "id" : "76fbb591-8e95-4df7-94ad-5d0606709141",
  "name" : "Alan",
  "roles" : [ "herbalist", "recycling-officer", "exploration-geologist" ],
  "credential" : {
    "id" : "a7ec6315-4292-454c-ac69-e61a7a36e07d",
    "issued" : 1565743683
  }
}

Also allow mapping class as array of JSONs.

  • Mapping options.. > Map as JSON Array

You can specify number of entries to generate in array.

[
  {
    "id": "526686d0-8d84-4b85-a751-77fa7a157a69",
    "name": "Rachel",
    "roles": [ "fitness-centre-manager", "plant-breeder", "television-production-assistant" ],
    "credential": {
      "id": "94f349c9-405a-4921-8dbf-8abfacc28cf1",
      "issued": 1548018277
    }
  }
]

Annotations Support

Annotations from Jackson are supported when serializing to JSON, but keep in mind that only fields used for serialization, not getters.

However, Jackson annotations from respected fields getters will be applied during serialization if found, but such behavior is not guarantied to be always correct.

Example for User class:

public class User {
    
    private UUID id;
    @JsonProperty(value = "firstName")
    private String name;
    private String surname;
    @JsonIgnore
    private List<String> roles;

    @JsonIgnore
    public String getSurname() {
        return surname;
    }
}

Mapping User class as JSON will result in (most of Jackson annotations and their parameters are supported):

{
  "id": "975e80ed-b95d-46b2-9338-519ff7083dd3",
  "firstName": "Alfred"
}

Json Schema

Allow mapping class as JSON Schema, 3 different drafts are available:

Mapping is under:

  • Mapping options.. > Map as JSON Schema
{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "type": "object",
  "properties": {
    "credential": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "issued": {
          "type": "integer"
        }
      }
    },
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "roles": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  }
}

Avro Schema

Allow mapping class as AVRO Schema for version 1.9.2

There is option for two different AVRO Schema builders like:

Mapping is under:

  • Mapping options.. > Map as AVRO Schema
{
  "type" : "record",
  "name" : "User",
  "namespace" : "io.goodforgod.dummymapper",
  "fields" : [ {
    "name" : "id",
    "type" : {
      "type" : "record",
      "name" : "UUID",
      "namespace" : "java.util",
      "fields" : [ ]
    }
  }, {
    "name" : "name",
    "type" : "string"
  }, {
    "name" : "roles",
    "type" : {
      "type" : "array",
      "items" : "string",
      "java-class" : "java.util.List"
    }
  }, {
    "name" : "credential",
    "type" : {
      "type" : "record",
      "name" : "Credential",
      "fields" : [ {
        "name" : "id",
        "type" : "string"
      }, {
        "name" : "issued",
        "type" : "long"
      } ]
    }
  } ]
}

Jackson Annotation Support

Jackson annotations are supported when serializing to AVRO Schema, but keep in mind that only fields used for serialization, not getters.

Most of Jackson annotations and their parameters are supported.

Example on how to mark field as required (Option Required By Default apply such transformation for all fields if selected):

public class User {
    
    @JsonProperty(required = true)
    private UUID id;
    private String name;
    private String surname;
}

Apache Annotation Support

Annotations from Apache Avro library supported when serializing to AVRO Schema.

GraphQL

Allow mapping class as GraphQL query with version v14

Mapping is under:

  • Mapping options.. > Map as GraphQL
schema {
  query: Query
}

type Credential {
  id: String
  issued: Long!
}

#Query root
type Query {
  credential: Credential
  id: UUID
  name: String
  roles: [String]
}

#Long type
scalar Long

#Unrepresentable type
scalar UNREPRESENTABLE

#UUID String
scalar UUID

Annotation Support

Annotations from GraphQL SPQR library supported when serializing to GraphQL.

Limitations

There some limitations for plugin when mapping classes:

  • Enums are not supported and thus will be displayed as String fields, except for Map as JSON where enum values will be generated correctly. This issue is due to library used in plugin unable to create Java Enums.
  • Getters, Setters not used for any mapping, only annotations from getters are used for mapping (depends on operation) if found. Standard Java naming convention expected from getters to be found.
  • Annotations Class Params are not supported. Thus, most of annotation parameters like String, Boolean, etc. are supported, but Class or other complex parameters not supported.

Licence

This project licensed under the Apache 2.0 - see the LICENSE file for details.