Skip to content

Commit

Permalink
Merge AbsoluteLabelImport and RelativeLabelImport classes.
Browse files Browse the repository at this point in the history
RELNOTES: None.
PiperOrigin-RevId: 243623858
  • Loading branch information
laurentlb authored and copybara-github committed Apr 15, 2019
1 parent 4333692 commit ff35a05
Showing 1 changed file with 14 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
import javax.annotation.Nullable;

/**
* Factory class for creating appropriate instances of {@link SkylarkImports}.
Expand All @@ -37,38 +38,27 @@ private SkylarkImports() {

@VisibleForSerialization
@AutoCodec
static final class AbsoluteLabelImport extends SkylarkImport {
private final Label importLabel;
static final class LabelImport extends SkylarkImport {
@Nullable private final Label importLabel;

@VisibleForSerialization
AbsoluteLabelImport(String importString, Label importLabel) {
LabelImport(String importString, @Nullable Label importLabel) {
super(importString);
this.importLabel = importLabel;
}

@Override
public Label getLabel(Label containingFileLabel) {
// When the import label contains no explicit repository identifier, we resolve it relative
// to the repo of the containing file.
return containingFileLabel.resolveRepositoryRelative(importLabel);
}
}

@VisibleForSerialization
@AutoCodec
static final class RelativeLabelImport extends SkylarkImport {
@VisibleForSerialization
RelativeLabelImport(String importString) {
super(importString);
}
if (importLabel != null) {
// When the import label contains no explicit repository identifier, we resolve it relative
// to the repo of the containing file.
return containingFileLabel.resolveRepositoryRelative(importLabel);
}

@Override
public Label getLabel(Label containingFileLabel) {
// Unlike a relative path import, the import target is relative to the containing package,
// not the containing directory within the package.
// The import target is relative to the containing package, not the containing directory
// within the package.
try {
// This is for imports relative to the current repository, so repositoryMapping can be
// empty
// This is for imports relative to the current repository, so repositoryMapping can be empty
return containingFileLabel.getRelativeWithRemapping(getImportString(), ImmutableMap.of());
} catch (LabelSyntaxException e) {
// shouldn't happen because the parent label is assumed validated and the target string is
Expand Down Expand Up @@ -147,7 +137,7 @@ public static SkylarkImport create(
if (packageId.equals(LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER)) {
throw new SkylarkImportSyntaxException(EXTERNAL_PKG_NOT_ALLOWED_MSG);
}
return new AbsoluteLabelImport(importString, importLabel);
return new LabelImport(importString, importLabel);
} else if (importString.startsWith(":")) {
// Relative label. We require that relative labels use an explicit ':' prefix.
String importTarget = importString.substring(1);
Expand All @@ -156,7 +146,7 @@ public static SkylarkImport create(
// Null indicates successful target validation.
throw new SkylarkImportSyntaxException(INVALID_TARGET_PREFIX + maybeErrMsg);
}
return new RelativeLabelImport(importString);
return new LabelImport(importString, null);
}

throw new SkylarkImportSyntaxException(INVALID_PATH_SYNTAX);
Expand Down

0 comments on commit ff35a05

Please sign in to comment.