private InitCodeView buildInitCodeViewRequestObject( MethodContext context, InitCodeContext initCodeContext, InitCodeNode root) { assertNoOverlap(root, initCodeContext.sampleArgStrings()); return buildInitCodeView( context, root.listInInitializationOrder(), ImmutableList.of(root), sampleFuncParams(root, initCodeContext.sampleArgStrings())); }
private OptionalArrayMethodView createSmokeTestCaseApiMethodView(GapicMethodContext context) { OptionalArrayMethodView initialApiMethodView = new DynamicLangApiMethodTransformer(new PythonApiMethodParamTransformer()) .generateMethod(context); OptionalArrayMethodView.Builder apiMethodView = initialApiMethodView.toBuilder(); InitCodeTransformer initCodeTransformer = new InitCodeTransformer(); InitCodeView initCodeView = initCodeTransformer.generateInitCode( context, testCaseTransformer.createSmokeTestInitContext(context)); apiMethodView.initCode(initCodeView); return apiMethodView.build(); }
/** Generates initialization code from the given MethodContext and InitCodeContext objects. */ public InitCodeView generateInitCode( MethodContext methodContext, InitCodeContext initCodeContext) { InitCodeNode rootNode = InitCodeNode.createTree(initCodeContext); if (initCodeContext.outputType() == InitCodeOutputType.FieldList) { return buildInitCodeViewFlattened(methodContext, initCodeContext, rootNode); } else { return buildInitCodeViewRequestObject(methodContext, initCodeContext, rootNode); } }
fieldConfig.withResourceNameConfig(singleResourceNameConfig); initValue = createResourceNameInitValueView(context, anyResourceNameFieldConfig, item).build(); break; case FIXED: fieldConfig.withResourceNameConfig(singleResourceNameConfig); ResourceNameInitValueView initView = createResourceNameInitValueView(context, singleResourceNameFieldConfig, item) .build(); initValue = break; case SINGLE: initValue = createResourceNameInitValueView(context, fieldConfig, item).build(); break; case NONE: Lists.newArrayList( initValueConfig.getSingleResourceNameConfig().getNameTemplate().vars()); formattedInitValue.formatArgs(getFormatFunctionArgs(context, varList, initValueConfig)); createResourceNameInitValueView(context, fieldConfig, item) .convertToString(true) .build(); context.getTypeTable().getSnippetZeroValueAndSaveNicknameFor(item.getType())); simpleInitValue.isRepeated(item.getType().isRepeated()); if (isRequired(item.getFieldConfig(), context)) {
.initObjectType(methodContext.getMethodModel().getInputType()) .initFields(initContext.initFields()) .initValueConfigMap(createCollectionMap(methodContext)) .suggestedName(Name.from("request")) .fieldConfigMap(initContext.fieldConfigMap()) namer.getFieldGetFunctionName(methodContext.getFeatureConfig(), fieldConfig); String expectedValueIdentifier = getVariableName(methodContext, fieldItemTree); String expectedTransformFunction = null; String actualTransformFunction = null; createAssertView( expectedValueIdentifier, expectedTransformFunction,
/** * Return a list of three InitCodeView objects, including simpleInitCodeView and generating two * additional objects, which will be initialized with different initial values. */ private List<InitCodeView> createGrpcStreamingInitCodeViews( MethodContext methodContext, InitCodeContext initCodeContext, InitCodeView simpleInitCodeView) { List<InitCodeView> requestInitCodeList = new ArrayList<>(); requestInitCodeList.add(simpleInitCodeView); requestInitCodeList.add(initCodeTransformer.generateInitCode(methodContext, initCodeContext)); requestInitCodeList.add(initCodeTransformer.generateInitCode(methodContext, initCodeContext)); return requestInitCodeList; }
.suggestedName(Name.from("request")) .initFieldConfigStrings(methodContext.getMethodConfig().getSampleCodeInitFields()) .initValueConfigMap(InitCodeTransformer.createCollectionMap(methodContext)) .initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)) .outputType(initCodeOutputType)
@Test public void testOverlap() { InitCodeNode x = InitCodeNode.createWithChildren("x", InitCodeLineType.SimpleInitLine); InitCodeNode y = InitCodeNode.createWithChildren("y", InitCodeLineType.SimpleInitLine); InitCodeNode a = InitCodeNode.createWithChildren("a", InitCodeLineType.StructureInitLine, x, y); InitCodeNode b = InitCodeNode.createWithChildren("b", InitCodeLineType.SimpleInitLine); InitCodeNode root = InitCodeNode.createWithChildren("root", InitCodeLineType.StructureInitLine, a, b); InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a", "b")); InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a.x", "a.y")); InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a%x", "a%y")); assertThrows(() -> InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a", "a.x"))); assertThrows(() -> InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a", "a.y"))); assertThrows(() -> InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a", "a"))); assertThrows(() -> InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a.x", "a.x"))); assertThrows(() -> InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a.y", "a.y"))); assertThrows(() -> InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a", "a%x"))); assertThrows(() -> InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a%x", "a"))); assertThrows(() -> InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a.x%y", "a"))); assertThrows(() -> InitCodeTransformer.assertNoOverlap(root, ImmutableList.of("a%x", "a%x"))); }
InitCodeView initCode = initCodeTransformer.generateInitCode(methodContext, initCodeContext); InitCodeView requestObjectInitCode = requestObjectInitCodeContext != null ? initCodeTransformer.generateInitCode( requestObjectMethodContext, requestObjectInitCodeContext) : null; .asserts(initCodeTransformer.generateRequestAssertViews(methodContext, initCodeContext)) .clientMethodType(clientMethodType) .grpcStreamingType(methodConfig.getGrpcStreamingType())
public DynamicLangApiMethodTransformer(ApiMethodParamTransformer apiMethodParamTransformer) { this(apiMethodParamTransformer, new InitCodeTransformer()); }
private TestCaseView createRequestObjectTestCase( GapicMethodContext requestContext, MethodConfig methodConfig, SymbolTable testNameTable, Synchronicity synchronicity) { InitCodeContext initCodeContext = initCodeTransformer.createRequestInitCodeContext( requestContext, new SymbolTable(), methodConfig.getRequiredFieldConfigs(), InitCodeOutputType.SingleObject, valueGenerator); return testCaseTransformer.createTestCaseView( requestContext, testNameTable, initCodeContext, synchronicity == Synchronicity.Sync ? ClientMethodType.RequestObjectMethod : ClientMethodType.AsyncRequestObjectCallSettingsMethod, synchronicity, null, null); }
private StaticLangApiMethodView generateInitCode( StaticLangApiMethodView method, MethodContext context, Collection<FieldConfig> fieldConfigs, InitCodeOutputType initCodeOutputType, CallingForm callingForm) { // Replace the sample/init code using the same context as for the whole snippet file. // This is a bit hacky, but fixes the problem that initcode is generated using a different // context. Without this, the per-snippet imports don't get included in the snippet file. StaticLangApiMethodView.Builder builder = method.toBuilder(); sampleTransformer.generateSamples( builder, context, fieldConfigs, initCodeOutputType, initCodeContext -> initCodeTransformer.generateInitCode(context, initCodeContext), Arrays.asList(callingForm)); return builder.build(); } }
private InitCodeContext createInitCodeContext( MethodContext context, Collection<FieldConfig> fieldConfigs, InitCodeOutputType initCodeOutputType, List<String> sampleCodeDefaultValues, ImmutableMap<String, SampleParameterConfig> sampleParamConfigMap) { return InitCodeContext.newBuilder() .initObjectType(context.getMethodModel().getInputType()) .suggestedName(Name.from("request")) .initFieldConfigStrings(sampleCodeDefaultValues) .sampleParamConfigMap(sampleParamConfigMap) .initValueConfigMap(InitCodeTransformer.createCollectionMap(context)) .initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)) .outputType(initCodeOutputType) .fieldConfigMap(FieldConfig.toFieldConfigMap(fieldConfigs)) .build(); } }
private OptionalArrayMethodView createExampleApiMethodView( GapicMethodContext context, boolean packageHasMultipleServices) { OptionalArrayMethodView apiMethodView = new NodeJSMethodViewGenerator( new DynamicLangApiMethodTransformer( new NodeJSApiMethodParamTransformer(), new InitCodeTransformer(), SampleType.IN_CODE)) .generateOneApiMethod( context, testCaseTransformer.createSmokeTestInitContext(context), packageHasMultipleServices); return apiMethodView; }
private List<TestCaseView> createTestCaseViews(GapicInterfaceContext context) { ArrayList<TestCaseView> testCaseViews = new ArrayList<>(); SymbolTable testNameTable = new SymbolTable(); for (MethodModel method : context.getSupportedMethods()) { GapicMethodContext methodContext = context.asRequestMethodContext(method); ClientMethodType clientMethodType = ClientMethodType.RequestObjectMethod; if (methodContext.getMethodConfig().isPageStreaming()) { clientMethodType = ClientMethodType.PagedRequestObjectMethod; } else if (methodContext.getMethodConfig().isLongRunningOperation()) { clientMethodType = ClientMethodType.OperationRequestObjectMethod; } InitCodeContext initCodeContext = initCodeTransformer.createRequestInitCodeContext( methodContext, new SymbolTable(), methodContext.getMethodConfig().getRequiredFieldConfigs(), InitCodeOutputType.SingleObject, valueGenerator); testCaseViews.add( testCaseTransformer.createTestCaseView( methodContext, testNameTable, initCodeContext, clientMethodType)); } return testCaseViews; }
private InitCodeView buildInitCodeViewFlattened( MethodContext context, InitCodeContext initCodeContext, InitCodeNode root) { assertNoOverlap(root, initCodeContext.sampleArgStrings()); // Remove the request object for flattened method List<InitCodeNode> orderedItems = root.listInInitializationOrder(); orderedItems.remove(orderedItems.size() - 1); return buildInitCodeView( context, orderedItems, ImmutableList.copyOf(root.getChildren().values()), sampleFuncParams(root, initCodeContext.sampleArgStrings())); }
private OptionalArrayMethodView createSmokeTestCaseApiMethodView(GapicMethodContext context) { OptionalArrayMethodView initialApiMethodView = new DynamicLangApiMethodTransformer(new PhpApiMethodParamTransformer()) .generateMethod(context); OptionalArrayMethodView.Builder apiMethodView = initialApiMethodView.toBuilder(); InitCodeTransformer initCodeTransformer = new InitCodeTransformer(); InitCodeView initCodeView = initCodeTransformer.generateInitCode( context, testCaseTransformer.createSmokeTestInitContext(context)); apiMethodView.initCode(initCodeView); return apiMethodView.build(); }
private StaticLangApiMethodView createSmokeTestCaseApiMethodView(MethodContext methodContext) { StaticLangApiMethodView initialApiMethodView; if (methodContext.getMethodConfig().isPageStreaming()) { initialApiMethodView = apiMethodTransformer.generatePagedRequestObjectMethod(methodContext); } else { initialApiMethodView = apiMethodTransformer.generateRequestObjectMethod(methodContext); } StaticLangApiMethodView.Builder apiMethodView = initialApiMethodView.toBuilder(); InitCodeView initCodeView = initCodeTransformer.generateInitCode( methodContext, testCaseTransformer.createSmokeTestInitContext(methodContext)); apiMethodView.initCode(initCodeView); return apiMethodView.build(); } }
.suggestedName(Name.from("request")) .initFieldConfigStrings(methodContext.getMethodConfig().getSampleCodeInitFields()) .initValueConfigMap(InitCodeTransformer.createCollectionMap(methodContext)) .initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)) .outputType(InitCodeOutputType.SingleObject)
private OptionalArrayMethodView createExampleApiMethodView(GapicMethodContext context) { PythonMethodViewGenerator methodViewGenerator = new PythonMethodViewGenerator( new DynamicLangApiMethodTransformer( new PythonApiMethodParamTransformer(), new InitCodeTransformer(new PythonImportSectionTransformer()))); return methodViewGenerator.generateOneApiMethod(context); }