WebservicePythonCodeGenerator implements WebserviceCodeGenerator { @Override public String generate(WebserviceCodeGenInfo info) { endl = info.getLineBreak(); PathQuery query = info.getQuery(); if (query == null) { return NULL_QUERY; } List<String> problems = query.verifyQuery(); if (!problems.isEmpty()) { return getInvalidQuery() + formatProblems(problems); } StringBuffer sb = new StringBuffer(getBoilerPlate()); sb.append("service = Service(\"").append(info.getServiceBaseURL()).append("/service\""); if (!info.isPublic()) { sb.append(", token = \"YOUR-API-KEY\""); } sb.append(")" + endl + endl); List<String> rootLessViews = new ArrayList<String>(); List<String> rowKeyAccesses = new ArrayList<String>(); for (String v: query.getView()) { rootLessViews.add(decapitate(v)); } for (String key : rootLessViews) { rowKeyAccesses.add("row[\"" + key + "\"]"); } String error = null; if (query instanceof TemplateQuery) { error = handleTemplate(sb, query); } else { error = handlePathQuery(sb, query, rootLessViews); } if (error != null) { return error; } StringBuffer currentLine = new StringBuffer(INDENT + "print("); Iterator<String> rowKeyIt = rowKeyAccesses.iterator(); boolean firstElement = true; while (rowKeyIt.hasNext()) { String toPrint = rowKeyIt.next(); if (!firstElement && StringUtils.isNotBlank(currentLine.toString())) { toPrint = " " + toPrint; } if (rowKeyIt.hasNext()) { toPrint += ","; } if (currentLine.length() + toPrint.length() > 100) { sb.append(currentLine.toString() + " \\" + endl); currentLine = new StringBuffer(INDENT + INDENT); if (!firstElement) { toPrint = toPrint.substring(1); } } currentLine.append(toPrint); firstElement = false; } sb.append(currentLine.toString() + ")" + endl); return sb.toString(); }  @Override String generate(WebserviceCodeGenInfo info);  }
@Test public void testPrintOneAttribute() throws Exception { List<ClassDescriptor> cds = new ArrayList<ClassDescriptor>(); List<AttributeDescriptor> ads = new ArrayList<AttributeDescriptor>(); ads.add(new AttributeDescriptor("testatt", "java.lang.String", null)); cds.add( new ClassDescriptor( "testns.testclass", null, false, ads, new HashSet<ReferenceDescriptor>(), new HashSet<CollectionDescriptor>(), null)); Model m = new Model("model1", "testns", cds); PathQuery pq = new PathQuery(m); pq.addView("testclass.testatt"); String result = cg.generate(new WebserviceCodeGenInfo(pq, null, null, null, true, null, "\n")); String[] lines = result.split("\n"); int expectedLines = 27; Assert.assertEquals(expectedLines, lines.length); Assert.assertEquals("print(row[\"testatt\"])", lines[expectedLines - 1].trim()); }
@Test public void testPrintThreeAttributes() throws Exception { List<ClassDescriptor> cds = new ArrayList<ClassDescriptor>(); List<AttributeDescriptor> ads = new ArrayList<AttributeDescriptor>(); ads.add(new AttributeDescriptor("testatt", "java.lang.String", null)); ads.add(new AttributeDescriptor("testatt2", "java.lang.String", null)); ads.add(new AttributeDescriptor("testatt3", "java.lang.String", null)); cds.add( new ClassDescriptor( "testns.testclass", null, false, ads, new HashSet<ReferenceDescriptor>(), new HashSet<CollectionDescriptor>(), null)); Model m = new Model("model1", "testns", cds); PathQuery pq = new PathQuery(m); pq.addViews("testclass.testatt", "testclass.testatt2", "testclass.testatt3"); String result = cg.generate(new WebserviceCodeGenInfo(pq, null, null, null, true, null, "\n")); String[] lines = result.split("\n"); int expectedLines = 27; Assert.assertEquals(expectedLines, lines.length); Assert.assertEquals( "print(row[\"testatt\"], row[\"testatt2\"], row[\"testatt3\"])", lines[expectedLines - 1].trim()); }