Skip to content

Commit

Permalink
feat: add infer from string to mongo object id and newe object_id cas…
Browse files Browse the repository at this point in the history
…t type
  • Loading branch information
ajclopez committed Jul 21, 2024
1 parent f0d4e02 commit 230d77b
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 71 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ For Java 17:
<dependency>
<groupId>io.github.ajclopez</groupId>
<artifactId>mongo-spring-search</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
</dependency>
```

Expand All @@ -76,7 +76,7 @@ Add implementation to your project in your `build.gradle` file

For Java 17:
```
implementation 'io.github.ajclopez:mongo-spring-search:2.0.0'
implementation 'io.github.ajclopez:mongo-spring-search:2.0.1'
````
For Java 14:
Expand Down Expand Up @@ -264,10 +264,11 @@ Map<String, CastType> casters = new HashMap<String, CastType>();
casters.put("key1", CastType.STRING);
casters.put("key2", CastType.NUMBER);
casters.put("key3", CastType.STRING);
casters.put("key4", CastType.BOOLEAN);
casters.put("key4", CastType.OBJECT_ID);
casters.put("key5", CastType.BOOLEAN);

Configuration options = new Configuration(casters, null, null);
MongoSpringSearch.mss("key1=VALUE&key2=10&key3=20&key4=true", Optional.of(options));
MongoSpringSearch.mss("key1=VALUE&key2=10&key3=20&key4=666fde1af688038e64d6b58a&key5=true", Optional.of(options));
```

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.ajclopez</groupId>
<artifactId>mongo-spring-search</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
<packaging>jar</packaging>

<name>mongo-spring-search</name>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/github/ajclopez/mss/model/CastType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum CastType {
DATE,
BOOLEAN,
PATTERN,
OBJECT_ID,
STRING

}
11 changes: 9 additions & 2 deletions src/main/java/io/github/ajclopez/mss/parser/QueryParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.ajclopez.mss.model.SearchCriteria;
import io.github.ajclopez.mss.model.SearchOperation;
import io.github.ajclopez.mss.pattern.SearchPatterns;
import org.bson.types.ObjectId;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -81,11 +82,11 @@ public static SearchCriteria criteriaParser(String condition, Map<String, CastTy

/**
*
* The value is automatically cast: {@code Number, Date, Boolean, RegExp, String, List}.
* The value is automatically cast: {@code Number, Date, Boolean, RegExp, ObjectId, String, List}.
*
* @param value The value will be cast.
* @param caster Specify casting per value.
* @return Object Cast to {@code Number, Date, Boolean, RegExp, String, List}.
* @return Object Cast to {@code Number, Date, Boolean, RegExp, ObjectId, String, List}.
*/
public static Object parseValue(String value, CastType caster) {

Expand All @@ -112,6 +113,8 @@ public static Object parseValue(String value, CastType caster) {
}

return Pattern.compile(value.replace("/", ""));
case OBJECT_ID:
return new ObjectId(value);
case STRING:
return value;
default:
Expand Down Expand Up @@ -162,6 +165,10 @@ private static Object parseValue(String value) {
if ( matcher.matches() ) {
return Pattern.compile(matcher.group(1).replace("/", ""), SearchPatterns.getFlags(matcher.group(2)));
}

if ( ObjectId.isValid(value) ) {
return new ObjectId(value);
}

return value;
}
Expand Down

This file was deleted.

43 changes: 0 additions & 43 deletions src/test/java/io/github/ajclopez/mss/QueryParserTest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.github.ajclopez.mss;
package io.github.ajclopez.mss.model;

import io.github.ajclopez.mss.model.SearchCriteria;
import io.github.ajclopez.mss.model.SearchOperation;
import io.github.ajclopez.mss.parser.QueryParser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down
152 changes: 152 additions & 0 deletions src/test/java/io/github/ajclopez/mss/parser/QueryParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package io.github.ajclopez.mss.parser;

import io.github.ajclopez.mss.model.CastType;
import io.github.ajclopez.mss.model.SearchCriteria;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.List;
import java.util.regex.Pattern;

class QueryParserTest {


@Test
void whenQueryNotMatchThenReturnNullWhenCasterIsNull() {

SearchCriteria result = QueryParser.criteriaParser("", null);
Assertions.assertNull(result);
}

@Test
void whenValueIsNullThenReturnNullValueWhenCasterIsNull() {

Object result = QueryParser.parseValue(null, null);
Assertions.assertNull(result);
}

@Test
void whenValueIsStringWithCommaThenReturnListWhenCasterIsNull() {

Object result = QueryParser.parseValue("value1,value2,value3", null);

Assertions.assertNotNull(result);
Assertions.assertInstanceOf(List.class, result);

var list = (List<?>) result;
Assertions.assertEquals(3, list.size());
}

@Test
void whenTrueValueThenReturnBooleanWithTrueValueWhenCasterIsNull() {

Object result = QueryParser.parseValue("true", null);
Assertions.assertEquals(true, result);
}

@Test
void whenFalseValueThenReturnBooleanWithFalseValueWhenCasterIsNull() {

Object result = QueryParser.parseValue("false", null);
Assertions.assertEquals(false, result);
}

@Test
void whenNullStringThenReturnNullValueWhenCasterIsNull() {

Object result = QueryParser.parseValue("null", null);
Assertions.assertNull(result);
}

@Test
void whenDateTimeStringThenReturnInstantValueWhenCasterIsNull() {

Object result = QueryParser.parseValue("2024-07-21T12:00:00.000Z", null);
Assertions.assertNotNull(result);
Assertions.assertInstanceOf(Instant.class, result);
}

@Test
void whenRegexStringThenReturnPatternWhenCasterIsNull() {

String regex = "^58";
Object result = QueryParser.parseValue(regex, null);
Assertions.assertEquals(Pattern.compile(regex).toString(), result.toString());
}

@Test
void whenObjectIdStringThenReturnObjectIdWhenCasterIsNull() {

Object result = QueryParser.parseValue("6674249e4d854906d60314ce", null);
Assertions.assertNotNull(result);
Assertions.assertInstanceOf(ObjectId.class, result);
Assertions.assertEquals("6674249e4d854906d60314ce", result.toString());
}

@Test
void whenStringValueThenReturnStringWhenCasterIsNull() {

Object result = QueryParser.parseValue("firstname", null);
Assertions.assertNotNull(result);
Assertions.assertInstanceOf(String.class, result);
Assertions.assertEquals("firstname", result);
}

@Test
void whenTrueValueThenReturnBooleanWithTrueWhenCasterIsBoolean() {

Object result = QueryParser.parseValue("true", CastType.BOOLEAN);
Assertions.assertEquals(true, result);
}

@Test
void whenFalseValueThenReturnBooleanWithFalseWhenCasterIsBoolean() {

Object result = QueryParser.parseValue("false", CastType.BOOLEAN);
Assertions.assertEquals(false, result);
}

@Test
void whenDateTimeStringThenReturnInstantValueWhenCasterIsDate() {

Object result = QueryParser.parseValue("2024-07-21T12:00:00.000Z", CastType.DATE);
Assertions.assertNotNull(result);
Assertions.assertInstanceOf(Instant.class, result);
}

@Test
void whenNumberValueThenReturnNumberWhenCasterIsNumber() {

Object result = QueryParser.parseValue("10", CastType.NUMBER);
Assertions.assertInstanceOf(Number.class, result);
Assertions.assertEquals(10L, result);
}

@Test
void whenRegexValueThenReturnPatternWhenCasterIsPattern() {

String regex = "^58";
Object result = QueryParser.parseValue(regex, CastType.PATTERN);
Assertions.assertEquals(Pattern.compile(regex).toString(), result.toString());
}

@Test
void whenObjectIdValueThenReturnObjectIdWhenCasterIsObjectId() {

Object result = QueryParser.parseValue("6674249e4d854906d60314ce", CastType.OBJECT_ID);
Assertions.assertNotNull(result);
Assertions.assertInstanceOf(ObjectId.class, result);
Assertions.assertEquals("6674249e4d854906d60314ce", result.toString());
}

@Test
void whenStringValueThenReturnStringWhenCasterIsString() {

Object result = QueryParser.parseValue("firstname", CastType.STRING);
Assertions.assertNotNull(result);
Assertions.assertInstanceOf(String.class, result);
Assertions.assertEquals("firstname", result);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.ajclopez.mss;
package io.github.ajclopez.mss.pattern;

import io.github.ajclopez.mss.pattern.SearchPatterns;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down

0 comments on commit 230d77b

Please sign in to comment.