(InstanceScope scope, List<Object> exprs, ST prototype)
| 847 | // todo: i, i0 not set unless mentioned? map:{k,v | ..}? |
| 848 | |
| 849 | protected ST.AttributeList zip_map(InstanceScope scope, List<Object> exprs, ST prototype) { |
| 850 | if ( exprs==null || prototype==null || exprs.size()==0) { |
| 851 | return null; // do not apply if missing templates or empty values |
| 852 | } |
| 853 | // make everything iterable |
| 854 | for (int i = 0; i < exprs.size(); i++) { |
| 855 | Object attr = exprs.get(i); |
| 856 | if ( attr!=null ) exprs.set(i, convertAnythingToIterator(scope, attr)); |
| 857 | } |
| 858 | |
| 859 | // ensure arguments line up |
| 860 | |
| 861 | int numExprs = exprs.size(); |
| 862 | CompiledST code = prototype.impl; |
| 863 | Map<String, FormalArgument> formalArguments = code.formalArguments; |
| 864 | if ( !code.hasFormalArgs || formalArguments==null ) { |
| 865 | errMgr.runTimeError(this, scope, ErrorType.MISSING_FORMAL_ARGUMENTS); |
| 866 | return null; |
| 867 | } |
| 868 | |
| 869 | // todo: track formal args not names for efficient filling of locals |
| 870 | String[] formalArgumentNames = formalArguments.keySet().toArray(new String[formalArguments.size()]); |
| 871 | int nformalArgs = formalArgumentNames.length; |
| 872 | if ( prototype.isAnonSubtemplate() ) nformalArgs -= predefinedAnonSubtemplateAttributes.size(); |
| 873 | if ( nformalArgs!= numExprs ) { |
| 874 | errMgr.runTimeError(this, |
| 875 | scope, |
| 876 | ErrorType.MAP_ARGUMENT_COUNT_MISMATCH, |
| 877 | numExprs, |
| 878 | nformalArgs); |
| 879 | // TODO just fill first n |
| 880 | // truncate arg list to match smaller size |
| 881 | int shorterSize = Math.min(formalArgumentNames.length, numExprs); |
| 882 | numExprs = shorterSize; |
| 883 | String[] newFormalArgumentNames = new String[shorterSize]; |
| 884 | System.arraycopy(formalArgumentNames, 0, newFormalArgumentNames, 0, shorterSize); |
| 885 | formalArgumentNames = newFormalArgumentNames; |
| 886 | } |
| 887 | |
| 888 | // keep walking while at least one attribute has values |
| 889 | ST.AttributeList results = new ST.AttributeList(); |
| 890 | int i = 0; // iteration number from 0 |
| 891 | while ( true ) { |
| 892 | // get a value for each attribute in list; put into ST instance |
| 893 | int numEmpty = 0; |
| 894 | ST embedded = group.createStringTemplateInternally(prototype); |
| 895 | embedded.rawSetAttribute("i0", i); |
| 896 | embedded.rawSetAttribute("i", i+1); |
| 897 | for (int a = 0; a < numExprs; a++) { |
| 898 | Iterator<?> it = (Iterator<?>)exprs.get(a); |
| 899 | if ( it!=null && it.hasNext() ) { |
| 900 | String argName = formalArgumentNames[a]; |
| 901 | Object iteratedValue = it.next(); |
| 902 | embedded.rawSetAttribute(argName, iteratedValue); |
| 903 | } |
| 904 | else { |
| 905 | numEmpty++; |
| 906 | } |
no test coverage detected