Posts

Spring DI with different styles

Spring as a dependency injection (DI) framework allows us to define our application components' dependency tree in three different ways: XML Annotation Java Config I've written a simple app, bookstore, with the three styles and they're available in the following repository. You can take a look and see how each style would look like. It also has a version that uses no Spring beans for comparison. https://github.com/ryu1kn/spring--working-with-beans Different styles have different pros/cons. Spring lets you mix the styles; so you can change it as you go. Here is my observations on each style. XML-based configuration Pros Weak/loose coupling with Spring framework in your app code. Good for keeping the option of removing Spring later Class dependency information is centralised. Fine-grained control on the dependency definition. Changing only the dependency information doesn't require the recompilation of your app code. Cons Unless you have a good IDE

Using JavaScript eco-system from Clojure/Scala/Kotlin/PureScript/TypeScript

I've been playing with Clojure for close to two months now. One thing I like about Clojure is that it seems like I'm typing fewer characters to get what I want. Less code to read and maintain. Once I rewrote one of my recent js script with Clojure and its size shrunk down to 2/3. Another nice thing is that it can leverage the large collection of software asset (i.e. libraries) that people have written for Java (JVM). But I noticed that I was reading more characters when checking API docs or library code for checking the use of a library. Wondered if I can save some time if use Clojure with JavaScript libraries. I also wondered how the experience would differ if I use other languages to do the same thing; so after I did it with Clojure, I did it with Scala, then PureScript (which works only with js), ... I've put them in one repository so that you can compare them yourselves, and see what can be involved if you choose one to run your code on JavaScript engines. There is

Clojure Way

Do I have to use partial to do currying? Yes. cf. Rich Hickey's reason for not auto-currying Clojure functions? Do I have to do an explicit null check? e.g. html-node below can be null . (-> html-node (.getTextNode) (.getLiteral)) Seems so. (if html-node (-> html-node (.getTextNode) (.getLiteral)) "") ; or (#(if % (-> % (.getTextNode) (.getLiteral)) "") html-node) cf. in Scala: htmlNode.map(_.getTextNode.getLiteral).getOrElse("") Can I compose a function from a Java object method with only its method name? No. You may wrap with a lambda. cf. Usage of java methods as a function argument in Clojure (comp #(.getBar %) #(.getFoo %))

Use Blogger API from its Java client

Google Blogger provides an API to create and edit blog posts on Blogger. See their Introduction here. It also provides API client libraries for various languages including Java, JS, Python, etc. As I haven't found a sample app for the Java client, I'm writing this with the hope that this can save someone's time. In this post, I'm going to create a new blog post with a title "My Test Post" . Prerequisite You have a blog on Blogger (in this post, it's https://blog.example.org ) You have a GCP project with Billing enabled. You have gcloud command installed. cf. here . Steps Enable Blogger API Create OAuth 2.0 credentials Create OAuth consent screen Create a Java app that update your blog Get a new OAuth token Run the Java app to create a new post on your blog Step 1. Enable Blogger API Go to Blogger API page, make sure the correct project is selected, then enable the API. https://console.cloud.google.com/apis/api/blogger.googleapis.com

Leverage shell commands to quickly edit text on Visual Studio Code

Image
I really like vi. When I started using Visual Studio Code as my main editor shortly after it hit version 1, one thing I felt missing in Code was the ability to quickly edit text using various commands already available on your shell. For example, let’s say you’re editing a section of text that looks like following. Contributors: - Mike, 3 PRs - Elizabeth, 5 PRs - Robert, 2 PRs - Mary, 10 PRs If you want to order the contributors by the amount of contributions, in vi, you can utilise shell’s sort command: Select the list of contributors Execute a shell command sort -t, -k 2 -n -r on the visual mode Then you’ll get Contributors: - Mary, 10 PRs - Elizabeth, 5 PRs - Mike, 3 PRs - Robert, 2 PRs If you want to drop the number of PRs from the list, you can just pipe the sort output to cut command (e.g. sort -t, -k 2 -n -r | cut -d, -f1 ), because it’s just a shell command! Contributors: - Mary - Elizabeth - Mike - Ro

Terraform

Organise your infrastructure with terraform How to create reusable infrastructure with Terraform modules How to manage Terraform state But HIL is somehow limited Support for nested maps in variables #2114 Type system to support list and map element types Intermediate variables (OR: add interpolation support to input variables) Use null_data_source Can't create an output map

Golang way

I want to convert a list of type T1 to a list of type T2. Do I convert its element one-by-one? Yes, simply iterate over the whole list. cf. Type converting slices of interfaces in go Does a Go project needs to be under GOPATH ? I wrote a tool in Go and want to check it into an exising repository, but don't want to put the repo under GOPATH as it's mostly written in non-Go languages Add an appropriate place to GOPATH every time you run go commands. GOPATH for a project written in several languages (for example Go and Python)? AWS SDK for Go exposes struct s but not many interface s hence hard to mock in unit tests. Define interfaces as you need and use AWS SDK through them. cf. Discussion: How should you unit test code which uses the aws-sdk-go Where is list manipulation functions like filter , map , reduce , ...? Use for loop. cf. Map, reduce and filter in Go? , Writing type parametric functions in Go Dereferencing a nil pointer doesn't cause an error