org.finos.legend.sdlc.versions.EntityPathTransformer Maven / Gradle / Ivy
// Copyright 2021 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.finos.legend.sdlc.versions;
import org.finos.legend.sdlc.domain.model.entity.Entity;
import org.finos.legend.sdlc.tools.entity.EntityPaths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Stream;
class EntityPathTransformer
{
private static final Pattern PACKAGE_PATH_PATTERN = Pattern.compile("^(\\w[\\w$]*+::)++\\w[\\w$]*+$");
private static final String PACKAGE_KEY = "package";
private static final String NAME_KEY = "name";
private final Function pathTransformer;
private final List entities = new ArrayList<>();
private EntityPathTransformer(Function pathTransformer)
{
this.pathTransformer = pathTransformer;
}
EntityPathTransformer addEntity(Entity entity)
{
this.entities.add(entity);
return this;
}
EntityPathTransformer addEntities(Iterable entities)
{
entities.forEach(this::addEntity);
return this;
}
EntityPathTransformer addEntities(Stream entities)
{
entities.forEach(this::addEntity);
return this;
}
EntityPathTransformer addEntities(Entity... entities)
{
if (entities != null)
{
for (int i = 0; i < entities.length; i++)
{
addEntity(entities[i]);
}
}
return this;
}
List getEntities()
{
return Collections.unmodifiableList(this.entities);
}
int getEntityCount()
{
return this.entities.size();
}
List transformEntities()
{
List newEntities = new ArrayList<>(this.entities.size());
this.entities.stream().map(this::transformEntity).forEach(newEntities::add);
return newEntities;
}
private Entity transformEntity(Entity entity)
{
String newPath = applyPathTransformer(entity.getPath());
if (newPath == null)
{
throw new IllegalStateException("Could not find transformation for entity path: " + entity.getPath());
}
return Entity.newEntity(newPath, entity.getClassifierPath(), transformPackageableElement(newPath, entity.getContent()));
}
private Object transformValue(Object value)
{
if (value == null)
{
return null;
}
if (value instanceof String)
{
String string = (String)value;
return isValidPath(string) ? applyPathTransformer(string) : string;
}
if (value instanceof List)
{
List list = (List)value;
List