/* ************************************************************************
#
#  DivConq
#
#  http://divconq.com/
#
#  Copyright:
#    Copyright 2012 eTimeline, LLC. All rights reserved.
#
#  License:
#    See the license.txt file in the project's top-level directory for details.
#
#  Authors:
#    * Andy White
#
************************************************************************ */
package divconq.script.mutator;

import divconq.script.Activity;
import divconq.script.IMutator;
import divconq.script.StackEntry;
import divconq.struct.RecordStruct;
import divconq.struct.Struct;
import divconq.util.StringUtil;
import divconq.xml.XElement;

public class SetFieldMutator implements IMutator {
	// only works on scalars

	@Override
	public void mutate(Activity activity, StackEntry stack, XElement code, Struct dest) {
		// resolve value only works with scalars
		if ((dest == null) || !(dest instanceof RecordStruct)) {
			// TODO log
			return;
		}
		
		RecordStruct dr = (RecordStruct)dest;
		
		String name = stack.stringFromSource(code, "Name");
		
		if (StringUtil.isEmpty(name)) {
			// TODO log
			return;
		}
		
		Struct sref = stack.refFromSource(code, "Value", dr.getFieldType(name)); 
		
		dr.setField(name, sref);
	}
}
