<commit_before>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package status

import (
	"github.com/juju/juju/workload"
)

type listFormatter struct {
	payloads      []workload.Payload
	compatVersion int
}

func newListFormatter(payloads []workload.Payload, compatVersion int) *listFormatter {
	lf := listFormatter{
		payloads:      payloads,
		compatVersion: compatVersion,
	}
	return &lf
}

func (lf *listFormatter) format() []formattedPayload {
	if lf.payloads == nil {
		return nil
	}

	var formatted []formattedPayload
	for _, payload := range lf.payloads {
		formatted = append(formatted, lf.formatPayload(payload))
	}
	return formatted
}

func (lf *listFormatter) formatPayload(payload workload.Payload) formattedPayload {
	tags := make([]string, len(payload.Tags))
	copy(tags, payload.Tags)
	return formattedPayload{
		Unit:    payload.Unit,
		Machine: payload.Machine,
		ID:      payload.ID,
		Type:    payload.Type,
		Class:   payload.Name,
		Tags:    tags,
		Status:  payload.Status,
	}
}<commit_msg>Do not format an empty Payload.Tags.<commit_after>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package status

import (
	"github.com/juju/juju/workload"
)

type listFormatter struct {
	payloads      []workload.Payload
	compatVersion int
}

func newListFormatter(payloads []workload.Payload, compatVersion int) *listFormatter {
	lf := listFormatter{
		payloads:      payloads,
		compatVersion: compatVersion,
	}
	return &lf
}

func (lf *listFormatter) format() []formattedPayload {
	if lf.payloads == nil {
		return nil
	}

	var formatted []formattedPayload
	for _, payload := range lf.payloads {
		formatted = append(formatted, lf.formatPayload(payload))
	}
	return formatted
}

func (lf *listFormatter) formatPayload(payload workload.Payload) formattedPayload {
	var tags []string
	if len(payload.Tags) > 0 {
		tags = make([]string, len(payload.Tags))
		copy(tags, payload.Tags)
	}
	return formattedPayload{
		Unit:    payload.Unit,
		Machine: payload.Machine,
		ID:      payload.ID,
		Type:    payload.Type,
		Class:   payload.Name,
		Tags:    tags,
		// TODO(ericsnow) Explicitly convert to a string?
		Status: payload.Status,
	}
}
{'fraction_non_alphanumeric': 0.10798750557786703, 'fraction_numerical': 0.0049085229808121375, 'mean_word_length': 4.038202247191011, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2618851', 'n_tokens_mistral': 778, 'n_tokens_neox': 718, 'n_words': 193}