(InstanceScope scope, List<Object> exprs, ST prototype)
| 873 | // todo: i, i0 not set unless mentioned? map:{k,v | ..}? |
| 874 | |
| 875 | protected ST.AttributeList zip_map(InstanceScope scope, List<Object> exprs, ST prototype) { |
| 876 | if ( exprs==null || prototype==null || exprs.size()==0) { |
| 877 | return null; // do not apply if missing templates or empty values |
| 878 | } |
| 879 | // make everything iterable |
| 880 | for (int i = 0; i <exprs.size(); i++) { |
| 881 | Object attr = exprs.get(i); |
| 882 | if ( attr !=null ) exprs.set(i, convertAnythingToIterator(scope, attr)); |
| 883 | } |
| 884 | |
| 885 | // ensure arguments line up |
| 886 | |
| 887 | int numExprs = exprs.size(); |
| 888 | CompiledST code = prototype.impl; |
| 889 | Map<String, FormalArgument> formalArguments = code.formalArguments; |
| 890 | if ( !code.hasFormalArgs|| formalArguments==null ) { |
| 891 | errMgr.runTimeError(this, scope, ErrorType.MISSING_FORMAL_ARGUMENTS); |
| 892 | return null; |
| 893 | } |
| 894 | |
| 895 | // todo: track formal args not names for efficient filling of locals |
| 896 | String[] formalArgumentNames = formalArguments.keySet().toArray(new String[formalArguments.size()]); |
| 897 | int nformalArgs = formalArgumentNames.length; |
| 898 | if ( prototype.isAnonSubtemplate() ) nformalArgs -= predefinedAnonSubtemplateAttributes.size(); |
| 899 | if ( nformalArgs != numExprs ) { |
| 900 | errMgr.runTimeError(this, |
| 901 | scope, |
| 902 | ErrorType.MAP_ARGUMENT_COUNT_MISMATCH, |
| 903 | numExprs, |
| 904 | nformalArgs); |
| 905 | // TODO just fill first n |
| 906 | // truncate arg list to match smaller size |
| 907 | int shorterSize = Math.min(formalArgumentNames.length, numExprs); |
| 908 | numExprs = shorterSize; |
| 909 | String[] newFormalArgumentNames = new String[shorterSize]; |
| 910 | System.arraycopy(formalArgumentNames, 0, newFormalArgumentNames, 0, shorterSize); |
| 911 | formalArgumentNames = newFormalArgumentNames; |
| 912 | } |
| 913 | |
| 914 | // keep walking while at least one attribute has values |
| 915 | ST.AttributeList results = new ST.AttributeList(); |
| 916 | int i = 0; // iteration number from 0 |
| 917 | while ( true ) { |
| 918 | // get a value for each attribute in list; put into ST instance |
| 919 | int numEmpty = 0; |
| 920 | ST embedded = group.createStringTemplateInternally(prototype); |
| 921 | embedded.rawSetAttribute("i0", i); |
| 922 | embedded.rawSetAttribute("i", i+1); |
| 923 | for (int a = 0; a < numExprs; a++) { |
| 924 | Iterator<?> it = (Iterator<?>)exprs.get(a); |
| 925 | if ( it !=null && it.hasNext() ) { |
| 926 | String argName = formalArgumentNames[a]; |
| 927 | Object iteratedValue = it.next(); |
| 928 | embedded.rawSetAttribute(argName, iteratedValue); |
| 929 | } |
| 930 | else { |
| 931 | numEmpty++; |
| 932 | } |
no test coverage detected