mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 16:07:15 +02:00
Add metadata parameters to CreateLrpTokensCommand
As part of this change, built out a KeyValueMapParameter from the existing TransitionListParameter in order to enable other Map types in commands (two of which are used in CreateLrpTokensCommand). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=137476564
This commit is contained in:
parent
1dbc5f6bb0
commit
c89a902b72
6 changed files with 326 additions and 52 deletions
|
@ -16,33 +16,16 @@ package google.registry.tools.params;
|
|||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSortedMap;
|
||||
import com.google.common.collect.Ordering;
|
||||
import google.registry.model.registry.Registry.TldState;
|
||||
import java.util.Map;
|
||||
import org.joda.money.Money;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/**
|
||||
* Combined converter and validator class for transition list JCommander argument strings.
|
||||
*
|
||||
* <p>These strings have the form {@code <DateTime>=<T-str>,[<DateTime>=<T-str>]*} where
|
||||
* {@code <T-str>} is a string that can be parsed into an instance of some value type {@code T}, and
|
||||
* the entire argument represents a series of timed transitions of some property taking on those
|
||||
* values. This class converts such a string into an ImmutableSortedMap mapping DateTime to
|
||||
* {@code T}. Validation and conversion share the same logic; validation is just done by attempting
|
||||
* conversion and throwing exceptions if need be.
|
||||
*
|
||||
* <p>Subclasses must implement parseValue() to define how to parse {@code <T-str>} into a
|
||||
* {@code T}.
|
||||
*
|
||||
* @param <T> instance value type
|
||||
*/
|
||||
/** Combined converter and validator class for transition list JCommander argument strings. */
|
||||
// TODO(b/19031334): Investigate making this complex generic type work with the factory.
|
||||
public abstract class TransitionListParameter<T>
|
||||
extends ParameterConverterValidator<ImmutableSortedMap<DateTime, T>> {
|
||||
public abstract class TransitionListParameter<V> extends KeyValueMapParameter<DateTime, V> {
|
||||
|
||||
private static final DateTimeParameter DATE_TIME_CONVERTER = new DateTimeParameter();
|
||||
|
||||
|
@ -50,24 +33,17 @@ public abstract class TransitionListParameter<T>
|
|||
super("Not formatted correctly or has transition times out of order.");
|
||||
}
|
||||
|
||||
/** Override to define how to parse rawValue into an object of type T. */
|
||||
protected abstract T parseValue(String rawValue);
|
||||
|
||||
@Override
|
||||
public ImmutableSortedMap<DateTime, T> convert(String transitionListString) {
|
||||
ImmutableMap.Builder<DateTime, T> builder = new ImmutableMap.Builder<>();
|
||||
for (Map.Entry<String, String> entry :
|
||||
Splitter.on(',').withKeyValueSeparator('=').split(transitionListString).entrySet()) {
|
||||
builder.put(
|
||||
DATE_TIME_CONVERTER.convert(entry.getKey()),
|
||||
parseValue(entry.getValue()));
|
||||
}
|
||||
ImmutableMap<DateTime, T> transitionMap = builder.build();
|
||||
checkArgument(Ordering.natural().isOrdered(transitionMap.keySet()),
|
||||
"Transition times out of order.");
|
||||
return ImmutableSortedMap.copyOf(transitionMap);
|
||||
protected final DateTime parseKey(String rawKey) {
|
||||
return DATE_TIME_CONVERTER.convert(rawKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final ImmutableSortedMap<DateTime, V> processMap(ImmutableMap<DateTime, V> map) {
|
||||
checkArgument(Ordering.natural().isOrdered(map.keySet()), "Transition times out of order.");
|
||||
return ImmutableSortedMap.copyOf(map);
|
||||
}
|
||||
|
||||
/** Converter-validator for TLD state transitions. */
|
||||
public static class TldStateTransitions extends TransitionListParameter<TldState> {
|
||||
@Override
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue