Skip to content

Commit

Permalink
Centralize custom name extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
fbiville committed Jun 23, 2017
1 parent 0179ad6 commit 7c60ccf
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 36 deletions.
Expand Up @@ -24,7 +24,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
Expand All @@ -40,6 +39,7 @@
import javax.lang.model.util.Types;

import org.neo4j.procedure.Procedure;
import org.neo4j.tooling.procedure.compilerutils.CustomNameExtractor;
import org.neo4j.tooling.procedure.messages.CompilationMessage;
import org.neo4j.tooling.procedure.messages.MessagePrinter;
import org.neo4j.tooling.procedure.validators.DuplicatedProcedureValidator;
Expand All @@ -57,21 +57,6 @@ public class ProcedureProcessor extends AbstractProcessor
private ElementVisitor<Stream<CompilationMessage>,Void> visitor;
private MessagePrinter messagePrinter;

public static Optional<String> getCustomName( Procedure proc )
{
String name = proc.name();
if ( !name.isEmpty() )
{
return Optional.of( name );
}
String value = proc.value();
if ( !value.isEmpty() )
{
return Optional.of( value );
}
return Optional.empty();
}

@Override
public Set<String> getSupportedOptions()
{
Expand Down Expand Up @@ -102,7 +87,8 @@ public synchronized void init( ProcessingEnvironment processingEnv )
visitor = new StoredProcedureVisitor( typeUtils, elementUtils,
processingEnv.getOptions().containsKey( IGNORE_CONTEXT_WARNINGS_OPTION ) );
duplicationPredicate =
new DuplicatedProcedureValidator<>( elementUtils, sprocType, ProcedureProcessor::getCustomName );
new DuplicatedProcedureValidator<>( elementUtils, sprocType,
( proc ) -> CustomNameExtractor.getName( proc::name, proc::value ) );
}

@Override
Expand Down
Expand Up @@ -20,6 +20,8 @@
package org.neo4j.tooling.procedure;

import com.google.auto.service.AutoService;

import org.neo4j.tooling.procedure.compilerutils.CustomNameExtractor;
import org.neo4j.tooling.procedure.compilerutils.TypeMirrorUtils;
import org.neo4j.tooling.procedure.messages.CompilationMessage;
import org.neo4j.tooling.procedure.messages.MessagePrinter;
Expand All @@ -29,7 +31,6 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
Expand All @@ -56,21 +57,6 @@ public class UserFunctionProcessor extends AbstractProcessor
private MessagePrinter messagePrinter;
private Function<Collection<Element>,Stream<CompilationMessage>> duplicationPredicate;

public static Optional<String> getCustomName( UserFunction function )
{
String name = function.name();
if ( !name.isEmpty() )
{
return Optional.of( name );
}
String value = function.value();
if ( !value.isEmpty() )
{
return Optional.of( value );
}
return Optional.empty();
}

@Override
public Set<String> getSupportedAnnotationTypes()
{
Expand All @@ -96,7 +82,7 @@ public synchronized void init( ProcessingEnvironment processingEnv )
messagePrinter = new MessagePrinter( processingEnv.getMessager() );
visitor = new UserFunctionVisitor( typeUtils, elementUtils, new TypeMirrorUtils( typeUtils, elementUtils ) );
duplicationPredicate = new DuplicatedProcedureValidator<>( elementUtils, userFunctionType,
UserFunctionProcessor::getCustomName );
( function ) -> CustomNameExtractor.getName( function::name, function::value ) );
}

@Override
Expand Down
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.tooling.procedure.compilerutils;

import java.util.Optional;
import java.util.function.Supplier;

import org.neo4j.procedure.Procedure;
import org.neo4j.procedure.UserFunction;
import org.neo4j.procedure.UserAggregationFunction;

public class CustomNameExtractor
{
private CustomNameExtractor()
{

}

/**
* Extracts user-defined names, usually from a {@link Procedure}, {@link UserFunction}
* or {@link UserAggregationFunction}.
*
* As such, extracted strings are assumed to be non-null.
*/
public static Optional<String> getName(Supplier<String> nameSupplier, Supplier<String> valueSupplier)
{
String name = nameSupplier.get().trim();
if ( !name.isEmpty() )
{
return Optional.of( name );
}
String value = valueSupplier.get().trim();
if ( !value.isEmpty() )
{
return Optional.of( value );
}
return Optional.empty();
}
}
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.tooling.procedure.compilerutils;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class CustomNameExtractorTest
{

@Test
public void favours_name_over_value()
{
assertThat(CustomNameExtractor.getName( () -> "name", () -> "value" )).contains( "name" );
assertThat(CustomNameExtractor.getName( () -> "name", () -> "" )).contains( "name" );
assertThat(CustomNameExtractor.getName( () -> "name", () -> " " )).contains( "name" );
assertThat(CustomNameExtractor.getName( () -> " name ", () -> " " )).contains( "name" );
}

@Test
public void returns_value_if_trimmed_name_is_empty()
{
assertThat(CustomNameExtractor.getName( () -> "", () -> "value" )).contains( "value" );
assertThat(CustomNameExtractor.getName( () -> " ", () -> "value" )).contains( "value" );
assertThat(CustomNameExtractor.getName( () -> " ", () -> " value " )).contains( "value" );
}

@Test
public void returns_nothing_if_none_defined()
{
assertThat(CustomNameExtractor.getName( () -> "", () -> "" )).isEmpty();
assertThat(CustomNameExtractor.getName( () -> " ", () -> "" )).isEmpty();
assertThat(CustomNameExtractor.getName( () -> "", () -> " " )).isEmpty();
assertThat(CustomNameExtractor.getName( () -> " ", () -> " " )).isEmpty();
}
}
Expand Up @@ -34,7 +34,7 @@
import javax.tools.Diagnostic;

import org.neo4j.procedure.Procedure;
import org.neo4j.tooling.procedure.ProcedureProcessor;
import org.neo4j.tooling.procedure.compilerutils.CustomNameExtractor;
import org.neo4j.tooling.procedure.messages.CompilationMessage;
import org.neo4j.tooling.procedure.validators.examples.DefaultProcedureA;
import org.neo4j.tooling.procedure.validators.examples.DefaultProcedureB;
Expand All @@ -58,7 +58,8 @@ public class DuplicatedProcedureValidatorTest
public void prepare()
{
elements = compilation.getElements();
validator = new DuplicatedProcedureValidator<>( elements, Procedure.class, ProcedureProcessor::getCustomName );
validator = new DuplicatedProcedureValidator<>( elements, Procedure.class,
( proc ) -> CustomNameExtractor.getName( proc::name, proc::value ) );
}

@Test
Expand Down

0 comments on commit 7c60ccf

Please sign in to comment.