Giter Site home page Giter Site logo

matyb / annotated-bundle Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 216 KB

A class similar to java.util.ResourceBundle, but binds optional annotations to their properties. Useful for adding meta data to properties for testing and what not, that is not readily available to existing code.

License: Apache License 2.0

Java 97.47% Shell 2.53%

annotated-bundle's Introduction

A java.util.ResourceBundle replacement for configuration properties. Helpful for attaching metadata to properties for the purpose of validating, inspecting, binding, etc. properties without infringing on any existing usage of those resources.

This is not a replacement for xml (JSON, etc.) or any structured data format you may find in configuration files, those suit that purpose much better. This is, however a simple way you can augment your production properties files with information that is only visible and significant to your tests or validation code.

For example the property: warning.dialog.alert.icon=/images/w_alert.png

Could be analyzed by acceptance tests or validation code w/ data from the comment (implicitly bound to the warning.dialog.alert.icon property because it precedes the property line):

  #@ location:warning_dialog; resource_type:png-image;
  warning.dialog.alert.icon=/images/w_alert.png

Now at runtime, the annotated keys and values are available via the AnnotatedResourceBundle class. Keeping with the same example:

AnnotatedResourceBundle rb = new AnnotatedResourceBundle("images.properties");
for(Entry<String, String> entry : rb.getAttributes("warning.dialog.alert.icon").entrySet()){
      System.out.println(entry);
}

would output:

  location=warning_dialog
  resource_type=png-image

There is also the ability to reference properties in the annotations:

  #@ host:${host_name}; user:${user_name}; password:${pass};
  connection=my_connection://
  host_name=www.github.com
  user_name=matyb
  password=pass123
  AnnotatedResourceBundle annotatedResourceBundle = new AnnotatedResourceBundle("connections.properties");
  for(Entry<String, String> entry : annotatedResourceBundle.getAttributes("connection").entrySet()){
      System.out.println(entry);
  }

would output:

  host_name=www.github.com
  user_name=matyb
  password=pass123

Lastly - the annotations can be added outside the class, they are merely bound using the @ symbol. So in a project where you don't want this information in your production code (ie you cannot or do not want to modify it):

  project/src/pkg/some_file.properties
  tests/test/pkg/some_file.annotations

We can attach the annotations outside the implementations code (so that this metadata can easily vary independently of the properties). So with a property like: warning.dialog.alert.icon=/images/w_alert.png

in the properties file, we can add metadata at runtime to it with:

#@ @warning.dialog.alert.icon; location:warning_dialog; resource_type:png-image;

added to the annotations file (in the same pkg).

For specific examples, please check the tests.

annotated-bundle's People

Contributors

matyb avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.