Skip to main content

A helper package to generate code from declarative json

Project description

lance

lance is a library to generate code from code.

Usage

Let's see how we can generate code by using lance

Java

Class Generation

Here is a json file which is fed to lance library to generate java code

 {
   "fqcn": "com.susamn.MetaClass",
   "type": "CLASS"
 }

generates java code as:

package com.susamn;

public class MetaClass {

}
  • fqcn is Fully Qualified Class Name

Seems pretty easy right. Let's see some involved code generation which is usable. Here is the json file for that:

{
  "generate_path": "",
  "classes":[
    {
      "fqcn": "com.susamn.Meta",
      "type": "CLASS",
      "extends": {
        "fqcn": "com.susamn.Parent",
        "generic_types": ["java.lang.Integer"]
      },
      "implements": [
        {
          "fqcn": "java.io.Serializable",
          "generic_types": ["java.lang.Integer"]
        },
        {
          "fqcn": "org.apache.Logger",
          "generic_types": ["com.susamn.Root","java.lang.String"]
        }

      ],
      "annotations": [
        {
          "fqcn": "com.susamn.ClassLevelAnnotation1"
        },
        {
          "fqcn": "com.susamn.ClassLevelAnnotation2",
          "data": {
            "key1": {
             "type": "INTEGER",
             "value": 23
           }
         }
        }
      ],
      "attributes": [
        {
          "name": "logger",
          "mode": "public",
          "type": {
            "of": "CLASS",
            "fqcn": "org.slf4j.api.Logger"
          },
          "accessors": true,
          "initialized_form": {
            "form": "LoggerFactory.getLogger(RiskAssessmentController.class)",
            "imports": ["org.apache.logger.api.LoggerFactory","com.susamn.RiskAssessmentController"]
          }
        },
        {
          "name": "foo",
          "mode": "private",
          "type": {
           "of": "STRING"
          },
          "accessors": true,
          "initialized_form": {
            "form": "\"Foo_Value\"",
            "imports": []
          },
          "annotation_level": "GETTER_ANNOTATION",
          "annotations": [
           {
              "fqcn": "com.susamn.Annotation1",
              "data": {
               "key1": {
                 "type": "INTEGER",
                 "value": 23
                }
             }
           },
            {
             "fqcn": "com.susamn.Annotation2",
              "data": {
                "key1": {
                 "type": "STRING",
                  "value": "String data"
                }
             }
            }
          ]
       },
        {
          "name": "foobar",
          "mode": "private",
         "type": {
           "of": "STRING"
         },
         "accessors": true,
          "annotations": [
            {
              "fqcn": "com.susamn.Annotation3"
            },
            {
              "fqcn": "com.susamn.Annotation4",
              "data": {
               "key1": {
                  "type": "INTEGER",
                  "value": 23
                }
              }
           }
          ]
       },
        {
          "name": "bar",
          "mode": "private",
         "type": {
           "of": "LIST_STRING"
         },
         "accessors": true
       },
        {
         "name": "far",
         "mode": "private",
          "type": {
            "of":"LIST_INTEGER"
          },
         "accessors": true
        },
       {
          "name": "boo",
         "mode": "private",
          "type": {
            "of":"LIST_CLASS",
           "fqcn": "com.susamn.Custom"
          },
          "accessors": true
       },
       {
          "name": "doo",
          "mode": "private",
          "type": {
            "of":"INTEGER"
         },
         "accessors": true
       },
        {
          "name": "dar",
          "mode": "private",
         "type": {
            "of":"FLOAT"
         }
       }
     ],
      "methods": [
       {
         "name": "processRequest",
         "mode": "public",
         "type": {
           "of": "STRING"
         },
         "inputs": [
           {
              "name": "body",
              "type": {
               "of": "STRING",
               "annotations": [
                 {
                    "fqcn": "com.susamn.ResponseBody"
                  }
                ]
              }
            },
            {
              "name": "clazz",
             "type": {
               "of":"LIST_CLASS",
               "fqcn": "com.susamn.Custom2"
              }
           },
           {
             "type": {
               "of": "CLASS",
               "fqcn": "org.springframework.web.mvc.HttpEntity",
               "generic_types": ["java.lang.String"]
             }
           }
         ],
          "annotations": [
            {
              "fqcn": "com.susamn.Annotation11",
             "data": {
               "key1": {
                  "type": "INTEGER",
                  "value": 78.10
               }
              }
            },
            {
              "fqcn": "com.susamn.Annotation10",
              "data": {
                "key1": {
                 "type": "INTEGER",
                 "value": 12.19
               }
              }
            }
          ]
        },
        {
          "name": "syncRequest",
          "mode": "public",
          "type": {
            "of": "CLASS",
            "fqcn": "org.springframework.web.mvc.HttpEntity",
            "generic_types": ["GENERIC"]
          },
          "inputs": [
            {
              "name": "value",
              "type": {
                "of": "STRING",
                "annotations": [
                  {
                    "fqcn": "com.susamn.ResponseBody"
                  }
                ]
              }
            }
          ]
        },
        {
          "name": "deleteRequest",
          "mode": "public",
          "type": {
            "of": "CLASS",
            "fqcn": "org.springframework.web.mvc.HttpEntity",
            "generic_types": ["java.lang.Boolean"]
          },
          "inputs": [
            {
              "type": {
                "of": "STRING",
                "annotations": [
                  {
                    "fqcn": "com.susamn.ResponseBody"
                  }
                ]
              }
            }
          ],
          "body": {
            "form": ["Map<Integer,RiskAssessment> map = new HashMap<>();", "map.put(1, new RiskAssessment());"],
            "imports": ["java.util.Map","java.util.HashMap","com.susamn.RiskAssessment"]
          }
        }
      ]
    }
  ]
}

