<%-
import converter_package

model = type.map[:model].type
gen_type = type.map[:gen_type].type
mappings = type.memo['mappings'] || []
requires_context = mappings.any?(&:requires_context)
arg_def_prefix = requires_context ? 'ctx context.Context, ' : nil
arg_prefix = requires_context ? 'ctx, ' : nil

model_slice = model.package.types.detect do |t|
  t.is_a?(GoonModelGen::Golang::NamedSlice) &&
  (
    (t.base_type == model) ||
    (t.ptr_slice? && (t.base_type.target == model))
  )
end

if model_slice
  element_is_ptr = model_slice.ptr_slice?
  plural_model_type_name = model_slice.qualified_name
else
  element_is_ptr =
    !type.memo['model_slice_with_ptr'].nil? ? type.memo['model_slice_with_ptr'] :
    model.fields.any?{|f| !f.tags['goon'].nil? }
  plural_model_type_name = (element_is_ptr ? '[]*' : '[]') + model.qualified_name
end
-%>

func <%= gen_type.name %>SliceToModelSlice(<%= arg_def_prefix %>payloads *[]*<%= gen_type.qualified_name(dependencies) %>) (*<%= plural_model_type_name %>, error) {
	if payloads == nil {
		return nil, <%= converter_package.name %>.NoPayloadGiven
	}

  s := <%= plural_model_type_name %>{}
	for _, payload := range *payloads {
		m, err := <%= gen_type.name %>ToModel(<%= arg_prefix %>payload)
		if err != nil {
			return nil, err
		}
		s = append(s, <%= element_is_ptr ? '' : '*' %>m)
	}

	return &s, nil
}