Skip to content

Commit

Permalink
Fixing code style in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sashirestela committed Jan 18, 2025
1 parent 15a2627 commit 8c6b7ca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.github.sashirestela.cleverclient.example;

import io.github.sashirestela.cleverclient.CleverClient;
import io.github.sashirestela.cleverclient.example.jsonplaceholder.UserService;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.github.sashirestela.cleverclient.CleverClient;
import io.github.sashirestela.cleverclient.example.jsonplaceholder.UserService;

public class ResponseInterceptorExample extends AbstractExample {

public ResponseInterceptorExample(String clientAlias) {
Expand Down Expand Up @@ -38,55 +38,54 @@ public void run() {

private String transformUsers(String jsonInput) {
List<String> flatUsers = new ArrayList<>();

// Simpler pattern that matches each field individually
String patternStr =
"\"id\":\\s*(\\d+).*?" + // id
"\"name\":\\s*\"([^\"]+)\".*?" + // name
"\"username\":\\s*\"([^\"]+)\".*?" + // username
"\"email\":\\s*\"([^\"]+)\".*?" + // email
"\"street\":\\s*\"([^\"]+)\".*?" + // street
"\"suite\":\\s*\"([^\"]+)\".*?" + // suite
"\"city\":\\s*\"([^\"]+)\".*?" + // city
"\"phone\":\\s*\"([^\"]+)\".*?" + // phone
"\"website\":\\s*\"([^\"]+)\".*?" + // website
"\"company\":\\s*\\{\\s*\"name\":\\s*\"([^\"]+)\""; // company name

String patternStr = "\"id\":\\s*(\\d+).*?" + // id
"\"name\":\\s*\"([^\"]+)\".*?" + // name
"\"username\":\\s*\"([^\"]+)\".*?" + // username
"\"email\":\\s*\"([^\"]+)\".*?" + // email
"\"street\":\\s*\"([^\"]+)\".*?" + // street
"\"suite\":\\s*\"([^\"]+)\".*?" + // suite
"\"city\":\\s*\"([^\"]+)\".*?" + // city
"\"phone\":\\s*\"([^\"]+)\".*?" + // phone
"\"website\":\\s*\"([^\"]+)\".*?" + // website
"\"company\":\\s*\\{\\s*\"name\":\\s*\"([^\"]+)\""; // company name

Pattern pattern = Pattern.compile(patternStr, Pattern.DOTALL);
Matcher matcher = pattern.matcher(jsonInput);

while (matcher.find()) {
String flatUser = String.format(
"{\n" +
" \"id\": %s,\n" +
" \"name\": \"%s\",\n" +
" \"username\": \"%s\",\n" +
" \"email\": \"%s\",\n" +
" \"address\": \"%s, %s, %s\",\n" +
" \"phone\": \"%s\",\n" +
" \"website\": \"%s\",\n" +
" \"company\": \"%s\"\n" +
"}",
matcher.group(1), // id
matcher.group(2), // name
matcher.group(3), // username
matcher.group(4), // email
matcher.group(5), // street
matcher.group(6), // suite
matcher.group(7), // city
matcher.group(8), // phone
matcher.group(9), // website
matcher.group(10) // company name
"{\n" +
" \"id\": %s,\n" +
" \"name\": \"%s\",\n" +
" \"username\": \"%s\",\n" +
" \"email\": \"%s\",\n" +
" \"address\": \"%s, %s, %s\",\n" +
" \"phone\": \"%s\",\n" +
" \"website\": \"%s\",\n" +
" \"company\": \"%s\"\n" +
"}",
matcher.group(1), // id
matcher.group(2), // name
matcher.group(3), // username
matcher.group(4), // email
matcher.group(5), // street
matcher.group(6), // suite
matcher.group(7), // city
matcher.group(8), // phone
matcher.group(9), // website
matcher.group(10) // company name
);
flatUsers.add(flatUser);
}

// If no matches were found, print input for debugging
if (flatUsers.isEmpty()) {
System.err.println("No matches found in input: " + jsonInput);
return "[]";
}

// Combine all users into a JSON array
return "[\n " + String.join(",\n ", flatUsers) + "\n]";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.github.sashirestela.cleverclient.example.jsonplaceholder;

import java.util.List;

import io.github.sashirestela.cleverclient.annotation.GET;
import io.github.sashirestela.cleverclient.annotation.Query;
import io.github.sashirestela.cleverclient.annotation.Resource;

import java.util.List;

@Resource("/users")
public interface UserService {

Expand Down

0 comments on commit 8c6b7ca

Please sign in to comment.