GithubHelp home page GithubHelp logo

Jinjava is rendering 0 about jinjava HOT 9 CLOSED

orgill avatar orgill commented on July 22, 2024
Jinjava is rendering 0

from jinjava.

Comments (9)

mattcoley avatar mattcoley commented on July 22, 2024 1

Yep - is reserved character for minus operation.

from jinjava.

mattcoley avatar mattcoley commented on July 22, 2024

Do you have a unit test that replicates the broken behavior you are seeing?

from jinjava.

orgill avatar orgill commented on July 22, 2024

No, I don't have. I can share my code, if you need

from jinjava.

mattcoley avatar mattcoley commented on July 22, 2024

Yep we would need to see your template and how you construct your context.

from jinjava.

orgill avatar orgill commented on July 22, 2024

this is my config file: application.yml

trinodb:
url: jdbc:trino://egap-trino.egdp-analytics.aws.away.black:8443/hive?SSL=true&SSLVerification=NONE
driverClass: io.trino.jdbc.TrinoDriver
catalog: hive
aws:
secretmanager:
storename: affiliate/cofund-reporting/trino-dev
environment: dev
supplyDb: egdp_prod_supply
accountManagementDb: egdp_prod_accountmanagement
#opportunity_view: cpce_affiliate.opportunity_to_sf
view:
opportunity-to-sf: cpce_affiliate
table:
supply-lodging-profile: egdp_prod_supply
sync-price-currency: bexg_prod_sync
booking-coupon: egdp_classic_booking
booking-order-summary: hcom_data_prod_uw2_bix_booking
booking-order: hcom_data_prod_uw2_bix_booking
sync-cards: bexg_prod_sync

this is my java file:

package com.expediagroup.cofundreportapiservice.dao;

import com.expediagroup.cofundreportapiservice.domain.GetDataForPartnerRequest;
import com.expediagroup.cofundreportapiservice.domain.Performance;
import com.expediagroup.cofundreportapiservice.domain.Transaction;
import com.hubspot.jinjava.Jinjava;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@component
public class TrinoCofundReportDao implements CofundReportDao {
private static final Logger LOG = LoggerFactory.getLogger(TrinoCofundReportDao.class);
private String transactionQueryTemplate;
private String performanceQueryTemplate;
private final String summaryWithCancel;
private final String summaryWithoutCancel;
private final String timeFrameStringCardsConsumedTrue;
private final String timeFrameStringHcomConsumedTrue;
private final String timeFrameStringCardsConsumedFalse;
private final String timeFrameStringHcomConsumedFalse;
//private final String opportunityToSf;
@Autowired
private JdbcTemplate trinoJdbcTemplate;
private Jinjava jinJava = new Jinjava();
private TransactionMapper transactionMapper = new TransactionMapper();
private PerformanceMapper performanceMapper = new PerformanceMapper();
@value("${environment}")
private String env;

@Value("${table.supply-lodging-profile}")
private String supply;

@Value("${table.sync-cards}")
private String cards;

@Value("${accountManagementDb}")
private String accountManagementDb;

@Value("${view.opportunity-to-sf}")
private String opportunityView;

@Value("${table.sync-price-currency}")
private String currency;

@Value("${table.booking-coupon}")
private String bookingCoupon;

@Value("${table.booking-order-summary}")
private String bookingOrderSummary;

@Value("${table.booking-order}")
private String bookingOrder;
public TrinoCofundReportDao() {
    summaryWithCancel = readClasspathResource("aggregateExcludeCancelTrue.jinja");
    summaryWithoutCancel = readClasspathResource("aggregateExcludeCancelFalse.jinja");
    timeFrameStringCardsConsumedTrue = readClasspathResource("timeFrame_bex_string_ConsumedTrue.jinja");
    timeFrameStringHcomConsumedTrue = readClasspathResource("timeFrame_hcom_string_ConsumedTrue.jinja");
    timeFrameStringCardsConsumedFalse = readClasspathResource("timeFrame_bex_string_ConsumedFalse.jinja");
    timeFrameStringHcomConsumedFalse = readClasspathResource("timeFrame_hcom_string_ConsumedFalse.jinja");
    //opportunityToSf = readClasspathResource("opportunityToSf.jinja");
}

private String readClasspathResource(String fileName) {
    try {
        return FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource(fileName).getInputStream()));
    } catch (IOException e) {
        throw new RuntimeException("Unable to read file " + fileName, e);
    }
}

