Skip to content

Commit

Permalink
Fix plurals when follow another plural
Browse files Browse the repository at this point in the history
  • Loading branch information
george-harness committed Jun 8, 2021
1 parent c2bf5f9 commit 5dd7762
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
10 changes: 8 additions & 2 deletions src/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,28 @@ func (compiler *Compiler) Generate(query *dom.Query) string {
var methods strings.Builder
for i := 0; i < filtersCount; i++ {
var nextFieldName = ""
var titleNextFieldName = ""
if i == filtersCount-1 {
nextFieldName = "Final"
titleNextFieldName = "Final"
} else {
nextFieldName = query.Filters[i+1].FieldName
titleNextFieldName = strings.Title(nextFieldName)
if query.Filters[i+1].Operation == dom.In {
titleNextFieldName = pluralize.Plural(titleNextFieldName)
}
}

var currFieldType = query.Filters[i].FieldType
var currFieldName = query.Filters[i].FieldName
var currOperationType = query.Filters[i].Operation
switch currOperationType {
case dom.Eq:
methods.WriteString(fmt.Sprintf(filterMethodTemplate, name, strings.Title(nextFieldName), currFieldName, currFieldType, currFieldName,
methods.WriteString(fmt.Sprintf(filterMethodTemplate, name, titleNextFieldName, currFieldName, currFieldType, currFieldName,
collectionName, currFieldName, currFieldName))
case dom.In:
var pluralCurrentFieldName = pluralize.Plural(currFieldName)
methods.WriteString(fmt.Sprintf(filterMethodOperatorTemplate, name, strings.Title(nextFieldName), pluralCurrentFieldName, currFieldType,
methods.WriteString(fmt.Sprintf(filterMethodOperatorTemplate, name, titleNextFieldName, pluralCurrentFieldName, currFieldType,
pluralCurrentFieldName, collectionName, currFieldName, "in", pluralCurrentFieldName))

}
Expand Down
44 changes: 32 additions & 12 deletions src/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ func TestSanity2(t *testing.T) {
Name: "Select",
Collection: "io.harness.beans.DelegateTask",
Filters: []dom.Filter{
{FieldType: "String", FieldName: "uuid", Operation: dom.In},
{FieldType: "String", FieldName: "accountId", Operation: dom.Eq},
{FieldType: "String", FieldName: "orange", Operation: dom.In},
{FieldType: "String", FieldName: "worm", Operation: dom.Eq},
{FieldType: "String", FieldName: "apple", Operation: dom.In},
{FieldType: "String", FieldName: "banana", Operation: dom.In},
},
ProjectFields: []string{"foo", "bar"},
}
Expand All @@ -99,38 +101,56 @@ import com.google.common.collect.ImmutableList;
import java.util.List;
public class DelegateTaskSelectQuery implements PersistentQuery {
public static SelectQueryUuids create(HPersistence persistence) {
public static SelectQueryOranges create(HPersistence persistence) {
return new QueryImpl(persistence.createQuery(DelegateTask.class)
.project(DelegateTaskKeys.foo, true)
.project(DelegateTaskKeys.bar, true));
}
public interface SelectQueryUuids {
SelectQueryAccountId uuids(Iterable<String> uuids);
public interface SelectQueryOranges {
SelectQueryWorm oranges(Iterable<String> oranges);
}
public interface SelectQueryAccountId {
SelectQueryFinal accountId(String accountId);
public interface SelectQueryWorm {
SelectQueryApples worm(String worm);
}
public interface SelectQueryApples {
SelectQueryBananas apples(Iterable<String> apples);
}
public interface SelectQueryBananas {
SelectQueryFinal bananas(Iterable<String> bananas);
}
public interface SelectQueryFinal {
Query<DelegateTask> query();
}
private static class QueryImpl implements SelectQueryUuids, SelectQueryAccountId, SelectQueryFinal {
private static class QueryImpl implements SelectQueryOranges, SelectQueryWorm, SelectQueryApples, SelectQueryBananas, SelectQueryFinal {
Query<DelegateTask> query;
private QueryImpl(Query<DelegateTask> query) {
this.query = query;
}
@Override
public SelectQueryAccountId uuids(Iterable<String> uuids) {
query.field(DelegateTaskKeys.uuid).in(uuids);
public SelectQueryWorm oranges(Iterable<String> oranges) {
query.field(DelegateTaskKeys.orange).in(oranges);
return this;
}
@Override
public SelectQueryFinal accountId(String accountId) {
query.filter(DelegateTaskKeys.accountId, accountId);
public SelectQueryApples worm(String worm) {
query.filter(DelegateTaskKeys.worm, worm);
return this;
}
@Override
public SelectQueryBananas apples(Iterable<String> apples) {
query.field(DelegateTaskKeys.apple).in(apples);
return this;
}
@Override
public SelectQueryFinal bananas(Iterable<String> bananas) {
query.field(DelegateTaskKeys.banana).in(bananas);
return this;
}
Expand Down

0 comments on commit 5dd7762

Please sign in to comment.