public default PrintArgView generatePrintArg( MethodContext context, OutputView.VariableView variableView) { return PrintArgView.newBuilder() .segments( ImmutableList.<PrintArgView.ArgSegmentView>of( PrintArgView.VariableSegmentView.of(variableView))) .build(); } }
/** * Converts the {@code VariableView} to a {@link com.google.api.codegen.viewmodel.PrintArgView} * that knows how to print proto types nicely. Has effects only when the variable is an enum. * * <p>In python, calling {@code print(x)} or {@code print(''.format(x))} would only get an int * when {@code x} is an enum. To print the enum name of the value, we need to convert the enum * value to it's name. * * <p>When the type of the enum value {@code x} is a top-level enum {@code EnumType}, we can call * {@code print(enums.EnumType(x).name)} to print the name of {@code x}. * * <p>When the type of the enum value {@code x} is a nested enum {@code MessageType.EnumType}, we * can call {@code print(enums.MessageType.EnumType(x).name)} to print the name of {@code x}. */ @Override public PrintArgView generatePrintArg( MethodContext context, OutputView.VariableView variableView) { if (variableView.type() == null || !variableView.type().isEnum()) { return PrintArgView.newBuilder() .segments(ImmutableList.<ArgSegmentView>of(VariableSegmentView.of(variableView))) .build(); } ImmutableList<ArgSegmentView> segments = ImmutableList.of( TextSegmentView.of(getEnumTypeClassName(context, variableView.type()) + "("), VariableSegmentView.of(variableView), TextSegmentView.of(").name")); return PrintArgView.newBuilder().segments(segments).build(); }