-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use GoModuleInfo for both ManifestWriter and GoModGenerator
Currently, `ManifestWriter` and `GoModGenerator` use different ways to calculate non-standard library dependencies and the go directive (minimum go version), which could produce inconsistencies. This commit updates both writers to use the same source of information (`GoModuleInfo`) for writing `generated.json` and `go.mod`.
- Loading branch information
Steven Yuan
committed
Jan 27, 2023
1 parent
5dba793
commit 8013efd
Showing
5 changed files
with
162 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoModuleInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.smithy.go.codegen; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
import software.amazon.smithy.codegen.core.SymbolDependency; | ||
import software.amazon.smithy.utils.BuilderRef; | ||
import software.amazon.smithy.utils.SmithyBuilder; | ||
|
||
public final class GoModuleInfo { | ||
|
||
public static final String DEFAULT_GO_DIRECTIVE = "1.15"; | ||
|
||
private List<SymbolDependency> dependencies; | ||
|
||
private List<SymbolDependency> stdLibDependencies; | ||
private List<SymbolDependency> nonStdLibDependencies; | ||
private String goDirective; | ||
private Map<String, String> minimumNonStdLibDependencies; | ||
|
||
private GoModuleInfo(Builder builder) { | ||
goDirective = SmithyBuilder.requiredState("goDirective", builder.goDirective); | ||
dependencies = builder.dependencies.copy(); | ||
|
||
// Intermediate dependency information | ||
stdLibDependencies = gatherStdLibDependencies(); | ||
nonStdLibDependencies = gatherNonStdLibDependencies(); | ||
|
||
// Module information used by ManifestWriter and GoModGenerator | ||
goDirective = calculateMinimumGoDirective(); | ||
minimumNonStdLibDependencies = gatherMinimumNonStdDependencies(); | ||
} | ||
|
||
public String getGoDirective() { | ||
return goDirective; | ||
} | ||
|
||
public Map<String, String> getMinimumNonStdLibDependencies() { | ||
return minimumNonStdLibDependencies; | ||
} | ||
|
||
private String calculateMinimumGoDirective() { | ||
String minimumGoDirective = goDirective; | ||
if (minimumGoDirective.compareTo(DEFAULT_GO_DIRECTIVE) < 0) { | ||
throw new IllegalArgumentException( | ||
"`goDirective` must be greater than or equal to the default go directive (" | ||
+ DEFAULT_GO_DIRECTIVE + "): " + minimumGoDirective); | ||
} | ||
for (SymbolDependency dependency : stdLibDependencies) { | ||
var otherVersion = dependency.getVersion(); | ||
if (minimumGoDirective.compareTo(otherVersion) < 0) { | ||
minimumGoDirective = otherVersion; | ||
} | ||
} | ||
return minimumGoDirective; | ||
} | ||
|
||
private List<SymbolDependency> gatherStdLibDependencies() { | ||
return filterDependencies(dependency -> | ||
dependency.getDependencyType().equals(GoDependency.Type.STANDARD_LIBRARY.toString())); | ||
} | ||
|
||
private List<SymbolDependency> gatherNonStdLibDependencies() { | ||
return filterDependencies(dependency -> | ||
!dependency.getDependencyType().equals(GoDependency.Type.STANDARD_LIBRARY.toString())); | ||
} | ||
|
||
private List<SymbolDependency> filterDependencies( | ||
Predicate<SymbolDependency> predicate | ||
) { | ||
List<SymbolDependency> filteredDependencies = new ArrayList<>(); | ||
for (SymbolDependency dependency : dependencies) { | ||
if (predicate.test(dependency)) { | ||
filteredDependencies.add(dependency); | ||
} | ||
} | ||
return filteredDependencies; | ||
} | ||
|
||
private Map<String, String> gatherMinimumNonStdDependencies() { | ||
return SymbolDependency.gatherDependencies(nonStdLibDependencies.stream(), | ||
GoDependency::mergeByMinimumVersionSelection).entrySet().stream().flatMap( | ||
entry -> entry.getValue().entrySet().stream()).collect( | ||
Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getVersion(), (a, b) -> b, TreeMap::new)); | ||
} | ||
|
||
public static class Builder implements SmithyBuilder<GoModuleInfo> { | ||
private String goDirective; | ||
private final BuilderRef<List<SymbolDependency>> dependencies = BuilderRef.forList(); | ||
|
||
public Builder goDirective(String goDirective) { | ||
this.goDirective = goDirective; | ||
return this; | ||
} | ||
|
||
public Builder dependencies(List<SymbolDependency> dependencies) { | ||
this.dependencies.clear(); | ||
this.dependencies.get().addAll(dependencies); | ||
return this; | ||
} | ||
|
||
@Override | ||
public GoModuleInfo build() { | ||
return new GoModuleInfo(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters