Skip to content

Use generics in globalsearch #1134

Open
@exaV

Description

@exaV

globalSearch currently returns a wildcard which always requires a cast afterwards, even though the return type is known for each SearchScope.

List<?> projects = gitLabApi.getSearchApi().globalSearch(SearchScope.PROJECTS, "text-to-search-for");

Ideally we would have a generic return type, based on SearchScope:

List<Project> projects = gitLabApi.getSearchApi().globalSearch(SearchScope.PROJECTS, "text-to-search-for")

SearchScope is currently defined as an enum which makes it impossible to use generics. We could convert it to class with static members to be able to use the same syntax and profit from generics.

   // with this definition the above usage of globalSearch compiles:

    public <T> Pager<T> globalSearch(SearchScope<T> scope, String search, int itemsPerPage) {
        // ...
        switch (scope) {
            case BLOBS:
                return (new Pager<>(this, scope.getResultType(), itemsPerPage, formData.asMap(), "search"));
         // ...
       }
    }

    public static class SearchScope<T> {
        private SearchScope(Class<T> resultType, String jsonName) {
        }
        
        public static final SearchScope<Project> PROJECTS = new SearchScope<>(Project.class, "projects");
        public static final SearchScope<SearchBlob> BLOBS = new SearchScope<>(SearchBlob.class, "blobs");
        
       // JsonCreator, JsonValue and ToString can be implemented using the `jsonName` field
    }

This would be a breaking change because code will have to be recompiled, although there should be no changes necessary on the users side.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions