diff --git a/Jenkinsfile b/Jenkinsfile index 938b9ca..e2f77d7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -9,7 +9,7 @@ pipeline { sh "./gradlew clean assemble build" junit testResults: "build/test-results/test/*.xml" javadoc javadocDir: "build/docs/javadoc", keepAll: true - archiveArtifacts artifacts: 'build/libs/*.jar,benchmark/build/libs/*.jar,wson-cli/build/distributions/wson-cli-envelope-*.jar', + archiveArtifacts artifacts: 'build/libs/*.jar,benchmark/build/libs/*.jar,wson-cli/build/distributions/wson-cli-envelope-*.jar,wson-cli/build/distributions/wson-cli', allowEmptyArchive: true, fingerprint: true, onlyIfSuccessful: true diff --git a/antlr/build.gradle b/antlr/build.gradle index 578689c..de289e4 100644 --- a/antlr/build.gradle +++ b/antlr/build.gradle @@ -4,6 +4,7 @@ plugins { dependencies { implementation rootProject + runtimeOnly catalog.antlr.runtime testImplementation catalog.jwo testImplementation project(':test-utils') @@ -11,7 +12,6 @@ dependencies { testImplementation catalog.xz antlr catalog.antlr - antlr catalog.antlr.runtime } generateGrammarSource { diff --git a/benchmark/build.gradle b/benchmark/build.gradle index fbd3b2f..0d8eef1 100644 --- a/benchmark/build.gradle +++ b/benchmark/build.gradle @@ -1,5 +1,6 @@ plugins { alias(catalog.plugins.envelope) + id 'me.champeau.jmh' version '0.7.1' } envelopeJar { @@ -21,4 +22,15 @@ dependencies { implementation catalog.jackson.databind runtimeOnly project(':test-utils') + + jmhAnnotationProcessor catalog.lombok } + +jmh { + threads = 1 + iterations = 2 + fork = 1 + warmupIterations = 1 + warmupForks = 0 + resultFormat = 'JSON' +} \ No newline at end of file diff --git a/benchmark/src/jmh/java/net/woggioni/wson/benchmark/WsonParserBenchmark.java b/benchmark/src/jmh/java/net/woggioni/wson/benchmark/WsonParserBenchmark.java new file mode 100644 index 0000000..2c31927 --- /dev/null +++ b/benchmark/src/jmh/java/net/woggioni/wson/benchmark/WsonParserBenchmark.java @@ -0,0 +1,135 @@ +package net.woggioni.wson.benchmark; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.SneakyThrows; +import net.woggioni.wson.antlr.JSONLexer; +import net.woggioni.wson.antlr.JSONListenerImpl; +import net.woggioni.wson.serialization.binary.JBONParser; +import net.woggioni.wson.serialization.json.JSONParser; +import net.woggioni.wson.value.ObjectValue; +import net.woggioni.wson.xface.Value; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.tree.ParseTreeWalker; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.tukaani.xz.XZInputStream; + +import java.io.BufferedInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.concurrent.TimeUnit; + +public class WsonParserBenchmark { + + @State(Scope.Benchmark) + public static class ExecutionPlan { + + @SneakyThrows + public InputStream hugeTestData() { + return new XZInputStream(Main.class.getResourceAsStream("/citylots.json.xz")); + } + + @SneakyThrows + public InputStream hugeBinaryTestData() { + return new XZInputStream(new BufferedInputStream(WsonParserBenchmark.class.getResourceAsStream("/citylots.jbon.xz"))); + } + + public InputStream smallTestData() { + return new BufferedInputStream(WsonParserBenchmark.class.getResourceAsStream("/wordpress.json")); + } + + public InputStream smallBinaryTestData() { + return new BufferedInputStream(WsonParserBenchmark.class.getResourceAsStream("/wordpress.jbon")); + } + } + + private static Value.Configuration buildConfiguration() { + return Value.Configuration.builder() + .objectValueImplementation(ObjectValue.Implementation.ArrayList) + .build(); + } + + @SneakyThrows + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MICROSECONDS) + public void antlr(ExecutionPlan plan) { + try (InputStream is = plan.smallTestData()) { + CharStream inputStream = CharStreams.fromReader(new InputStreamReader(is)); + JSONLexer lexer = new JSONLexer(inputStream); + CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); + net.woggioni.wson.antlr.JSONParser parser = new net.woggioni.wson.antlr.JSONParser(commonTokenStream); + JSONListenerImpl listener = new JSONListenerImpl(); + ParseTreeWalker walker = new ParseTreeWalker(); + walker.walk(listener, parser.json()); + } + } + + @SneakyThrows + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MICROSECONDS) + public void worthJson(ExecutionPlan plan) { + try (InputStream is = plan.smallTestData()) { + new JSONParser(buildConfiguration()).parse(is); + } + } + + @SneakyThrows + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MICROSECONDS) + public void worthJbon(ExecutionPlan plan) { + try (InputStream is = plan.smallBinaryTestData()) { + new JBONParser(buildConfiguration()).parse(is); + } + } + + @SneakyThrows + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.SECONDS) + public void worthJbonHuge(ExecutionPlan plan) { + try (InputStream is = plan.hugeBinaryTestData()) { + new JBONParser(buildConfiguration()).parse(is); + } + } + + @SneakyThrows + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.SECONDS) + public void worthHuge(ExecutionPlan plan) { + try (InputStream is = plan.hugeTestData()) { + new JSONParser(buildConfiguration()).parse(is); + } + } + + @SneakyThrows + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MICROSECONDS) + public void jackson(ExecutionPlan plan) { + try (InputStream is = plan.smallTestData()) { + ObjectMapper om = new ObjectMapper(); + om.readTree(new InputStreamReader(is)); + } + } + + @SneakyThrows + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.SECONDS) + public void jacksonHuge(ExecutionPlan plan) { + try (InputStream is = plan.hugeTestData()) { + ObjectMapper om = new ObjectMapper(); + om.readTree(new InputStreamReader(is)); + } + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 877b19d..26a2b32 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,10 @@ allprojects { java { modularity.inferModulePath = true + toolchain { + languageVersion = JavaLanguageVersion.of(17) + vendor = JvmVendorSpec.GRAAL_VM + } } lombok { diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 0000000..e69de29 diff --git a/gradle.properties b/gradle.properties index 5ed8463..1312efa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -wson.version = 2023.10.01 +wson.version = 2023.11.01 -lys.version = 2023.10.01 +lys.version = 2023.10.31 diff --git a/settings.gradle b/settings.gradle index 34886e8..0759d5c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -7,6 +7,9 @@ pluginManagement { includeGroup 'net.woggioni.gradle.lombok' includeGroup 'net.woggioni.gradle.envelope' includeGroup 'net.woggioni.gradle.multi-release-jar' + includeGroup 'net.woggioni.gradle.graalvm.native-image' + includeGroup 'net.woggioni.gradle.graalvm.jlink' + includeGroup 'net.woggioni.gradle.sambal' } } gradlePluginPortal() @@ -38,7 +41,6 @@ def includeDirs = [ 'wson-cli', 'wson-w3c-json', 'wcfg', - 'wcfg-cli' ] includeDirs.each { diff --git a/test-utils/src/main/resources/wordpress.jbon b/test-utils/src/main/resources/wordpress.jbon new file mode 100644 index 0000000..6c29371 --- /dev/null +++ b/test-utils/src/main/resources/wordpress.jbon @@ -0,0 +1,229 @@ +²v _linksh +about°_href?https://www.sitepoint.com/wp-json/wp/v2/types/post author°`embeddablehref@https://www.sitepoint.com/wp-json/wp/v2/users/72596collection°_href:https://www.sitepoint.com/wp-json/wp/v2/posts curies°ahref$https://api.w.org/{rel}namewptemplatedreplies°`embeddablehrefIhttps://www.sitepoint.com/wp-json/wp/v2/comments?post=168697self°_hrefAhttps://www.sitepoint.com/wp-json/wp/v2/posts/168697version-history°_hrefKhttps://www.sitepoint.com/wp-json/wp/v2/posts/168697/revisionswp:attachment°_hrefHhttps://www.sitepoint.com/wp-json/wp/v2/media?parent=168697 wp:featuredmedia°`embeddablehrefAhttps://www.sitepoint.com/wp-json/wp/v2/media/168703wp:term±aembeddablehrefKhttps://www.sitepoint.com/wp-json/wp/v2/categories?post=168697taxonomycategoryaembeddablehrefEhttps://www.sitepoint.com/wp-json/wp/v2/tags?post=168697taxonomypost_tag author¨îcategories±À[²[comment_statusopencontent`protectedrendered]€—

This article was created in partnership with BAWMedia. Thank you for supporting the partners who make SitePoint possible.

+

A well-designed website can serve as a powerful marketing tool. These days, creating one for a small business is not distressing or expensive at all. However, it was just a few short years ago.

+

Today, you can take advantage of the features provided by the best WordPress themes. There are special themes for small business-oriented websites. It's not difficult to find a website-building theme that matches a specific business. This can be a startup, a service provider, or some other venture.

+

You undoubtedly want nothing but the best business theme, right? Check out those described below. Each possesses functional designs loaded with amazing features. They will help you create a thoroughly engaging website to promote a business.

+

1. Be Theme

+

+

We’ll start with Be Theme, a responsive, multipurpose WordPress theme that takes every small business need into account with its more than 370 pre-built websites. There's a multiplicity of small business WordPress themes in this pre-built website collection ? each one embedded with the functionality you need to establish an effective online presence, and fully customizable to meet your business and marketing needs.

+

The range of business niches covered is impressive, With more pre-built websites being added every month it's destined to become even more so. Web designers like Be Theme because it allows them to create a website for most small business types in as little as four hours.

+

Clients appreciate the rapid turnaround they receive and the ease in which changes or additions they have in mind can be accommodated.

+

Be Theme, one of the best WordPress themes for small business websites is a ThemeForest top 5 best seller whose core features include easy to work with page-building tools, a multiplicity of design features and options, and great support.

+

2. Astra

+

+

Astra is fast, fully customizable, and one of the best WordPress themes for business websites as well as for blogs and personal portfolios. Built with SEO in mind, Astra is responsive and WooCommerce ready ? mandatory features in today's online business environment. Its capabilities are easily extendible with premium addons and Astra can be used with most of the popular page builders. This free WP-based theme is definitely worth considering.

+

3. The100

+

+

A theme selected for WordPress for small businesses can be free or it can be a premium theme requiring an expenditure on your behalf. There are several excellent free themes on the market, and one of them is The100. While it is advertised as having premium-like features, bear in mind that free themes like this one generally can't compete with premium themes. Nevertheless, The100 is an easy-to-use WP theme that features a multiplicity of layouts and plenty of customization options.

+

4. Uncode ? Creative Multiuse WordPress Theme

+

+

Uncode has proven to be one of the best WordPress themes for business websites. It's a multipurpose theme featuring 30+ homepage concepts designed to get designers and their clients off to a fast start on any small business website. Features include an enhanced version of the popular Visual Composer page builder, and an Adaptive Images System that enables mobile users to see what you want and expect them to see.

+

5. Houzez ? Highly Customizable Real Estate WordPress Theme

+

+

Some WordPress themes are created with a specific purpose in mind. Houzez is a specialty theme offering the features and functionality realtors and real estate agencies look for to promote their businesses and their marketability. Houzez' features include advanced property search filters, IDX systems, property management functionality, and solid customer support.

+

6. TheGem ? Creative Multi-Purpose High-Performance WordPress Theme

+

+

TheGem is without doubt one of the best WordPress business themes on the market. Its users like working with the trendy design concepts the authors have presented based on their analysis of current UX trends. Visual Composer is TheGems' page builder, and a judiciously selected set of plugins gives the web designer the flexibility to satisfy any small business's needs. The package includes a ready-to-go online fashion store.

+

7. Cesis ? Responsive Multi-Purpose WordPress Theme

+

+

When you're searching among the best WordPress themes for small business websites, Cesis is definitely worth a closer look. Its easy-to-use interface combined with a host of design elements and options allows you to build virtually anything you want. This is an important attribute when working with small businesses and startups, each having their unique business model and branding style.

+

8. Pofo – Creative Portfolio and Blog WordPress Theme

+

+

Web designers in need of small business WordPress themes include those whose clients represent creative teams and agencies as well as individual artists. Pofo is an ideal choice with its portfolio, eCommerce and blog features, bundled plugins, and more than 150 pre-built design elements. This premium theme's package also includes a nice assortment of home pages and more than 200 demo pages. Pofo is fully responsive, visually stunning, highly flexible, SEO and loading speed optimized.

+

Conclusion

+

Did you like this selection of the best WordPress themes for small business websites? It provides you with a wide range of options and merits close and careful study.

+

You really can't make a bad choice. With a little extra effort, you should be able to walk away with a perfect WordPress theme. It can be ideal for creating a certain small business website you have in mind. Likewise, it can help you create a range of websites for small businesses.

+date 2018-09-06T09:30:53date_gmt 2018-09-06T16:30:53excerpt`protectedrendered]ÂN

This article was created in partnership with BAWMedia. Thank you for supporting the partners who make SitePoint possible.

+

A well-designed website can serve as a powerful marketing tool. These days, creating one for a small business is not distressing or expensive at all. However, it was just a few short years ago.

+

Today, you can take advantage of the features provided by the best WordPress themes. There are special themes for small business-oriented websites. It's not difficult to find a website-building theme that matches a specific business. This can be a startup, a service provider, or some other venture.

+

You undoubtedly want nothing but the best business theme, right? Check out those described below. Each possesses functional designs loaded with amazing features. They will help you create a thoroughly engaging website to promote a business.

+

1. Be Theme

+

+

We’ll start with Be Theme, a responsive, multipurpose WordPress theme that takes every small business need into account with its more than 370 pre-built websites. There's a multiplicity of small business WordPress themes in this pre-built website collection ? each one embedded with the functionality you need to establish an effective online presence, and fully customizable to meet your business and marketing needs.

+

The range of business niches covered is impressive, With more pre-built websites being added every month it's destined to become even more so. Web designers like Be Theme because it allows them to create a website for most small business types in as little as four hours.

+

Clients appreciate the rapid turnaround they receive and the ease in which changes or additions they have in mind can be accommodated.

+

Be Theme, one of the best WordPress themes for small business websites is a ThemeForest top 5 best seller whose core features include easy to work with page-building tools, a multiplicity of design features and options, and great support.

+

2. Astra

+

+

Astra is fast, fully customizable, and one of the best WordPress themes for business websites as well as for blogs and personal portfolios. Built with SEO in mind, Astra is responsive and WooCommerce ready ? mandatory features in today's online business environment. Its capabilities are easily extendible with premium addons and Astra can be used with most of the popular page builders. This free WP-based theme is definitely worth considering.

+

3. The100

+

+

A theme selected for WordPress for small businesses can be free or it can be a premium theme requiring an expenditure on your behalf. There are several excellent free themes on the market, and one of them is The100. While it is advertised as having premium-like features, bear in mind that free themes like this one generally can't compete with premium themes. Nevertheless, The100 is an easy-to-use WP theme that features a multiplicity of layouts and plenty of customization options.

+

4. Uncode ? Creative Multiuse WordPress Theme

+

+

Uncode has proven to be one of the best WordPress themes for business websites. It's a multipurpose theme featuring 30+ homepage concepts designed to get designers and their clients off to a fast start on any small business website. Features include an enhanced version of the popular Visual Composer page builder, and an Adaptive Images System that enables mobile users to see what you want and expect them to see.

+featured_mediaþË formatstandardguid_rendered0https://www.sitepoint.com/?p=168697idòËlink]¤https://www.sitepoint.com/the-8-best-wordpress-themes-for-small-business-websites/meta¯modified 2018-09-04T21:31:02modified_gmt 2018-09-05T04:31:02ping_statusclosedslugDthe-8-best-wordpress-themes-for-small-business-websites statuspublish stickytags² ¶¢•´btemplate +title_renderedDThe 8 Best WordPress Themes for Small Business Websitestypepostv _linksh +about°_href?https://www.sitepoint.com/wp-json/wp/v2/types/post author°`embeddablehref@https://www.sitepoint.com/wp-json/wp/v2/users/72676collection°_href:https://www.sitepoint.com/wp-json/wp/v2/posts curies°ahref$https://api.w.org/{rel}namewptemplatedreplies°`embeddablehrefIhttps://www.sitepoint.com/wp-json/wp/v2/comments?post=168397self°_hrefAhttps://www.sitepoint.com/wp-json/wp/v2/posts/168397version-history°_hrefKhttps://www.sitepoint.com/wp-json/wp/v2/posts/168397/revisionswp:attachment°_hrefHhttps://www.sitepoint.com/wp-json/wp/v2/media?parent=168397 wp:featuredmedia°`embeddablehrefAhttps://www.sitepoint.com/wp-json/wp/v2/media/168458wp:term±aembeddablehrefKhttps://www.sitepoint.com/wp-json/wp/v2/categories?post=168397taxonomycategoryaembeddablehrefEhttps://www.sitepoint.com/wp-json/wp/v2/tags?post=168397taxonomypost_tag authorÈïcategories°Ìcomment_statusopencontent`protectedrendered]ÐÃ

This article was originally published on MongoDB. Thank you for supporting the partners who make SitePoint possible.

+

You can build your online, operational workloads atop MongoDB and still respond to events in real time by kicking off Amazon Kinesis stream processing actions, using MongoDB Stitch Triggers.

+

Let?s look at an example scenario in which a stream of data is being generated as a result of actions users take on a website. We?ll durably store the data and simultaneously feed a Kinesis process to do streaming analytics on something like cart abandonment, product recommendations, or even credit card fraud detection.

+

We?ll do this by setting up a Stitch Trigger. When relevant data updates are made in MongoDB, the trigger will use a Stitch Function to call out to AWS Kinesis, as you can see in this architecture diagram:

+

+

What you?ll need to follow along

+
    +
  1. An Atlas instance
    +If you don?t already have an application running on Atlas, you can follow our getting started with Atlas guide here. In this example, we?ll be using a database called streamdata, with a collection called clickdata where we?re writing data from our web-based e-commerce application.
  2. +
  3. An AWS account and a Kinesis stream
    +In this example, we?ll use a Kinesis stream to send data downstream to additional applications such as Kinesis Analytics. This is the stream we want to feed our updates into.
  4. +
  5. A Stitch application
    +If you don?t already have a Stitch application, log into Atlas, and click Stitch Apps from the navigation on the left, then click Create New Application.
  6. +
+

Create a Collection

+

The first step is to create a database and collection from the Stitch application console. Click Rules from the left navigation menu and click the Add Collection button. Type streamdata for the database and clickdata for the collection name. Select the template labeled Users can only read and write their own data and provide a field name where we?ll specify the user id.

+

Figure 2. Create a collection

+

Configuring Stitch to Talk to AWS

+

Stitch lets you configure Services to interact with external services such as AWS Kinesis. Choose Services from the navigation on the left, and click the Add a Service button, select the AWS service and set AWS Access Key ID, and Secret Access Key.

+

Figure 3. Service Configuration in Stitch

+

Services use Rules to specify what aspect of a service Stitch can use, and how. Add a rule which will enable that service to communicate with Kinesis by clicking the button labeled NEW RULE. Name the rule ?kinesis? as we?ll be using this specific rule to enable communication with AWS Kinesis. In the section marked Action, select the API labeled Kinesis and select All Actions.

+

Figure 4. Add a rule to enable integration with Kinesis

+

Write a Function that Streams Documents into Kinesis

+

Now that we have a working AWS service, we can use it to put records into a Kinesis stream. The way we do that in Stitch is with Functions. Let?s set up a putKinesisRecord function.

+

Select Functions from the left-hand menu, and click Create New Function. Provide a name for the function and paste the following in the body of the function.

+

Figure 5. Example Function - putKinesisRecord

+
exports = function(event){
+ const awsService = context.services.get('aws');
+try{
+   awsService.kinesis().PutRecord({
+     Data: JSON.stringify(event.fullDocument),
+     StreamName: "stitchStream",
+     PartitionKey: "1"
+      }).then(function(response) {
+        return response;
+      });
+}
+catch(error){
+  console.log(JSON.parse(error));
+}
+};
+
+

Test Out the Function

+

Let?s make sure everything is working by calling that function manually. From the Function Editor, Click Console to view the interactive javascript console for Stitch.

+

+

Functions called from Triggers require an event. To test execution of our function, we?ll need to pass a dummy event to the function. Creating variables from the console in Stitch is simple. Simply set the value of the variable to a JSON document. For our simple example, use the following:

+
event = {
+   "operationType": "replace",
+   "fullDocument": {
+       "color": "black",
+       "inventory": {
+           "$numberInt": "1"
+       },
+       "overview": "test document",
+       "price": {
+           "$numberDecimal": "123"
+       },
+       "type": "backpack"
+   },
+   "ns": {
+       "db": "streamdata",
+       "coll": "clickdata"
+   }
+}
+exports(event);
+
+

Paste the above into the console and click the button labeled Run Function As. Select a user and the function will execute.

+

Ta-da!

+

Putting It Together with Stitch Triggers

+

We?ve got our MongoDB collection living in Atlas, receiving events from our web app. We?ve got our Kinesis stream ready for data. We?ve got a Stitch Function that can put data into a Kinesis stream.

+

Configuring Stitch Triggers is so simple it?s almost anticlimactic. Click Triggers from the left navigation, name your trigger, provide the database and collection context, and select the database events Stitch will react to with execution of a function.

+

For the database and collection, use the names from step one. Now we?ll set the operations we want to watch with our trigger. (Some triggers might care about all of them ? inserts, updates, deletes, and replacements ? while others can be more efficient because they logically can only matter for some of those.) In our case, we?re going to watch for insert, update and replace operations.

+

Now we specify our putKinesisRecord function as the linked function, and we?re done.

+

Figure 6. Trigger Configuration in Stitch

+

As part of trigger execution, Stitch will forward details associated with the trigger event, including the full document involved in the event (i.e. the newly inserted, updated, or deleted document from the collection.) This is where we can evaluate some condition or attribute of the incoming document and decide whether or not to put the record onto a stream.

+

Test the Trigger!

+

Amazon provides a dashboard which will enable you to view details associated with the data coming into your stream.

+

Figure 7. Kinesis Stream Monitoring

+

As you execute the function from within Stitch, you?ll begin to see the data entering the Kinesis stream.

+

Building More Functionality

+

So far our trigger is pretty basic ? it watches a collection and when any updates or inserts happen, it feeds the entire document to our Kinesis stream. From here we can build out some more intelligent functionality. To wrap up this post, let?s look at what we can do with the data once it?s been durably stored in MongoDB and placed into a stream.

+

Once the record is in the Kinesis Stream you can configure additional services downstream to act on the data. A common use case incorporates Amazon Kinesis Data Analytics to perform analytics on the streaming data. Amazon Kinesis Data Analytics offers pre-configured templates to accomplish things like anomaly detection, simple alerts, aggregations, and more.

+

For example, our stream of data will contain orders resulting from purchases. These orders may originate from point-of-sale systems, as well as from our web-based e-commerce application. Kinesis Analytics can be leveraged to create applications that process the incoming stream of data. For our example, we could build a machine learning algorithm to detect anomalies in the data or create a product performance leaderboard from a sliding, or tumbling window of data from our stream.

+

Figure 8. Amazon Data Analytics - Anomaly Detection Example

+

Wrapping Up

+

Now you can connect MongoDB to Kinesis. From here, you?re able to leverage any one of the many services offered from Amazon Web Services to build on your application. In our next article in the series, we?ll focus on getting the data back from Kinesis into MongoDB. In the meantime, let us know what you?re building with Atlas, Stitch, and Kinesis!

+

Resources

+

MongoDB Atlas

+ +

MongoDB Stitch

+ +

Amazon Kinesis

+ +date 2018-09-06T09:30:31date_gmt 2018-09-06T16:30:31excerpt`protectedrendered]¸Y

This article was originally published on MongoDB. Thank you for supporting the partners who make SitePoint possible.

+

You can build your online, operational workloads atop MongoDB and still respond to events in real time by kicking off Amazon Kinesis stream processing actions, using MongoDB Stitch Triggers.

+

Let?s look at an example scenario in which a stream of data is being generated as a result of actions users take on a website. We?ll durably store the data and simultaneously feed a Kinesis process to do streaming analytics on something like cart abandonment, product recommendations, or even credit card fraud detection.

+

We?ll do this by setting up a Stitch Trigger. When relevant data updates are made in MongoDB, the trigger will use a Stitch Function to call out to AWS Kinesis, as you can see in this architecture diagram:

+

+

What you?ll need to follow along

+
    +
  1. An Atlas instance
    +If you don?t already have an application running on Atlas, you can follow our getting started with Atlas guide here. In this example, we?ll be using a database called streamdata, with a collection called clickdata where we?re writing data from our web-based e-commerce application.
  2. +
  3. An AWS account and a Kinesis stream
    +In this example, we?ll use a Kinesis stream to send data downstream to additional applications such as Kinesis Analytics. This is the stream we want to feed our updates into.
  4. +
  5. A Stitch application
    +If you don?t already have a Stitch application, log into Atlas, and click Stitch Apps from the navigation on the left, then click Create New Application.
  6. +
+

Create a Collection

+

The first step is to create a database and collection from the Stitch application console. Click Rules from the left navigation menu and click the Add Collection button. Type streamdata for the database and clickdata for the collection name. Select the template labeled Users can only read and write their own data and provide a field name where we?ll specify the user id.

+

Figure 2. Create a collection

+

Configuring Stitch to Talk to AWS

+

Stitch lets you configure Services to interact with external services such as AWS Kinesis. Choose Services from the navigation on the left, and click the Add a Service button, select the AWS service and set AWS Access Key ID, and Secret Access Key.

+

Figure 3. Service Configuration in Stitch

+

Services use Rules to specify what aspect of a service Stitch can use, and how. Add a rule which will enable that service to communicate with Kinesis by clicking the button labeled NEW RULE. Name the rule ?kinesis? as we?ll be using this specific rule to enable communication with AWS Kinesis. In the section marked Action, select the API labeled Kinesis and select All Actions.

+

Figure 4. Add a rule to enable integration with Kinesis

+

Write a Function that Streams Documents into Kinesis

+

Now that we have a working AWS service, we can use it to put records into a Kinesis stream. The way we do that in Stitch is with Functions. Let?s set up a putKinesisRecord function.

+

Select Functions from the left-hand menu, and click Create New Function. Provide a name for the function and paste the following in the body of the function.

+

Figure 5. Example Function - putKinesisRecord

+
exports = function(event){
+ const awsService = context.services.get('aws');
+try{
+   awsService.kinesis().PutRecord({
+     Data: JSON.stringify(event.fullDocument),
+     StreamName: "stitchStream",
+     PartitionKey: "1"
+      }).then(function(response) {
+        return response;
+      });
+}
+catch(error){
+  console.log(JSON.parse(error));
+}
+};
+
+

Test Out the Function

+

Let?s make sure everything is working by calling that function manually. From the Function Editor, Click Console to view the interactive javascript console for Stitch.

+featured_media”È formatstandardguid_rendered0https://www.sitepoint.com/?p=168397idšÇlink]Âhttps://www.sitepoint.com/integrating-mongodb-and-amazon-kinesis-for-intelligent-durable-streams/meta¯modified 2018-09-04T00:20:42modified_gmt 2018-09-04T07:20:42ping_statusclosedslugSintegrating-mongodb-and-amazon-kinesis-for-intelligent-durable-streams statuspublish stickytags²¢•þ´btemplate +title_renderedTIntegrating MongoDB and Amazon Kinesis for Intelligent, Durable Streamstypepostv _linksh +about°_href?https://www.sitepoint.com/wp-json/wp/v2/types/post author°`embeddablehref@https://www.sitepoint.com/wp-json/wp/v2/users/72596collection°_href:https://www.sitepoint.com/wp-json/wp/v2/posts curies°ahref$https://api.w.org/{rel}namewptemplatedreplies°`embeddablehrefIhttps://www.sitepoint.com/wp-json/wp/v2/comments?post=167869self°_hrefAhttps://www.sitepoint.com/wp-json/wp/v2/posts/167869version-history°_hrefKhttps://www.sitepoint.com/wp-json/wp/v2/posts/167869/revisionswp:attachment°_hrefHhttps://www.sitepoint.com/wp-json/wp/v2/media?parent=167869 wp:featuredmedia°`embeddablehrefAhttps://www.sitepoint.com/wp-json/wp/v2/media/167879wp:term±aembeddablehrefKhttps://www.sitepoint.com/wp-json/wp/v2/categories?post=167869taxonomycategoryaembeddablehrefEhttps://www.sitepoint.com/wp-json/wp/v2/tags?post=167869taxonomypost_tag author¨îcategories±˜Ìcomment_statusopencontent`protectedrendered]â

+

This article was originally published on Alibaba Cloud. Thank you for supporting the partners who make SitePoint possible.

+

Think you got a better tip for making the best use of Alibaba Cloud services? Tell us about it and go in for your chance to win a Macbook Pro (plus other cool stuff). Find out more here.

+

Gain an introduction to ApsaraDB for RDS, a cloud-based relational database product provided by Alibaba Cloud. In this webinar you will watch over the shoulder of a Solution Architect and Trainer, as he covers the basic concepts and features of ApsaraDB for RDS including:

+ +

This webinar is ideally suited for database engineers and beginners to the Alibaba Cloud product suite.

+date 2018-09-05T09:30:47date_gmt 2018-09-05T16:30:47excerpt`protectedrendered]ž

+

This article was originally published on Alibaba Cloud. Thank you for supporting the partners who make SitePoint possible.

+

Gain an introduction to ApsaraDB for RDS, a cloud-based relational database product provided by Alibaba Cloud. In this webinar you will watch over the shoulder of a Solution Architect and Trainer, as he covers the basic concepts and features of ApsaraDB for RDS including:

+ +

This webinar is ideally suited for database engineers and beginners to the Alibaba Cloud product suite.

+featured_mediaŽ¿ formatstandardguid_rendered0https://www.sitepoint.com/?p=167869idú¾link]¬https://www.sitepoint.com/how-to-set-up-a-secure-relational-database-on-alibaba-cloud/meta¯modified 2018-09-06T01:21:52modified_gmt 2018-09-06T08:21:52ping_statusclosedslugHhow-to-set-up-a-secure-relational-database-on-alibaba-cloud statuspublish stickytags±¦·¢•template +title_renderedHHow to Set up a Secure Relational Database on Alibaba Cloudtypepost \ No newline at end of file diff --git a/wcfg-cli/build.gradle b/wcfg-cli/build.gradle deleted file mode 100644 index e01474d..0000000 --- a/wcfg-cli/build.gradle +++ /dev/null @@ -1,12 +0,0 @@ -plugins { - alias(catalog.plugins.envelope) -} - -dependencies { - implementation catalog.picocli - implementation project(':wcfg') -} - -envelopeJar { - mainClass = 'net.woggioni.wson.wcfg.cli.Main' -} \ No newline at end of file diff --git a/wcfg-cli/src/main/java/module-info.java b/wcfg-cli/src/main/java/module-info.java deleted file mode 100644 index f44ea64..0000000 --- a/wcfg-cli/src/main/java/module-info.java +++ /dev/null @@ -1,3 +0,0 @@ -module net.woggioni.wson.wcfg.cli { - requires net.woggioni.wson.wcfg; -} \ No newline at end of file diff --git a/wcfg-cli/src/main/java/net/woggioni/wson/wcfg/cli/Main.java b/wcfg-cli/src/main/java/net/woggioni/wson/wcfg/cli/Main.java deleted file mode 100644 index 2ffb8f8..0000000 --- a/wcfg-cli/src/main/java/net/woggioni/wson/wcfg/cli/Main.java +++ /dev/null @@ -1,7 +0,0 @@ -package net.woggioni.wson.wcfg.cli; - -public class Main { - public void main(String[] args) { - System.out.println(); - } -} diff --git a/wcfg/build.gradle b/wcfg/build.gradle index a61be02..f2351fb 100644 --- a/wcfg/build.gradle +++ b/wcfg/build.gradle @@ -2,12 +2,18 @@ plugins { id 'antlr' } +configurations { + api { + exclude group: 'org.antlr', module: 'antlr4' + } +} + dependencies { implementation catalog.jwo implementation rootProject + implementation catalog.antlr.runtime antlr catalog.antlr - antlr catalog.antlr.runtime } generateGrammarSource { diff --git a/wcfg/src/main/antlr/net/woggioni/wson/wcfg/WCFG.g4 b/wcfg/src/main/antlr/net/woggioni/wson/wcfg/WCFG.g4 index 966d1fe..5bd3622 100644 --- a/wcfg/src/main/antlr/net/woggioni/wson/wcfg/WCFG.g4 +++ b/wcfg/src/main/antlr/net/woggioni/wson/wcfg/WCFG.g4 @@ -1,15 +1,19 @@ grammar WCFG; wcfg - : assignment* + : assignment* export? ; +export + : 'export' value ';' + ; + assignment - : IDENTIFIER ':=' (expression | value) ';' + : IDENTIFIER ':=' value ';' ; expression - : value ('<<' value)+ + : (obj | IDENTIFIER) ('<<' (obj | IDENTIFIER))+ ; obj @@ -18,7 +22,7 @@ obj ; pair - : STRING ':' (expression | value) + : STRING ':' value ; array @@ -34,6 +38,7 @@ value | IDENTIFIER | obj | array + | expression ; BOOLEAN diff --git a/wcfg/src/main/java/module-info.java b/wcfg/src/main/java/module-info.java index 0fa128c..c0e0a0e 100644 --- a/wcfg/src/main/java/module-info.java +++ b/wcfg/src/main/java/module-info.java @@ -1,6 +1,6 @@ module net.woggioni.wson.wcfg { requires static lombok; - requires static org.antlr.antlr4.runtime; + requires org.antlr.antlr4.runtime; requires net.woggioni.jwo; requires net.woggioni.wson; diff --git a/wcfg/src/main/java/net/woggioni/wson/wcfg/CompositeObjectValue.java b/wcfg/src/main/java/net/woggioni/wson/wcfg/CompositeObjectValue.java index f079eb3..d938974 100644 --- a/wcfg/src/main/java/net/woggioni/wson/wcfg/CompositeObjectValue.java +++ b/wcfg/src/main/java/net/woggioni/wson/wcfg/CompositeObjectValue.java @@ -1,6 +1,7 @@ package net.woggioni.wson.wcfg; import lombok.RequiredArgsConstructor; +import net.woggioni.jwo.LazyValue; import net.woggioni.wson.traversal.ValueIdentity; import net.woggioni.wson.value.ObjectValue; import net.woggioni.wson.xface.Value; @@ -11,53 +12,55 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import static net.woggioni.jwo.JWO.dynamicCast; - @RequiredArgsConstructor public class CompositeObjectValue implements ObjectValue { - private final List elements; + private final List elements; - private ObjectValue wrapped; + private LazyValue wrapped; - public CompositeObjectValue(List elements, Value.Configuration cfg) { + public CompositeObjectValue(List elements, Value.Configuration cfg) { this.elements = elements; - wrapped = ObjectValue.newInstance(cfg); - List identities = new ArrayList<>(); - for (ObjectValue element : elements) { - CompositeObjectValue compositeObjectValue; - if ((compositeObjectValue = dynamicCast(element, CompositeObjectValue.class)) != null) { - boolean differenceFound = false; - for (int i = 0; i < compositeObjectValue.elements.size(); i++) { - ObjectValue objectValue = compositeObjectValue.elements.get(i); - if (!differenceFound && (i >= identities.size() || !Objects.equals( - identities.get(i), - new ValueIdentity(compositeObjectValue.elements.get(i))))) { - differenceFound = true; - } - if (differenceFound) { - merge(wrapped, objectValue); - identities.add(new ValueIdentity(objectValue)); + this.wrapped = LazyValue.of(() -> { + ObjectValue result = ObjectValue.newInstance(cfg); + List identities = new ArrayList<>(); + for (Value element : elements) { + if (element instanceof CompositeObjectValue compositeObjectValue) { + boolean differenceFound = false; + for (int i = 0; i < compositeObjectValue.elements.size(); i++) { + ObjectValue objectValue = (ObjectValue) compositeObjectValue.elements.get(i); + if (!differenceFound && (i >= identities.size() || !Objects.equals( + identities.get(i), + new ValueIdentity(compositeObjectValue.elements.get(i))))) { + differenceFound = true; + } + if (differenceFound) { + merge(result, objectValue); + identities.add(new ValueIdentity(objectValue)); + } } + } else { + merge(result, (ObjectValue) element); + identities.add(new ValueIdentity(element)); } - } else { - merge(wrapped, element); - identities.add(new ValueIdentity(element)); } - } + return result; + }, LazyValue.ThreadSafetyMode.NONE); } private static void merge(ObjectValue v1, ObjectValue v2) { for (Map.Entry entry : v2) { - Value putResult = v1.getOrPut(entry.getKey(), entry.getValue()); - if (putResult != entry.getValue()) { - if (putResult.type() == Value.Type.OBJECT && entry.getValue().type() == Value.Type.OBJECT) { + String key = entry.getKey(); + Value value2put = entry.getValue(); + Value putResult = v1.getOrPut(key, value2put); + if (putResult != value2put) { + if (putResult.type() == Value.Type.OBJECT && value2put.type() == Value.Type.OBJECT) { ObjectValue ov = ObjectValue.newInstance(); merge(ov, (ObjectValue) putResult); - merge(ov, (ObjectValue) entry.getValue()); - v1.put(entry.getKey(), ov); + merge(ov, (ObjectValue) value2put); + v1.put(key, ov); } else { - v1.put(entry.getKey(), entry.getValue()); + v1.put(key, value2put); } } } @@ -65,27 +68,27 @@ public class CompositeObjectValue implements ObjectValue { @Override public Iterator> iterator() { - return wrapped.iterator(); + return wrapped.get().iterator(); } @Override public Value get(String key) { - return wrapped.get(key); + return wrapped.get().get(key); } @Override public Value getOrDefault(String key, Value defaultValue) { - return wrapped.getOrDefault(key, defaultValue); + return wrapped.get().getOrDefault(key, defaultValue); } @Override public boolean has(String key) { - return wrapped.has(key); + return wrapped.get().has(key); } @Override public int size() { - return wrapped.size(); + return wrapped.get().size(); } @Override diff --git a/wcfg/src/main/java/net/woggioni/wson/wcfg/ErrorHandler.java b/wcfg/src/main/java/net/woggioni/wson/wcfg/ErrorHandler.java new file mode 100644 index 0000000..7821fd1 --- /dev/null +++ b/wcfg/src/main/java/net/woggioni/wson/wcfg/ErrorHandler.java @@ -0,0 +1,18 @@ +package net.woggioni.wson.wcfg; + +import org.antlr.v4.runtime.BaseErrorListener; +import org.antlr.v4.runtime.RecognitionException; +import org.antlr.v4.runtime.Recognizer; + +class ErrorHandler extends BaseErrorListener { + @Override + public void syntaxError(Recognizer recognizer, + Object offendingSymbol, + int line, int charPositionInLine, + String msg, + RecognitionException e) { + throw new SyntaxError(msg, line, charPositionInLine); + } + + +} diff --git a/wcfg/src/main/java/net/woggioni/wson/wcfg/ListenerImpl.java b/wcfg/src/main/java/net/woggioni/wson/wcfg/ListenerImpl.java index 46bd520..2a17d8e 100644 --- a/wcfg/src/main/java/net/woggioni/wson/wcfg/ListenerImpl.java +++ b/wcfg/src/main/java/net/woggioni/wson/wcfg/ListenerImpl.java @@ -1,6 +1,9 @@ package net.woggioni.wson.wcfg; import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import net.woggioni.jwo.JWO; import net.woggioni.wson.value.ArrayValue; import net.woggioni.wson.value.BooleanValue; import net.woggioni.wson.value.FloatValue; @@ -10,27 +13,45 @@ import net.woggioni.wson.value.ObjectValue; import net.woggioni.wson.value.StringValue; import net.woggioni.wson.xface.Value; import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.tree.ErrorNode; import org.antlr.v4.runtime.tree.TerminalNode; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.stream.Collectors; -import static net.woggioni.jwo.JWO.dynamicCast; +import static net.woggioni.jwo.JWO.newThrowable; +import static net.woggioni.jwo.JWO.tail; class ListenerImpl implements WCFGListener { private final Value.Configuration cfg; @Getter - private final Value result; + private Value result; - private final List holders = new ArrayList<>(); + private final Map unresolvedReferences = new TreeMap<>(); private interface StackLevel { Value getValue(); } + @RequiredArgsConstructor + private static class ExportStackLevel implements StackLevel { + + @Setter + private Value value; + + @Override + public Value getValue() { + return value; + } + } + private static class ArrayStackLevel implements StackLevel { private final ArrayValue value = new ArrayValue(); @@ -57,7 +78,7 @@ class ListenerImpl implements WCFGListener { private static class ExpressionStackLevel implements StackLevel { private final Value.Configuration cfg; private ObjectValue value = null; - public List elements = new ArrayList<>(); + public List elements = new ArrayList<>(); public ExpressionStackLevel(Value.Configuration cfg) { this.cfg = cfg; @@ -65,8 +86,19 @@ class ListenerImpl implements WCFGListener { @Override public Value getValue() { - if(value == null) { - value = new CompositeObjectValue(elements, cfg); + if (value == null) { + List objects = elements.stream() + .map(it -> { + if (it instanceof ObjectValue ov) + return ov; + else if (it instanceof ValueHolder vh) { + return (ObjectValue) vh.getDelegate(); + } else { + throw newThrowable(RuntimeException.class, ""); + } + }) + .collect(Collectors.toList()); + value = new CompositeObjectValue(objects, cfg); } return value; } @@ -75,26 +107,37 @@ class ListenerImpl implements WCFGListener { private final List stack = new ArrayList<>(); private void add2Last(Value value) { - StackLevel last = stack.get(stack.size() - 1); + StackLevel last = tail(stack); if (last instanceof ArrayStackLevel asl) { asl.value.add(value); - if(value instanceof ValueHolder holder) { + if (value instanceof ValueHolder holder) { Value arrayValue = asl.getValue(); int index = arrayValue.size() - 1; holder.addDeleter(() -> arrayValue.set(index, holder.getDelegate())); } } else if (last instanceof ObjectStackLevel osl) { String key = osl.currentKey; - osl.currentKey = null; - osl.value.put(key, value); - if(value instanceof ValueHolder holder) { - Value objectValue = osl.getValue(); - holder.addDeleter(() -> objectValue.put(key, holder.getDelegate())); + Value objectValue = osl.getValue(); + objectValue.put(key, value); + if (value instanceof ValueHolder holder) { + holder.addDeleter(() -> { + objectValue.put(key, holder.getDelegate()); + }); } - } else if(last instanceof ExpressionStackLevel esl) { - esl.elements.add((ObjectValue) value); + } else if (last instanceof ExpressionStackLevel esl) { + List values = esl.elements; + int index = values.size(); + values.add(value); + if (value instanceof ValueHolder holder) { + holder.addDeleter(() -> { + values.set(index, holder.getDelegate()); + }); + } + } else if (last instanceof ExportStackLevel esl) { + esl.setValue(value); } } + private static String unquote(String quoted) { return quoted.substring(1, quoted.length() - 1); } @@ -121,6 +164,17 @@ class ListenerImpl implements WCFGListener { @Override public void exitWcfg(WCFGParser.WcfgContext ctx) { stack.clear(); + for (ValueHolder holder : unresolvedReferences.values()) { + if (holder.getDelegate() == null) { + TerminalNode node = holder.getNode(); + throw new ParseError( + "Undeclared identifier '" + node.getText() + "'", + node.getSymbol().getLine(), + node.getSymbol().getCharPositionInLine() + 1 + ); + } + holder.replace(); + } } @Override @@ -128,15 +182,15 @@ class ListenerImpl implements WCFGListener { ObjectStackLevel osl = (ObjectStackLevel) stack.get(0); String key = ctx.IDENTIFIER().getText(); osl.currentKey = key; - ValueHolder holder = new ValueHolder(); - holders.add(holder); - holder.addDeleter(() -> result.put(key, holder.getDelegate())); - result.put(key, holder); + ValueHolder holder = unresolvedReferences.computeIfAbsent(key, it -> new ValueHolder(ctx.IDENTIFIER())); +// holder.addDeleter(() -> holder.setDelegate(result.get(key))); } @Override public void exitAssignment(WCFGParser.AssignmentContext ctx) { ObjectStackLevel osl = (ObjectStackLevel) stack.get(0); + String key = osl.currentKey; + unresolvedReferences.get(key).setDelegate(osl.getValue().get(key)); osl.currentKey = null; } @@ -144,6 +198,16 @@ class ListenerImpl implements WCFGListener { public void enterExpression(WCFGParser.ExpressionContext ctx) { ExpressionStackLevel esl = new ExpressionStackLevel(cfg); stack.add(esl); + for(TerminalNode node : ctx.IDENTIFIER()) { + String key = node.getSymbol().getText(); + ValueHolder holder = unresolvedReferences + .computeIfAbsent(key, k -> new ValueHolder(node)); + int index = esl.elements.size(); + esl.elements.add(holder); + holder.addDeleter(() -> { + esl.elements.set(index, holder.getDelegate()); + }); + } } @Override @@ -200,16 +264,25 @@ class ListenerImpl implements WCFGListener { } else { add2Last(new FloatValue(Double.parseDouble(text))); } - } else if(ctx.IDENTIFIER() != null) { + } else if (ctx.IDENTIFIER() != null) { String name = ctx.IDENTIFIER().getText(); - Value referredValue = result.getOrDefault(name, null); - if(referredValue == null) { - throw new ParseError( - "Undeclared identifier '" + name + "'", - ctx.start.getLine(), - ctx.start.getCharPositionInLine() + 1 - ); - } + ValueHolder referredValue = unresolvedReferences.computeIfAbsent(name, + it -> new ValueHolder(ctx.IDENTIFIER()) + ); +// referredValue.addError(() -> new ParseError( +// "Undeclared identifier '" + name + "'", +// ctx.start.getLine(), +// ctx.start.getCharPositionInLine() + 1 +// ) +// ); +// Value referredValue = result.getOrDefault(name, null); +// if(referredValue == null) { +// throw new ParseError( +// "Undeclared identifier '" + name + "'", +// ctx.start.getLine(), +// ctx.start.getCharPositionInLine() + 1 +// ); +// } add2Last(referredValue); } } @@ -238,9 +311,19 @@ class ListenerImpl implements WCFGListener { } - public void replaceHolders() { - for(ValueHolder holder : holders) { - holder.replace(); + @Override + public void enterExport(WCFGParser.ExportContext ctx) { + ExportStackLevel esl = new ExportStackLevel(); + stack.add(esl); + } + + @Override + public void exitExport(WCFGParser.ExportContext ctx) { + result = pop().getValue(); + if (result instanceof ValueHolder holder) { + holder.addDeleter(() -> { + result = holder.getDelegate(); + }); } } } diff --git a/wcfg/src/main/java/net/woggioni/wson/wcfg/SyntaxError.java b/wcfg/src/main/java/net/woggioni/wson/wcfg/SyntaxError.java new file mode 100644 index 0000000..432978b --- /dev/null +++ b/wcfg/src/main/java/net/woggioni/wson/wcfg/SyntaxError.java @@ -0,0 +1,15 @@ +package net.woggioni.wson.wcfg; + +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public class SyntaxError extends RuntimeException { + public SyntaxError(String message, int line, int column) { + super(message + String.format(" at %d:%d", line, column)); + } + @Override + public String getMessage() { + return super.getMessage(); + } +} diff --git a/wcfg/src/main/java/net/woggioni/wson/wcfg/ValueHolder.java b/wcfg/src/main/java/net/woggioni/wson/wcfg/ValueHolder.java index f372f69..f7e5cd1 100644 --- a/wcfg/src/main/java/net/woggioni/wson/wcfg/ValueHolder.java +++ b/wcfg/src/main/java/net/woggioni/wson/wcfg/ValueHolder.java @@ -1,23 +1,32 @@ package net.woggioni.wson.wcfg; import lombok.Getter; +import lombok.RequiredArgsConstructor; import lombok.Setter; +import lombok.SneakyThrows; import net.woggioni.wson.xface.Value; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.tree.TerminalNode; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.function.Supplier; +@RequiredArgsConstructor class ValueHolder implements Value { - private List deleters = new ArrayList<>(); + @Getter + private final TerminalNode node; + private List deleters = new ArrayList<>(); public void addDeleter(Runnable runnable) { deleters.add(runnable); } @Setter @Getter - private Value delegate = Value.Null; + private Value delegate = null; + @Override public Type type() { return delegate.type(); diff --git a/wcfg/src/main/java/net/woggioni/wson/wcfg/WConfig.java b/wcfg/src/main/java/net/woggioni/wson/wcfg/WConfig.java index a82b2ae..1ad2410 100644 --- a/wcfg/src/main/java/net/woggioni/wson/wcfg/WConfig.java +++ b/wcfg/src/main/java/net/woggioni/wson/wcfg/WConfig.java @@ -18,12 +18,14 @@ public class WConfig { private final Value value; @SneakyThrows - public WConfig(Reader reader, Value.Configuration cfg) { + private WConfig(Reader reader, Value.Configuration cfg) { this.cfg = cfg; CodePointCharStream inputStream = CharStreams.fromReader(reader); WCFGLexer lexer = new WCFGLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); WCFGParser parser = new WCFGParser(commonTokenStream); + parser.removeErrorListeners(); + parser.addErrorListener(new ErrorHandler()); ListenerImpl listener = new ListenerImpl(cfg); ParseTreeWalker walker = new ParseTreeWalker(); walker.walk(listener, parser.wcfg()); @@ -44,4 +46,8 @@ public class WConfig { public Value whole() { return value; } + + public static WConfig parse(Reader reader, Value.Configuration cfg) { + return new WConfig(reader, cfg); + } } diff --git a/wcfg/src/test/java/net/woggioni/wson/wcfg/ParseTest.java b/wcfg/src/test/java/net/woggioni/wson/wcfg/ParseTest.java index 2b84c9f..248e009 100644 --- a/wcfg/src/test/java/net/woggioni/wson/wcfg/ParseTest.java +++ b/wcfg/src/test/java/net/woggioni/wson/wcfg/ParseTest.java @@ -2,6 +2,7 @@ package net.woggioni.wson.wcfg; import lombok.SneakyThrows; import net.woggioni.wson.serialization.json.JSONDumper; +import net.woggioni.wson.value.ObjectValue; import net.woggioni.wson.xface.Value; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CodePointCharStream; @@ -23,24 +24,21 @@ public class ParseTest { @SneakyThrows @ParameterizedTest @ValueSource(strings = { -// "build.wcfg", -// "test.wcfg", -// "recursive.wcfg", + "build.wcfg", + "test.wcfg", + "recursive.wcfg", "recursive2.wcfg", + "recursive3.wcfg", }) public void test(String resource) { + Value.Configuration cfg = Value.Configuration.builder() + .objectValueImplementation(ObjectValue.Implementation.HashMap) + .serializeReferences(true) + .build(); try(Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(resource))) { - CodePointCharStream inputStream = CharStreams.fromReader(reader); - WCFGLexer lexer = new WCFGLexer(inputStream); - CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); - WCFGParser parser = new WCFGParser(commonTokenStream); - Value.Configuration cfg = Value.Configuration.builder().serializeReferences(true).build(); - ListenerImpl listener = new ListenerImpl(cfg); - ParseTreeWalker walker = new ParseTreeWalker(); - walker.walk(listener, parser.wcfg()); - listener.replaceHolders(); - Value result = listener.getResult(); - new JSONDumper(cfg).dump(result, System.out); + WConfig wcfg = WConfig.parse(reader, cfg); + new JSONDumper(cfg).dump(wcfg.whole(), System.out); + System.out.println(); } } @@ -49,8 +47,8 @@ public class ParseTest { public void test2() { Value.Configuration cfg = Value.Configuration.builder().serializeReferences(true).build(); try (Reader reader = new InputStreamReader(getClass().getResourceAsStream("/build.wcfg"))) { - WConfig wConfig = new WConfig(reader, cfg); - Value result = wConfig.get("release", "dev"); + WConfig wcfg = WConfig.parse(reader, cfg); + Value result = wcfg.get("release", "dev"); try (OutputStream os = new BufferedOutputStream(new FileOutputStream("/tmp/build.json"))) { new JSONDumper(cfg).dump(result, os); } diff --git a/wcfg/src/test/resources/recursive3.wcfg b/wcfg/src/test/resources/recursive3.wcfg new file mode 100644 index 0000000..21f1c0f --- /dev/null +++ b/wcfg/src/test/resources/recursive3.wcfg @@ -0,0 +1,5 @@ +value := [1, 2, value2, null, false]; + +value2 := [3, 4, value, null, true, value3]; + +value3 := {"key" : "Some string" }; \ No newline at end of file diff --git a/wcfg/src/test/resources/test.wcfg b/wcfg/src/test/resources/test.wcfg index bc2b634..2fd1947 100644 --- a/wcfg/src/test/resources/test.wcfg +++ b/wcfg/src/test/resources/test.wcfg @@ -20,4 +20,6 @@ foobar := { } << { "enabled" : true } -}; \ No newline at end of file +}; + +export default << foobar; \ No newline at end of file diff --git a/wson-cli/build.gradle b/wson-cli/build.gradle index 497ecf8..2c90938 100644 --- a/wson-cli/build.gradle +++ b/wson-cli/build.gradle @@ -1,21 +1,68 @@ plugins { id 'maven-publish' alias catalog.plugins.envelope - alias catalog.plugins.kotlin.jvm + alias catalog.plugins.graalvm.native.image + alias catalog.plugins.sambal } +import net.woggioni.gradle.graalvm.NativeImageTask +import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform + dependencies { - implementation catalog.kotlin.stdlib.jdk8 - implementation catalog.jcommander + implementation catalog.jwo + implementation catalog.picocli implementation catalog.slf4j.simple implementation rootProject + implementation project(':wcfg') } -envelopeJar { - mainClass = 'net.woggioni.wson.cli.MainKt' +application { + mainClass = 'net.woggioni.wson.cli.WsonCli' mainModule = 'net.woggioni.wson.cli' } +compileJava { + options.compilerArgs = [ + "--patch-module", + "net.woggioni.wson.cli=${sourceSets.main.output.asPath}" + ] + options.javaModuleMainClass = 'net.woggioni.wson.cli.MainKt' + options.javaModuleVersion = project.version +} + +configureNativeImage { +// args = [ +// 'wson', +// '-f', +// '../test-utils/src/main/resources/wordpress.json', +// '-t', +// 'jbon', +// '-o', +// '/dev/null' +// ] + + args = [ + 'wcfg', + '-f', + '../wcfg/src/test/resources/build.wcfg', + '-t', + 'jbon', + '-o', + '/dev/null' + ] + + mergeConfiguration = true +} + +Provider nativeImageTaskProvider = tasks.named("nativeImage") { + useMusl = true + buildStaticImage = true +} + +artifacts { + archives nativeImageTaskProvider.map(NativeImageTask.&getOutputFile) +} + publishing { repositories { maven { @@ -25,7 +72,11 @@ publishing { publications { myDistribution(MavenPublication) { artifact envelopeJar + def host = DefaultNativePlatform.host() + artifact( + source: nativeImageTaskProvider, + classifier: host.operatingSystem.name + '-' + host.architecture.name + ) } } } - diff --git a/wson-cli/native-image/jni-config.json b/wson-cli/native-image/jni-config.json new file mode 100644 index 0000000..8b4e417 --- /dev/null +++ b/wson-cli/native-image/jni-config.json @@ -0,0 +1,6 @@ +[ +{ + "name":"java.lang.Boolean", + "methods":[{"name":"getBoolean","parameterTypes":["java.lang.String"] }] +} +] diff --git a/wson-cli/native-image/native-image.properties b/wson-cli/native-image/native-image.properties new file mode 100644 index 0000000..873aee7 --- /dev/null +++ b/wson-cli/native-image/native-image.properties @@ -0,0 +1 @@ +Args=-H:Optimize=3 --initialize-at-run-time=net.woggioni.wson.cli.WsonCli diff --git a/wson-cli/native-image/predefined-classes-config.json b/wson-cli/native-image/predefined-classes-config.json new file mode 100644 index 0000000..0e79b2c --- /dev/null +++ b/wson-cli/native-image/predefined-classes-config.json @@ -0,0 +1,8 @@ +[ + { + "type":"agent-extracted", + "classes":[ + ] + } +] + diff --git a/wson-cli/native-image/proxy-config.json b/wson-cli/native-image/proxy-config.json new file mode 100644 index 0000000..0d4f101 --- /dev/null +++ b/wson-cli/native-image/proxy-config.json @@ -0,0 +1,2 @@ +[ +] diff --git a/wson-cli/native-image/reflect-config.json b/wson-cli/native-image/reflect-config.json new file mode 100644 index 0000000..083a356 --- /dev/null +++ b/wson-cli/native-image/reflect-config.json @@ -0,0 +1,248 @@ +[ +{ + "name":"com.sun.crypto.provider.AESCipher$General", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"com.sun.crypto.provider.ARCFOURCipher", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"com.sun.crypto.provider.ChaCha20Cipher$ChaCha20Poly1305", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"com.sun.crypto.provider.DESCipher", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"com.sun.crypto.provider.DESedeCipher", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"com.sun.crypto.provider.GaloisCounterMode$AESGCM", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"groovy.lang.Closure" +}, +{ + "name":"java.lang.Object", + "allDeclaredFields":true, + "queryAllDeclaredMethods":true +}, +{ + "name":"java.nio.file.Path" +}, +{ + "name":"java.nio.file.Paths", + "methods":[{"name":"get","parameterTypes":["java.lang.String","java.lang.String[]"] }] +}, +{ + "name":"java.security.AlgorithmParametersSpi" +}, +{ + "name":"java.security.KeyStoreSpi" +}, +{ + "name":"java.security.SecureRandomParameters" +}, +{ + "name":"java.sql.Connection" +}, +{ + "name":"java.sql.Driver" +}, +{ + "name":"java.sql.DriverManager", + "methods":[{"name":"getConnection","parameterTypes":["java.lang.String"] }, {"name":"getDriver","parameterTypes":["java.lang.String"] }] +}, +{ + "name":"java.sql.Time", + "methods":[{"name":"","parameterTypes":["long"] }] +}, +{ + "name":"java.sql.Timestamp", + "methods":[{"name":"valueOf","parameterTypes":["java.lang.String"] }] +}, +{ + "name":"java.time.Duration", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"java.time.Instant", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"java.time.LocalDate", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"java.time.LocalDateTime", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"java.time.LocalTime", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"java.time.MonthDay", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"java.time.OffsetDateTime", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"java.time.OffsetTime", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"java.time.Period", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"java.time.Year", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"java.time.YearMonth", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"java.time.ZoneId", + "methods":[{"name":"of","parameterTypes":["java.lang.String"] }] +}, +{ + "name":"java.time.ZoneOffset", + "methods":[{"name":"of","parameterTypes":["java.lang.String"] }] +}, +{ + "name":"java.time.ZonedDateTime", + "methods":[{"name":"parse","parameterTypes":["java.lang.CharSequence"] }] +}, +{ + "name":"javax.security.auth.x500.X500Principal", + "fields":[{"name":"thisX500Name"}], + "methods":[{"name":"","parameterTypes":["sun.security.x509.X500Name"] }] +}, +{ + "name":"net.woggioni.wson.cli.OutputTypeConverter", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"net.woggioni.wson.cli.SerializationTypeConverter", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"net.woggioni.wson.cli.VersionProvider", + "allDeclaredFields":true, + "queryAllDeclaredMethods":true, + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"net.woggioni.wson.cli.WcfgCommand", + "allDeclaredFields":true, + "queryAllDeclaredMethods":true, + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"net.woggioni.wson.cli.WsonCli", + "allDeclaredFields":true, + "queryAllDeclaredMethods":true +}, +{ + "name":"net.woggioni.wson.cli.WsonCommand", + "allDeclaredFields":true, + "queryAllDeclaredMethods":true, + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"sun.security.pkcs12.PKCS12KeyStore", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"sun.security.pkcs12.PKCS12KeyStore$DualFormatPKCS12", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"sun.security.provider.JavaKeyStore$JKS", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"sun.security.provider.NativePRNG", + "methods":[{"name":"","parameterTypes":[] }, {"name":"","parameterTypes":["java.security.SecureRandomParameters"] }] +}, +{ + "name":"sun.security.provider.SHA", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"sun.security.provider.X509Factory", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"sun.security.rsa.RSAKeyFactory$Legacy", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"sun.security.ssl.KeyManagerFactoryImpl$SunX509", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"sun.security.ssl.SSLContextImpl$DefaultSSLContext", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"sun.security.ssl.TrustManagerFactoryImpl$PKIXFactory", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "name":"sun.security.x509.AuthorityInfoAccessExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +}, +{ + "name":"sun.security.x509.AuthorityKeyIdentifierExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +}, +{ + "name":"sun.security.x509.BasicConstraintsExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +}, +{ + "name":"sun.security.x509.CRLDistributionPointsExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +}, +{ + "name":"sun.security.x509.CertificatePoliciesExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +}, +{ + "name":"sun.security.x509.ExtendedKeyUsageExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +}, +{ + "name":"sun.security.x509.IssuerAlternativeNameExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +}, +{ + "name":"sun.security.x509.KeyUsageExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +}, +{ + "name":"sun.security.x509.NetscapeCertTypeExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +}, +{ + "name":"sun.security.x509.PrivateKeyUsageExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +}, +{ + "name":"sun.security.x509.SubjectAlternativeNameExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +}, +{ + "name":"sun.security.x509.SubjectKeyIdentifierExtension", + "methods":[{"name":"","parameterTypes":["java.lang.Boolean","java.lang.Object"] }] +} +] diff --git a/wson-cli/native-image/resource-config.json b/wson-cli/native-image/resource-config.json new file mode 100644 index 0000000..76dc6fd --- /dev/null +++ b/wson-cli/native-image/resource-config.json @@ -0,0 +1,17 @@ +{ + "resources":{ + "includes":[{ + "pattern":"\\QMETA-INF/MANIFEST.MF\\E" + }, { + "pattern":"\\QMETA-INF/services/java.lang.System$LoggerFinder\\E" + }, { + "pattern":"\\QMETA-INF/services/java.net.spi.URLStreamHandlerProvider\\E" + }, { + "pattern":"\\QMETA-INF/services/java.time.zone.ZoneRulesProvider\\E" + }, { + "pattern":"\\QMETA-INF/services/org.slf4j.spi.SLF4JServiceProvider\\E" + }, { + "pattern":"\\Qsimplelogger.properties\\E" + }]}, + "bundles":[] +} diff --git a/wson-cli/native-image/serialization-config.json b/wson-cli/native-image/serialization-config.json new file mode 100644 index 0000000..f3d7e06 --- /dev/null +++ b/wson-cli/native-image/serialization-config.json @@ -0,0 +1,8 @@ +{ + "types":[ + ], + "lambdaCapturingTypes":[ + ], + "proxies":[ + ] +} diff --git a/wson-cli/src/java9/java/module-info.java b/wson-cli/src/java9/java/module-info.java deleted file mode 100644 index e82b73c..0000000 --- a/wson-cli/src/java9/java/module-info.java +++ /dev/null @@ -1,8 +0,0 @@ -module net.woggioni.wson.cli { - requires kotlin.stdlib; - requires kotlin.stdlib.jdk8; - requires net.woggioni.wson; - requires com.beust.jcommander; - - opens net.woggioni.wson.cli to com.beust.jcommander; -} \ No newline at end of file diff --git a/wson-cli/src/java9/java/net/woggioni/wson/cli/Foo.java b/wson-cli/src/java9/java/net/woggioni/wson/cli/Foo.java deleted file mode 100644 index b33c521..0000000 --- a/wson-cli/src/java9/java/net/woggioni/wson/cli/Foo.java +++ /dev/null @@ -1,4 +0,0 @@ -package net.woggioni.wson.cli; - -public class Foo { -} diff --git a/wson-cli/src/main/java/module-info.java b/wson-cli/src/main/java/module-info.java new file mode 100644 index 0000000..8ae0b74 --- /dev/null +++ b/wson-cli/src/main/java/module-info.java @@ -0,0 +1,12 @@ +module net.woggioni.wson.cli { + requires static lombok; + requires net.woggioni.jwo; + requires net.woggioni.wson; + requires net.woggioni.wson.wcfg; + requires org.slf4j; + requires org.slf4j.simple; + requires info.picocli; + exports net.woggioni.wson.cli; + + opens net.woggioni.wson.cli to info.picocli; +} \ No newline at end of file diff --git a/wson-cli/src/main/java/net/woggioni/wson/cli/AbstractVersionProvider.java b/wson-cli/src/main/java/net/woggioni/wson/cli/AbstractVersionProvider.java new file mode 100644 index 0000000..ed598c2 --- /dev/null +++ b/wson-cli/src/main/java/net/woggioni/wson/cli/AbstractVersionProvider.java @@ -0,0 +1,51 @@ +package net.woggioni.wson.cli; + +import lombok.SneakyThrows; +import picocli.CommandLine; + +import java.io.InputStream; +import java.net.URL; +import java.util.Enumeration; +import java.util.Objects; +import java.util.jar.Attributes; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + + +abstract class AbstractVersionProvider implements CommandLine.IVersionProvider { + private String version; + private String vcsHash; + + @SneakyThrows + protected AbstractVersionProvider(String specificationTitle) { + String version = null; + String vcsHash = null; + Enumeration it = getClass().getClassLoader().getResources(JarFile.MANIFEST_NAME); + while (it.hasMoreElements()) { + URL manifestURL = it.nextElement(); + Manifest mf = new Manifest(); + try (InputStream inputStream = manifestURL.openStream()) { + mf.read(inputStream); + } + Attributes mainAttributes = mf.getMainAttributes(); + if (Objects.equals(specificationTitle, mainAttributes.getValue(Attributes.Name.SPECIFICATION_TITLE))) { + version = mainAttributes.getValue(Attributes.Name.SPECIFICATION_VERSION); + vcsHash = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION); + } + } + if (version == null || vcsHash == null) { + throw new RuntimeException("Version information not found in manifest"); + } + this.version = version; + this.vcsHash = vcsHash; + } + + @Override + public String[] getVersion() { + if (version.endsWith("-SNAPSHOT")) { + return new String[]{version, vcsHash}; + } else { + return new String[]{version}; + } + } +} \ No newline at end of file diff --git a/wson-cli/src/main/java/net/woggioni/wson/cli/SerializationFormat.java b/wson-cli/src/main/java/net/woggioni/wson/cli/SerializationFormat.java new file mode 100644 index 0000000..b94e47c --- /dev/null +++ b/wson-cli/src/main/java/net/woggioni/wson/cli/SerializationFormat.java @@ -0,0 +1,34 @@ +package net.woggioni.wson.cli; + +import lombok.RequiredArgsConstructor; + +import java.util.Arrays; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static net.woggioni.jwo.JWO.newThrowable; + +@RequiredArgsConstructor +enum SerializationFormat { + JSON("json"), JBON("jbon"); + + private final String name; + + + public static SerializationFormat parse(String value) { + return Arrays.stream(SerializationFormat.values()) + .filter(sf -> Objects.equals(sf.name, value)) + .findFirst() + .orElseThrow(() -> { + var availableValues = Stream.of( + JSON, + JBON + ).map(SerializationFormat::name).collect(Collectors.joining(", ")); + throw newThrowable(IllegalArgumentException.class, + "Unknown serialization format '%s', possible values are %s", + value, availableValues + ); + }); + } +} diff --git a/wson-cli/src/main/java/net/woggioni/wson/cli/SerializationTypeConverter.java b/wson-cli/src/main/java/net/woggioni/wson/cli/SerializationTypeConverter.java new file mode 100644 index 0000000..c0f398d --- /dev/null +++ b/wson-cli/src/main/java/net/woggioni/wson/cli/SerializationTypeConverter.java @@ -0,0 +1,10 @@ +package net.woggioni.wson.cli; + +import picocli.CommandLine; + +class SerializationTypeConverter implements CommandLine.ITypeConverter { + @Override + public SerializationFormat convert(String value) { + return SerializationFormat.parse(value); + } +} diff --git a/wson-cli/src/main/java/net/woggioni/wson/cli/VersionProvider.java b/wson-cli/src/main/java/net/woggioni/wson/cli/VersionProvider.java new file mode 100644 index 0000000..0fb0cf3 --- /dev/null +++ b/wson-cli/src/main/java/net/woggioni/wson/cli/VersionProvider.java @@ -0,0 +1,7 @@ +package net.woggioni.wson.cli; + +class VersionProvider extends AbstractVersionProvider { + protected VersionProvider() { + super("wson-cli"); + } +} diff --git a/wson-cli/src/main/java/net/woggioni/wson/cli/WcfgCommand.java b/wson-cli/src/main/java/net/woggioni/wson/cli/WcfgCommand.java new file mode 100644 index 0000000..bef4c9b --- /dev/null +++ b/wson-cli/src/main/java/net/woggioni/wson/cli/WcfgCommand.java @@ -0,0 +1,92 @@ +package net.woggioni.wson.cli; + +import lombok.NoArgsConstructor; +import lombok.SneakyThrows; +import net.woggioni.jwo.Fun; +import net.woggioni.wson.serialization.binary.JBONDumper; +import net.woggioni.wson.serialization.json.JSONDumper; +import net.woggioni.wson.value.ObjectValue; +import net.woggioni.wson.wcfg.WConfig; +import net.woggioni.wson.xface.Value; +import picocli.CommandLine; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; + + +@CommandLine.Command( + name = "wcfg", + versionProvider = VersionProvider.class) +public class WcfgCommand implements Runnable { + + @CommandLine.Option( + names = {"-f", "--file"}, + description = {"Name of the input file to parse"} + ) + private Path fileName; + + @CommandLine.Option(names = {"-o", "--output"}, + description = {"Name of the JSON file to generate"}) + private Path output; + + @CommandLine.Option( + names = {"-t", "--type"}, + description = {"Output type"}, + converter = {SerializationTypeConverter.class}) + private SerializationFormat outputType = SerializationFormat.JSON; + + @CommandLine.Option(names = {"-d", "--resolve-refernces"}, description = "Resolve references") + private boolean resolveReferences = false; + + @CommandLine.Option(names = {"-h", "--help"}, usageHelp = true) + private boolean help = false; + + @SneakyThrows + public void run() { + Value.Configuration cfg = Value.Configuration.builder() + .objectValueImplementation(ObjectValue.Implementation.LinkedHashMap) + .serializeReferences(!resolveReferences) + .build(); + InputStream inputStream; + if (fileName != null) { + inputStream = new BufferedInputStream(Files.newInputStream(fileName)); + } else { + inputStream = System.in; + } + + Value result; + try(Reader reader = new InputStreamReader(inputStream)) { + WConfig wcfg = WConfig.parse(reader, cfg); + result = wcfg.whole(); + } + + + OutputStream outputStream = Optional.ofNullable(output) + .map((Fun) Files::newOutputStream) + .map(BufferedOutputStream::new) + .orElse(System.out); + + switch (outputType) { + case JSON: + try (Writer writer = new OutputStreamWriter(outputStream)) { + new JSONDumper(cfg).dump(result, writer); + } + break; + case JBON: + try { + new JBONDumper(cfg).dump(result, outputStream); + } finally { + outputStream.close(); + } + } + } +} diff --git a/wson-cli/src/main/java/net/woggioni/wson/cli/WsonCli.java b/wson-cli/src/main/java/net/woggioni/wson/cli/WsonCli.java new file mode 100644 index 0000000..985b901 --- /dev/null +++ b/wson-cli/src/main/java/net/woggioni/wson/cli/WsonCli.java @@ -0,0 +1,33 @@ +package net.woggioni.wson.cli; + +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import picocli.CommandLine; + +@Slf4j +@CommandLine.Command( + name = "wcfg", + versionProvider = VersionProvider.class, + subcommands = {WsonCommand.class, WcfgCommand.class}) +public class WsonCli implements Runnable { + public static void main(String[] args) { + final var commandLine = new CommandLine(new WsonCli()); + commandLine.setExecutionExceptionHandler((ex, cl, parseResult) -> { + log.error(ex.getMessage(), ex); + return CommandLine.ExitCode.SOFTWARE; + }); + System.exit(commandLine.execute(args)); + } + + @Getter + @CommandLine.Option(names = {"-V", "--version"}, versionHelp = true) + private boolean versionHelp; + + @CommandLine.Spec + private CommandLine.Model.CommandSpec spec; + + @Override + public void run() { + spec.commandLine().usage(System.out); + } +} diff --git a/wson-cli/src/main/java/net/woggioni/wson/cli/WsonCommand.java b/wson-cli/src/main/java/net/woggioni/wson/cli/WsonCommand.java new file mode 100644 index 0000000..28c61ac --- /dev/null +++ b/wson-cli/src/main/java/net/woggioni/wson/cli/WsonCommand.java @@ -0,0 +1,104 @@ +package net.woggioni.wson.cli; + +import lombok.SneakyThrows; +import net.woggioni.jwo.Fun; +import net.woggioni.wson.serialization.binary.JBONDumper; +import net.woggioni.wson.serialization.binary.JBONParser; +import net.woggioni.wson.serialization.json.JSONDumper; +import net.woggioni.wson.serialization.json.JSONParser; +import net.woggioni.wson.xface.Value; +import picocli.CommandLine; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; + +import static net.woggioni.jwo.JWO.newThrowable; + + +@CommandLine.Command( + name = "wson", + versionProvider = VersionProvider.class) +public class WsonCommand implements Runnable { + + @CommandLine.Option( + names = {"-f", "--file"}, + description = {"Name of the input file to parse"} + ) + private Path fileName; + + @CommandLine.Option( + names = {"--input-type"}, + description = {"Input type"}, + converter = {SerializationTypeConverter.class}) + private SerializationFormat inputType = SerializationFormat.JSON; + + @CommandLine.Option(names = {"-o", "--output"}, + description = {"Name of the JSON file to generate"}) + private Path output; + + @CommandLine.Option( + names = {"-t", "--type"}, + description = {"Output type"}, + converter = {SerializationTypeConverter.class}) + private SerializationFormat outputType = SerializationFormat.JSON; + + @CommandLine.Option(names = {"-h", "--help"}, usageHelp = true) + private boolean help = false; + + @SneakyThrows + public void run() { + Value.Configuration cfg = Value.Configuration.builder().serializeReferences(true).build(); + InputStream inputStream; + if (fileName != null) { + inputStream = new BufferedInputStream(Files.newInputStream(fileName)); + } else { + inputStream = System.in; + } + + Value result; + switch (inputType) { + case JSON: + try (Reader reader = new InputStreamReader(inputStream)) { + result = new JSONParser(cfg).parse(reader); + } + break; + case JBON: + try { + result = new JBONParser(cfg).parse(inputStream); + } finally { + inputStream.close(); + } + break; + default: + throw newThrowable(RuntimeException.class, "This sohuld never happen"); + } + + OutputStream outputStream = Optional.ofNullable(output) + .map((Fun) Files::newOutputStream) + .map(BufferedOutputStream::new) + .orElse(System.out); + + switch (outputType) { + case JSON: + try (Writer writer = new OutputStreamWriter(outputStream)) { + new JSONDumper(cfg).dump(result, writer); + } + break; + case JBON: + try { + new JBONDumper(cfg).dump(result, outputStream); + } finally { + outputStream.close(); + } + } + } +} diff --git a/wson-cli/src/main/java9/module-info.java b/wson-cli/src/main/java9/module-info.java deleted file mode 100644 index 7a4bf32..0000000 --- a/wson-cli/src/main/java9/module-info.java +++ /dev/null @@ -1,9 +0,0 @@ -module net.woggioni.wson.cli { - requires kotlin.stdlib; - requires kotlin.stdlib.jdk8; - requires net.woggioni.wson; - requires com.beust.jcommander; - exports net.woggioni.wson.cli; - - opens net.woggioni.wson.cli to com.beust.jcommander; -} \ No newline at end of file diff --git a/wson-cli/src/main/kotlin/net/woggioni/wson/cli/AbstractVersionProvider.kt b/wson-cli/src/main/kotlin/net/woggioni/wson/cli/AbstractVersionProvider.kt new file mode 100644 index 0000000..cd37378 --- /dev/null +++ b/wson-cli/src/main/kotlin/net/woggioni/wson/cli/AbstractVersionProvider.kt @@ -0,0 +1,45 @@ +package net.woggioni.wson.cli + +import java.net.URL +import java.util.Enumeration +import java.util.jar.Attributes +import java.util.jar.JarFile +import java.util.jar.Manifest +import lombok.SneakyThrows +import picocli.CommandLine + + +abstract class AbstractVersionProvider @SneakyThrows protected constructor(specificationTitle: String?) : + CommandLine.IVersionProvider { + private val version: String + private val vcsHash: String + + init { + var version: String? = null + var vcsHash: String? = null + val it: Enumeration = javaClass.classLoader.getResources(JarFile.MANIFEST_NAME) + while (it.hasMoreElements()) { + val manifestURL: URL = it.nextElement() + val mf = Manifest() + manifestURL.openStream().use(mf::read) + val mainAttributes: Attributes = mf.mainAttributes + if (specificationTitle == mainAttributes.getValue(Attributes.Name.SPECIFICATION_TITLE)) { + version = mainAttributes.getValue(Attributes.Name.SPECIFICATION_VERSION) + vcsHash = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION) + } + } + if (version == null || vcsHash == null) { + throw RuntimeException("Version information not found in manifest") + } + this.version = version + this.vcsHash = vcsHash + } + + override fun getVersion(): Array { + return if (version.endsWith("-SNAPSHOT")) { + arrayOf(version, vcsHash) + } else { + arrayOf(version) + } + } +} \ No newline at end of file diff --git a/wson-cli/src/main/kotlin/net/woggioni/wson/cli/main.kt b/wson-cli/src/main/kotlin/net/woggioni/wson/cli/main.kt index 66547f1..791d427 100644 --- a/wson-cli/src/main/kotlin/net/woggioni/wson/cli/main.kt +++ b/wson-cli/src/main/kotlin/net/woggioni/wson/cli/main.kt @@ -7,16 +7,13 @@ import java.io.OutputStreamWriter import java.nio.file.Files import java.nio.file.Path import kotlin.system.exitProcess -import com.beust.jcommander.IStringConverter -import com.beust.jcommander.JCommander -import com.beust.jcommander.Parameter -import com.beust.jcommander.ParameterException -import com.beust.jcommander.converters.PathConverter import net.woggioni.wson.serialization.binary.JBONDumper import net.woggioni.wson.serialization.binary.JBONParser import net.woggioni.wson.serialization.json.JSONDumper import net.woggioni.wson.serialization.json.JSONParser import net.woggioni.wson.xface.Value +import org.slf4j.LoggerFactory +import picocli.CommandLine sealed class SerializationFormat(val name: String) { @@ -41,88 +38,99 @@ sealed class SerializationFormat(val name: String) { } } -private class OutputTypeConverter : IStringConverter { +class OutputTypeConverter : CommandLine.ITypeConverter { override fun convert(value: String): SerializationFormat = SerializationFormat.parse(value) } -private class CliArg { - @Parameter(names = ["-f", "--file"], description = "Name of the input file to parse", converter = PathConverter::class) +class VersionProvider internal constructor() : AbstractVersionProvider("wson-cli") + +@CommandLine.Command( + name = "wson-cli", + versionProvider = VersionProvider::class) +private class WsonCli : Runnable { + + @CommandLine.Option( + names = ["-f", "--file"], + description = ["Name of the input file to parse"], + ) var fileName: Path? = null - @Parameter(names = ["--input-type"], description = "Input type", converter = OutputTypeConverter::class) + @CommandLine.Option( + names = ["--input-type"], + description = ["Input type"], + converter = [OutputTypeConverter::class]) var inputType: SerializationFormat = SerializationFormat.JSON - @Parameter(names = ["-o", "--output"], description = "Name of the JSON file to generate", converter = PathConverter::class) + @CommandLine.Option(names = ["-o", "--output"], + description = ["Name of the JSON file to generate"]) var output: Path? = null - @Parameter(names = ["-t", "--type"], description = "Output type", converter = OutputTypeConverter::class) + @CommandLine.Option( + names = ["-t", "--type"], + description = ["Output type"], + converter = [OutputTypeConverter::class]) var outputType: SerializationFormat = SerializationFormat.JSON - @Parameter(names = ["-h", "--help"], help = true) + @CommandLine.Option(names = ["-h", "--help"], usageHelp = true) var help: Boolean = false + + override fun run() { + val cfg = Value.Configuration.builder().serializeReferences(true).build() + val inputStream = if (fileName != null) { + BufferedInputStream(Files.newInputStream(fileName)) + } else { + System.`in` + } + + val result = when(inputType) { + SerializationFormat.JSON -> { + val reader = InputStreamReader(inputStream) + try { + JSONParser(cfg).parse(reader) + } finally { + reader.close() + } + } + SerializationFormat.JBON -> { + try { + JBONParser(cfg).parse(inputStream) + } finally { + inputStream.close() + } + } + } + + val outputStream = output?.let { + BufferedOutputStream(Files.newOutputStream(it)) + } ?: System.out + when(outputType) { + SerializationFormat.JSON -> { + val writer = OutputStreamWriter(outputStream) + try { + JSONDumper(cfg).dump(result, writer) + } finally { + writer.close() + } + } + SerializationFormat.JBON -> { + try { + JBONDumper(cfg).dump(result, outputStream) + } finally { + outputStream.close() + } + } + } + } } + fun main(vararg args: String) { - val cliArg = CliArg() - val cliArgumentParser = JCommander.newBuilder() - .addObject(cliArg) - .build() - try { - cliArgumentParser.parse(*args) - } catch (pe: ParameterException) { - cliArgumentParser.usage() - exitProcess(-1) - } - if (cliArg.help) { - cliArgumentParser.usage() - exitProcess(0) - } - val cfg = Value.Configuration.builder().serializeReferences(true).build() - val inputStream = if (cliArg.fileName != null) { - BufferedInputStream(Files.newInputStream(cliArg.fileName)) - } else { - System.`in` - } - - val result = when(cliArg.inputType) { - SerializationFormat.JSON -> { - val reader = InputStreamReader(inputStream) - try { - JSONParser(cfg).parse(reader) - } finally { - reader.close() - } - } - SerializationFormat.JBON -> { - try { - JBONParser(cfg).parse(inputStream) - } finally { - inputStream.close() - } - } - } - - val outputStream = if (cliArg.output != null) { - BufferedOutputStream(Files.newOutputStream(cliArg.output)) - } else { - System.out - } - when(cliArg.outputType) { - SerializationFormat.JSON -> { - val writer = OutputStreamWriter(outputStream) - try { - JSONDumper(cfg).dump(result, writer) - } finally { - writer.close() - } - } - SerializationFormat.JBON -> { - try { - JBONDumper(cfg).dump(result, outputStream) - } finally { - outputStream.close() - } - } + val log = LoggerFactory.getLogger("wson-cli") + val commandLine = CommandLine(WsonCli()) + commandLine.setExecutionExceptionHandler { ex, cl, parseResult -> + log.error(ex.message, ex) + CommandLine.ExitCode.SOFTWARE } + exitProcess(commandLine.execute(*args)) } \ No newline at end of file