Skip to content

format function on CodeWriter which returns String #422

@trivikr

Description

@trivikr

Is your feature request related to a problem? Please describe.

  • JavaScript SDK team would like to reduce bundle size of JavaScript SDK v3 by removing unnecessary variables
  • One such way is to remove some local redundant variables like wrappedItem from ser-deser functions (code)
    • First part of this is using reusable function like getArrayIfSingleItem (commit)
    • The second part of it is to pass output pass call value returned by getArrayIfSingleItem to another function (experimental commit)

Describe the solution you'd like

  • CodeWriter exposes a function (say format) which returns formatted string.
  • This String can later be passed to

Existing write function: https://github.com/awslabs/smithy/blob/34d51e3f7f9c351ed0867a816e082ba3ac56cc4c/smithy-utils/src/main/java/software/amazon/smithy/utils/CodeWriter.java#L925-L945

This function can be as follows:

    public final CodeWriter write(Object content, Object... args) {
        String value = format(content, args);
        String[] lines = value.split(newlineRegexQuoted, -1);

        // Indent lines and strip excessive newlines.
        for (String line : lines) {
            currentState.writeLine(line + newline);
        }

        return this;
    }

    public final String format(Object content, Object... args) {
        return formatter.format(content, currentState.indentText, this, args);
    }

Describe alternatives you've considered

  • Using String.format instead which might surface bugs in future
  • Get around by writing complex CodeGen code using just the write method

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions