Skip to content

Commit

Permalink
Add deprecated option to config field resolvers (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahvita authored Jun 12, 2023
1 parent 58c6863 commit c378460
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,21 @@ private void generateConfig() {
});
});

getAllConfigFields().stream().filter(ConfigField::getWithHelper)
getAllConfigFields().stream().filter(ConfigField::getWithHelper).filter(ConfigField::isDeprecated)
.forEach(configField -> {
writer.writeDocs(configField.getDeprecated().get());
writeWithHelperFunction(writer, configField);
});

getAllConfigFields().stream().filter(ConfigField::getWithHelper).filter(
Predicate.not(ConfigField::isDeprecated))
.forEach(configField -> {
writer.writeDocs(
String.format("With%s returns a functional option for setting the Client's %s option.",
String.format(
"With%s returns a functional option for setting the Client's %s option.",
configField.getName(), configField.getName()));
writer.openBlock("func With$L(v $P) func(*Options) {", "}", configField.getName(),
configField.getType(),
() -> {
writer.openBlock("return func(o *Options) {", "}", () -> {
writer.write("o.$L = v", configField.getName());
});
}).write("");
writeWithHelperFunction(writer, configField);

});

generateApplicationProtocolTypes();
Expand All @@ -233,6 +236,16 @@ private void generateConfig() {
});
}

private void writeWithHelperFunction(GoWriter writer, ConfigField configField) {
writer.openBlock("func With$L(v $P) func(*Options) {", "}", configField.getName(),
configField.getType(),
() -> {
writer.openBlock("return func(o *Options) {", "}", () -> {
writer.write("o.$L = v", configField.getName());
});
}).write("");
}

private List<ConfigField> getAllConfigFields() {
List<ConfigField> configFields = new ArrayList<>();
for (RuntimeClientPlugin runtimeClientPlugin : runtimePlugins) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public Optional<String> getDeprecated() {
return Optional.ofNullable(deprecated);
}

/**
* @return Returns if the config option is deprecated.
*/
public Boolean isDeprecated() {
return getDeprecated().isPresent();
}

@Override
public SmithyBuilder<ConfigField> toBuilder() {
return builder().type(type).name(name).documentation(documentation).withHelper(withHelper);
Expand Down
57 changes: 53 additions & 4 deletions middleware/metadata.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package middleware

import "github.com/aws/smithy-go"

// MetadataReader provides an interface for reading metadata from the
// underlying metadata container.
type MetadataReader = smithy.PropertiesReader
type MetadataReader interface {
Get(key interface{}) interface{}
}

// Metadata provides storing and reading metadata values. Keys may be any
// comparable value type. Get and set will panic if key is not a comparable
Expand All @@ -13,4 +13,53 @@ type MetadataReader = smithy.PropertiesReader
// Metadata uses lazy initialization, and Set method must be called as an
// addressable value, or pointer. Not doing so may cause key/value pair to not
// be set.
type Metadata = smithy.Properties
type Metadata struct {
values map[interface{}]interface{}
}

// Get attempts to retrieve the value the key points to. Returns nil if the
// key was not found.
//
// Panics if key type is not comparable.
func (m Metadata) Get(key interface{}) interface{} {
return m.values[key]
}

// Clone creates a shallow copy of Metadata entries, returning a new Metadata
// value with the original entries copied into it.
func (m Metadata) Clone() Metadata {
vs := make(map[interface{}]interface{}, len(m.values))
for k, v := range m.values {
vs[k] = v
}

return Metadata{
values: vs,
}
}

// Set stores the value pointed to by the key. If a value already exists at
// that key it will be replaced with the new value.
//
// Set method must be called as an addressable value, or pointer. If Set is not
// called as an addressable value or pointer, the key value pair being set may
// be lost.
//
// Panics if the key type is not comparable.
func (m *Metadata) Set(key, value interface{}) {
if m.values == nil {
m.values = map[interface{}]interface{}{}
}
m.values[key] = value
}

// Has returns whether the key exists in the metadata.
//
// Panics if the key type is not comparable.
func (m Metadata) Has(key interface{}) bool {
if m.values == nil {
return false
}
_, ok := m.values[key]
return ok
}

0 comments on commit c378460

Please sign in to comment.