@Override
public List<Performance> getPerformanceMetricsForPartner(GetDataForPartnerRequest request) {
    if ("test".equals(env)) {
        performanceQueryTemplate = readClasspathResource("performanceQueryTemplateTest.jinja");
    } else {
        performanceQueryTemplate = readClasspathResource("performanceQueryTemplate.jinja");
    }
    final String performanceQueryString = getQueryString(request, performanceQueryTemplate);
    LOG.info("Now calling getPerformanceMetricsForPartner())");
    LOG.info("performanceQueryString.first =" + performanceQueryString.substring(0, 16 * 1024));
    LOG.info("performanceQueryString.second =" + performanceQueryString.substring(16 * 1024));

    LOG.info("+++Test opportunity table:");
    LOG.info(trinoJdbcTemplate.queryForList("select * from " + accountManagementDb + ".retailpartner_eps_opportunity_domain_event_v1 limit 2").toString());

    LOG.info("+++Test retail partner table:");
    LOG.info(trinoJdbcTemplate.queryForList("select * from  " + accountManagementDb + ".retailpartner_eps_opportunity_domain_event_v1 limit 2").toString());

    LOG.info("+++Test opportunity view :");
    //LOG.info(trinoJdbcTemplate.queryForList("select * from   " + opportunityView + " limit 2").toString());

    LOG.info("+++Test retail cards table:");
    LOG.info(trinoJdbcTemplate.queryForList("select * from bexg_prod_sync.cards limit 2").toString());

    LOG.info("+++Test retail hcom order:");
    LOG.info(trinoJdbcTemplate.queryForList("select * from hcom_data_prod_uw2_bix_booking.hermes_bkg_order limit 2").toString());

    LOG.info("+++Test retail hcom order summary:");
    LOG.info(trinoJdbcTemplate.queryForList("select * from hcom_data_prod_uw2_bix_booking.hermes_booking_omniture_order_summary limit 2").toString());

    LOG.info("+++Test retail coupon dim table:");
    LOG.info(trinoJdbcTemplate.queryForList("select * from egdp_classic_booking.coupn_dim limit 2").toString());

    LOG.info("+++Test retail supply table:");
    //LOG.info(trinoJdbcTemplate.queryForList("select * from    " + supply + ".lodging_profile_eg limit 2").toString());

    LOG.info("+++Test retail price currency table:");
    LOG.info(trinoJdbcTemplate.queryForList("select * from bexg_prod_sync.price_currn_dim limit 2").toString());

    final List<Performance> results = trinoJdbcTemplate.query(performanceQueryString, performanceMapper);
    LOG.info("Results of performance report: " + results);
    return results;
}

@Override
public List<Transaction> getTransactionsForPartner(GetDataForPartnerRequest request) {
    if ("test".equals(env)) {
        transactionQueryTemplate = readClasspathResource("transactionQueryTemplateTest.jinja");
    } else {
        transactionQueryTemplate = readClasspathResource("transactionQueryTemplate.jinja");
    }
    final String transactionQueryString = getQueryString(request, transactionQueryTemplate);
    return trinoJdbcTemplate.query(transactionQueryString, transactionMapper);
}