this generates java code:

package com.susamn;

import com.susamn.Annotation1;
import com.susamn.Annotation10;
import com.susamn.Annotation11;
import com.susamn.Annotation2;
import com.susamn.Annotation3;
import com.susamn.Annotation4;
import com.susamn.ClassLevelAnnotation1;
import com.susamn.ClassLevelAnnotation2;
import com.susamn.Custom;
import com.susamn.Custom2;
import com.susamn.Parent;
import com.susamn.ResponseBody;
import com.susamn.RiskAssessment;
import com.susamn.RiskAssessmentController;
import com.susamn.Root;
import java.io.Serializable;
import java.lang.Boolean;
import java.lang.Integer;
import java.lang.String;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.Logger;
import org.apache.logger.api.LoggerFactory;
import org.slf4j.api.Logger;
import org.springframework.web.mvc.HttpEntity;


@ClassLevelAnnotation1
@ClassLevelAnnotation2(key1 = 23)
public class Meta extends Parent<Integer> implements Serializable<Integer>, Logger<Root,String> {

    public Logger logger = LoggerFactory.getLogger(RiskAssessmentController.class);
    private List<String> bar;
    private List<Integer> far;
    private List<Custom> boo;
    private int doo;
    private float dar;

    @Annotation1(key1 = 23)
    @Annotation2(key1 = "String data")
    private String foo = "Foo_Value";

    @Annotation3
    @Annotation4(key1 = 23)
    private String foobar;

    public Logger getLogger(){
        return this.logger;
    }
    public void setLogger(Logger logger){
        this.logger = logger;
    }
    public String getFoo(){
        return this.foo;
    }
    public void setFoo(String foo){
        this.foo = foo;
    }
    public String getFoobar(){
        return this.foobar;
    }
    public void setFoobar(String foobar){
        this.foobar = foobar;
    }
    public List<String> getBar(){
        return this.bar;
    }
    public void setBar(List<String> bar){
        this.bar = bar;
    }
    public List<Integer> getFar(){
        return this.far;
    }
    public void setFar(List<Integer> far){
        this.far = far;
    }
    public List<Custom> getBoo(){
        return this.boo;
    }
    public void setBoo(List<Custom> boo){
        this.boo = boo;
    }
    public int getDoo(){
        return this.doo;
    }
    public void setDoo(int doo){
        this.doo = doo;
    }
    @Annotation11(key1 = 78.1)
    @Annotation10(key1 = 12.19)
    public String processRequest(@ResponseBody String body, List<Custom2> clazz, HttpEntity<String> val0){
    }
    public HttpEntity<?> syncRequest(@ResponseBody String value){
    }
    public HttpEntity<Boolean> deleteRequest(@ResponseBody String val0){
        Map<Integer,RiskAssessment> map = new HashMap<>();
        map.put(1, new RiskAssessment());
    }

}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lance-0.1.2.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

lance-0.1.2-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file lance-0.1.2.tar.gz.

File metadata

  • Download URL: lance-0.1.2.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

File hashes

Hashes for lance-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9a2de150816e4578a8d001d6e74ae7de83362502512438dd6365e21acd0d382f
MD5 547c6a3aaffbb280c3119afe3c2afd9e
BLAKE2b-256 354d676aedd3fb87b6e51b1d1909adf1e041ab63d854679d832219fc6c4c209f

See more details on using hashes here.

File details

Details for the file lance-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: lance-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

File hashes

Hashes for lance-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7a758443040f7d8def305b145f98767942ad5c3f4969766dcfef65d81a15be04
MD5 e058a98697ea5d90072a42865d4d0bab
BLAKE2b-256 667f4a86ae515d047041abb21c0f76351593c6e2446144088de99e47ddd0695f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page