-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fragment support between Java / Template file
See redhat-developer/vscode-quarkus#563 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
4232de9
commit 820e5a9
Showing
35 changed files
with
1,326 additions
and
298 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
.../projects/maven/qute-quickstart/src/main/java/org/acme/qute/ItemResourceWithFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.acme.qute; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import io.quarkus.qute.CheckedTemplate; | ||
import io.quarkus.qute.TemplateExtension; | ||
import io.quarkus.qute.TemplateInstance; | ||
|
||
@Path("items2") | ||
public class ItemResourceWithFragment { | ||
|
||
@CheckedTemplate | ||
static class Templates { | ||
static native TemplateInstance items(List<Item> items); | ||
static native TemplateInstance items$id1(List<Item> items); | ||
static native TemplateInstance items3$id2(List<Item> items); | ||
static native TemplateInstance items3$(List<Item> items); | ||
} | ||
|
||
@CheckedTemplate(ignoreFragments = true) | ||
static class Templates2 { | ||
static native TemplateInstance items2(List<Item> items); | ||
static native TemplateInstance items2$id1(List<Item> items); | ||
static native TemplateInstance items2$id2(List<Item> items); | ||
} | ||
|
||
@GET | ||
@Produces(MediaType.TEXT_HTML) | ||
public TemplateInstance get() { | ||
List<Item> items = new ArrayList<>(); | ||
items.add(new Item(new BigDecimal(10), "Apple")); | ||
items.add(new Item(new BigDecimal(16), "Pear")); | ||
items.add(new Item(new BigDecimal(30), "Orange")); | ||
return Templates.items(items); | ||
} | ||
|
||
/** | ||
* This template extension method implements the "discountedPrice" computed | ||
* property. | ||
*/ | ||
@TemplateExtension | ||
static BigDecimal discountedPrice(Item item) { | ||
return item.price.multiply(new BigDecimal("0.9")); | ||
} | ||
|
||
} |
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...edhat.qute.jdt/src/main/java/com/redhat/qute/commons/datamodel/DataModelBaseTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.qute.commons.datamodel; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Base class for data model fragment / template. | ||
* | ||
* @param <T> data model parameter. | ||
* | ||
* @see <a href= | ||
* "https://quarkus.io/guides/qute-reference#fragments">Fragments</a> | ||
* @see <a href= | ||
* "https://quarkus.io/guides/qute-reference#type_safe_fragments">Type-safe | ||
* Fragments</a> | ||
* | ||
* @author Angelo ZERR | ||
*/ | ||
|
||
public class DataModelBaseTemplate<T extends DataModelParameter> { | ||
|
||
private String sourceType; | ||
|
||
private String sourceMethod; | ||
|
||
private List<T> parameters; | ||
|
||
private transient Map<String, T> parametersMap; | ||
|
||
/** | ||
* Returns the Java source type where this data model template is defined. | ||
* | ||
* @return the Java source type where this data model template is defined. | ||
*/ | ||
public String getSourceType() { | ||
return sourceType; | ||
} | ||
|
||
/** | ||
* Set the Java source type where this data model template is defined. | ||
* | ||
* @param sourceType the Java source type where this data model template is | ||
* defined. | ||
*/ | ||
public void setSourceType(String sourceType) { | ||
this.sourceType = sourceType; | ||
} | ||
|
||
/** | ||
* Returns the Java source method where this data model template is defined and | ||
* null otherwise. | ||
* | ||
* @return the Java source method where this data model template is defined and | ||
* null otherwise. | ||
*/ | ||
public String getSourceMethod() { | ||
return sourceMethod; | ||
} | ||
|
||
/** | ||
* Set the Java source method where this data model template is defined and null | ||
* otherwise. | ||
* | ||
* @param sourceMethod the Java source method where this data model template is | ||
* defined and null otherwise. | ||
*/ | ||
public void setSourceMethod(String sourceMethod) { | ||
this.sourceMethod = sourceMethod; | ||
} | ||
|
||
/** | ||
* Returns the list of data model parameters. | ||
* | ||
* @return the list of data model parameters. | ||
*/ | ||
public List<T> getParameters() { | ||
return parameters; | ||
} | ||
|
||
/** | ||
* Set the list of data model parameters. | ||
* | ||
* @param parameters the list of data model parameters. | ||
*/ | ||
public void setParameters(List<T> parameters) { | ||
this.parameters = parameters; | ||
} | ||
|
||
/** | ||
* Returns the parameter from the given key and null otherwise. | ||
* | ||
* @param key the parameter key. | ||
* | ||
* @return the parameter from the given key and null otherwise. | ||
*/ | ||
public T getParameter(String key) { | ||
if (parameters == null) { | ||
return null; | ||
} | ||
return getParametersMap().get(key); | ||
} | ||
|
||
/** | ||
* Add the given parameter. | ||
* | ||
* @param parameter the parameter to add. | ||
*/ | ||
public void addParameter(T parameter) { | ||
if (parameters == null) { | ||
parameters = new ArrayList<>(); | ||
} | ||
parameters.add(parameter); | ||
getParametersMap().put(parameter.getKey(), parameter); | ||
} | ||
|
||
private Map<String, T> getParametersMap() { | ||
if (parametersMap == null) { | ||
parametersMap = parameters.stream() | ||
.collect(Collectors.toMap(DataModelParameter::getKey, Function.identity())); | ||
} | ||
return parametersMap; | ||
} | ||
} |
Oops, something went wrong.