private String getQueryString(GetDataForPartnerRequest request, String queryTemplate) {
    final Map<String, Object> timeFrTemplateValues = new HashMap<>();
    final Map<String, Object> templateValues = new HashMap<>();

    final String dateFrom = request.getDateFrom().format(DateTimeFormatter.ISO_LOCAL_DATE);
    final String dateTo = request.getDateTo().format(DateTimeFormatter.ISO_LOCAL_DATE);
    templateValues.put("partner_id", request.getPartnerId());
    System.out.println("\n\n\n\\\n\\n\n\ncheckcheckcheck    supply   "+supply+"\n\\n\n\n\n\n\n\n");
    templateValues.put("supply-lodging-profile", supply);
    System.out.println("\n\n\n\\\n\\n\n\ncheckcheckcheck    opportunityView   "+opportunityView+"\n\\n\n\n\n\n\n\n");
    templateValues.put("opportunity-to-sf", opportunityView);
    System.out.println("\n\n\n\\\n\\n\n\ncheckcheckcheck   currency    "+currency+"\n\\n\n\n\n\n\n\n");
    templateValues.put("sync-price-currency", currency);
    System.out.println("\n\n\n\\\n\\n\n\ncheckcheckcheck   bookingCoupon    "+bookingCoupon+"\n\\n\n\n\n\n\n\n");
    templateValues.put("booking-coupon", bookingCoupon);
    System.out.println("\n\n\n\\\n\\n\n\ncheckcheckcheck   bookingOrderSummary    "+bookingOrderSummary+"\n\\n\n\n\n\n\n\n");
    templateValues.put("booking-order-summary", bookingOrderSummary);
    System.out.println("\n\n\n\\\n\\n\n\ncheckcheckcheck   bookingOrder    "+bookingOrder+"\n\\n\n\n\n\n\n\n");
    templateValues.put("booking-order", bookingOrder);
    timeFrTemplateValues.put("date_from", dateFrom);
    timeFrTemplateValues.put("date_to", dateTo);

    final var timeFrCardsConsumedTrue = jinJava.render(timeFrameStringCardsConsumedTrue, timeFrTemplateValues);
    final var timeFrCardsConsumedFalse = jinJava.render(timeFrameStringCardsConsumedFalse, timeFrTemplateValues);
    final var timeFrHcomConsumedFalse = jinJava.render(timeFrameStringHcomConsumedFalse, timeFrTemplateValues);
    final var timeFrHcomConsumedTrue = jinJava.render(timeFrameStringHcomConsumedTrue, timeFrTemplateValues);

    if (request.isConsumed()) {
        templateValues.put("timeframe_string_cards", timeFrCardsConsumedTrue);
        templateValues.put("timeframe_string_hcom", timeFrHcomConsumedTrue);
    } else {
        templateValues.put("timeframe_string_cards", timeFrCardsConsumedFalse);
        templateValues.put("timeframe_string_hcom", timeFrHcomConsumedFalse);
    }

    if (request.isExcludeCancellations()) {
        templateValues.put("aggregate_section", summaryWithCancel);
    } else {
        templateValues.put("aggregate_section", summaryWithoutCancel);
    }

    System.out.println("\n\n\n\\\n\\n\n\nblablablablabla   booking-order-summary    "+templateValues+"\n\\n\n\n\n\n\n\n");
    return jinJava.render(queryTemplate, templateValues);
}

}


Values are fetched successfully form configs. I even printed them right before return "jinJava.render(queryTemplate, templateValues);" line and those were there. but when rendered, they are injected as 0.

from jinjava.

Indystef avatar Indystef commented on July 22, 2024

I can confirm this behavior. I am using Java 17, and each and every render returns "0"

from jinjava.

jasmith-hs avatar jasmith-hs commented on July 22, 2024

@Indystef are you able to post a concise reproduction example? Preferably a unit test in this repo that demonstrates the behavior

from jinjava.

Indystef avatar Indystef commented on July 22, 2024

I figured it out. Just use an expression with a minus sign in it ... that gets interpreted as a numeric operation. For instance this code:

    Jinjava jinjava = new Jinjava();
    var context = Map.of( "test-expression", "world");
    var template = "Hello {{ test-expression }}";
    var result = jinjava.render(template, context);
    System.out.println(result);

will output "Hello 0"

from jinjava.

orgill avatar orgill commented on July 22, 2024

from jinjava.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.