public static void writeIfZeroValue()

in codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoValueAccessUtils.java [191:237]


    public static void writeIfZeroValue(
            Model model,
            GoWriter writer,
            MemberShape member,
            String operand,
            boolean ignoreEmptyString,
            boolean ignoreUnboxedTypes,
            Runnable lambda
    ) {
        Shape targetShape = model.expectShape(member.getTarget());
        Shape container = model.expectShape(member.getContainer());

        String check = "{";
        if (GoPointableIndex.of(model).isNillable(member)) {
            if (!ignoreEmptyString && targetShape.getType() == ShapeType.STRING) {
                check = String.format("if %s == nil || len(*%s) == 0 {", operand, operand);
            } else {
                check = String.format("if %s == nil {", operand);
            }
        } else if (container instanceof CollectionShape || container.getType() == ShapeType.MAP) {
            // Always serialize values in map/list/sets, no additional check, which means that the
            // lambda will not be run, because there is no zero value to check against.
            if (!ignoreEmptyString && targetShape.getType() == ShapeType.STRING) {
                check = String.format("if len(%s) == 0 {", operand);
            } else {
                return;
            }

        } else if (targetShape.hasTrait(EnumTrait.class)) {
            check = String.format("if len(%s) == 0 {", operand);

        } else if (!ignoreUnboxedTypes && targetShape.getType() == ShapeType.BOOLEAN) {
            check = String.format("if !%s {", operand);

        } else if (!ignoreUnboxedTypes && CodegenUtils.isNumber(targetShape)) {
            check = String.format("if %s == 0 {", operand);

        } else if (!ignoreEmptyString && targetShape.getType() == ShapeType.STRING) {
            check = String.format("if len(%s) == 0 {", operand);

        } else {
            // default to empty block for variable scoping with not value check.
            return;
        }

        writer.openBlock(check, "}", lambda);
    }