From notifications-return-6115-archive-asf-public=cust-asf.ponee.io@freemarker.apache.org Tue Oct 15 21:54:02 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 5855418067C for ; Tue, 15 Oct 2019 23:54:02 +0200 (CEST) Received: (qmail 69157 invoked by uid 500); 15 Oct 2019 21:54:01 -0000 Mailing-List: contact notifications-help@freemarker.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@freemarker.apache.org Delivered-To: mailing list notifications@freemarker.apache.org Received: (qmail 69098 invoked by uid 99); 15 Oct 2019 21:54:01 -0000 Received: from mailrelay1-us-west.apache.org (HELO mailrelay1-us-west.apache.org) (209.188.14.139) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Oct 2019 21:54:01 +0000 Received: from jira-he-de.apache.org (static.172.67.40.188.clients.your-server.de [188.40.67.172]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id BE1C5E30A4 for ; Tue, 15 Oct 2019 21:54:00 +0000 (UTC) Received: from jira-he-de.apache.org (localhost.localdomain [127.0.0.1]) by jira-he-de.apache.org (ASF Mail Server at jira-he-de.apache.org) with ESMTP id 3B39F78053F for ; Tue, 15 Oct 2019 21:54:00 +0000 (UTC) Date: Tue, 15 Oct 2019 21:54:00 +0000 (UTC) From: =?utf-8?Q?D=C3=A1niel_D=C3=A9k=C3=A1ny_=28Jira=29?= To: notifications@freemarker.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Comment Edited] (FREEMARKER-107) Hash expansion to macro arguments (Python **kwargs style) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/FREEMARKER-107?page=3Dcom.atlas= sian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=3D= 16952329#comment-16952329 ]=20 D=C3=A1niel D=C3=A9k=C3=A1ny edited comment on FREEMARKER-107 at 10/15/19 9= :53 PM: -------------------------------------------------------------------- OK, so let's call that thing {{.arg_locals}}, because it's not really the a= rguments, which the {{args}} name implies. So {{.arg_locals}} takes a snaps= hot of the +subset+ of local variables that were originally created to hold= the arguments. It reads the +current+ value of those locals, and puts them= into an FTL hash (a map). I'm saying +current+, because if you had {{<#loc= al a =3D newValue>}} somewhere before that, that will affect {{.arg_locals}= } with this approach. The next wrinkle, as we know, is that if you want to use this for {{?spread= _args}}, then you need the catch-alls exploded, so then, we also need an=C2= =A0{{.arg_locals_exploded}}. Ugh. Having all 3 variations looks like an overkill. It seems you only need=C2= =A0{{.arg_locals_exploded}} (a quite verbose name though). But, maybe try t= o solve some actual problems with only {{.args}}, and then only with {{.arg= s_locals}}, and then only with {{.arg_locals_exploded}}. Just to see if som= ething interesting falls off. was (Author: ddekany): OK, so let's call that thing {{.arg_locals}}, because it's not really the a= rguments, which the {{args}} name implies. So {{.arg_locals}} takes a snaps= hot of the +subset+ of local variables that were originally created to hold= the arguments. It reads the +current+ value of those locals, and puts them= into an FTL hash (a map). I'm saying +current+, because if you had {{<#loc= al a =3D newValue>}} somewhere before that, that will affect {{.arg_locals}= } with this approach. The next wrinkle, as we know, is that if you want to use this for {{?spread= _args}}, then you need the catch-alls exploded, so then, we also need an=C2= =A0{{.arg_locals_exploded}}. Ugh. Haven all 3 variations looks like an overkill. It seems you only need=C2=A0= {{.arg_locals_exploded}} (a quite verbose name though). But, maybe try to s= olve some actual problems with only {{.args}}, and then only with {{.args_l= ocals}}, and then only with {{.arg_locals_exploded}}. Just to see if someth= ing interesting falls off. > Hash expansion to macro arguments (Python **kwargs style) > --------------------------------------------------------- > > Key: FREEMARKER-107 > URL: https://issues.apache.org/jira/browse/FREEMARKER-107 > Project: Apache Freemarker > Issue Type: New Feature > Components: engine > Affects Versions: 2.3.28 > Reporter: Pascal Proulx > Priority: Major > > Hello, > We heavily rely on Freemarker macros to build a helper template API, but = have had to make large workarounds for passing contents of hashes as macro = arguments, for several years. (In truth I should have made this ticket much= sooner!) > It would help greatly simplify our work to have hash expansion to macro a= rguments, like this: > {code:java} > <#macro myMacro arg1 arg2 arg3=3D"value3">... > <#assign myHash =3D {"arg1":"value1", "arg2":"value2"}> > <@myMacro **myHash/><#-- the hash contents are passed as parameters, inst= ead of the hash itself --> > {code} > This exists in Python: > {code:java} > def test_var_args_call(arg1, arg2, arg3): > pass > kwargs =3D {"arg3": 3, "arg2": "two"} > test_var_args_call(1, **kwargs) > {code} > Essentially the hash contents fill in any arguments not explicitly specif= ied. > For the case where arguments are specified in addition to the hash, you m= ay need to decide on a good syntax, e.g.: > {code:java} > <@myMacro arg1=3D"value1" **myHash/>{code} > This example doesn't have much precedent in freemarker syntax but is fair= ly understandable. > Although we don't need it nearly as much, the same could be done with lis= ts and function arguments: > {code:java} > <#function myFunc arg1 arg2>... > <#assign myList =3D ["val1", "val2"]> > ${myFunc(*myList)} > <#assign myList =3D ["val2"]> > ${myFunc("val1", *myList)} > {code} > Again similar to Python: > {code:java} > def test_var_args_call(arg1, arg2, arg3): > pass > args =3D ("two", 3) > test_var_args_call(1, *args) > {code} > You might want this for consistency, although in practice the hash expans= ion will be many times more useful to us. > If there's a lack a manpower I could try to see what I can do digging int= o the source, but wanted to bring this up for discussion first. It doesn't = appear hard to implement to dump a hash into the macro args map, but there = is defining the syntax. > We use Freemarker 2.3.28 at the moment. > Thank you -- This message was sent by Atlassian Jira (v8.3.4#803005)