<commit_before>/*
Copyright (c) 2020 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package component

import (
	"encoding/json"
	"fmt"

	"k8s.io/apimachinery/pkg/runtime"

	"github.com/vmware-tanzu/octant/internal/util/kubernetes"
	"github.com/vmware-tanzu/octant/pkg/store"
)

// Editor is an editor component.
//
// +octant:component
type Editor struct {
	Base
	Config EditorConfig `json:"config"`
}

// EditorConfig is configuration for Editor.
type EditorConfig struct {
	Value        string            `json:"value"`
	ReadOnly     bool              `json:"readOnly"`
	Metadata     map[string]string `json:"metadata"`
	SubmitAction string            `json:"submitAction"`
	SubmitLabel  string            `json:"submitLabel"`
}

// NewEditor creates an instance of an editor component.
func NewEditor(title []TitleComponent, value string, readOnly bool) *Editor {
	return &Editor{
		Base: newBase(TypeEditor, title),
		Config: EditorConfig{
			Value:    value,
			ReadOnly: readOnly,
		},
	}
}

func (e *Editor) SetValueFromObject(object runtime.Object) error {
	s, err := kubernetes.SerializeToString(object)
	if err != nil {
		return fmt.Errorf("serialize object: %w", err)
	}

	e.Config.Value = s<commit_msg>Editor submitAction and submitLabel are optional (#1283)<commit_after>/*
Copyright (c) 2020 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package component

import (
	"encoding/json"
	"fmt"

	"k8s.io/apimachinery/pkg/runtime"

	"github.com/vmware-tanzu/octant/internal/util/kubernetes"
	"github.com/vmware-tanzu/octant/pkg/store"
)

// Editor is an editor component.
//
// +octant:component
type Editor struct {
	Base
	Config EditorConfig `json:"config"`
}

// EditorConfig is configuration for Editor.
type EditorConfig struct {
	Value        string            `json:"value"`
	ReadOnly     bool              `json:"readOnly"`
	Metadata     map[string]string `json:"metadata"`
	SubmitAction string            `json:"submitAction,omitempty"`
	SubmitLabel  string            `json:"submitLabel,omitempty"`
}

// NewEditor creates an instance of an editor component.
func NewEditor(title []TitleComponent, value string, readOnly bool) *Editor {
	return &Editor{
		Base: newBase(TypeEditor, title),
		Config: EditorConfig{
			Value:    value,
			ReadOnly: readOnly,
		},
	}
}

func (e *Editor) SetValueFromObject(object runtime.Object) error {
	s, err := kubernetes.SerializeToString(object)
	if err != nil {
		return fmt.Errorf("serialize object: %w", err)
	}

	e.Config.Value = s
{'fraction_non_alphanumeric': 0.11863088292493193, 'fraction_numerical': 0.007001166861143524, 'mean_word_length': 3.6509945750452077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1656587', 'n_tokens_mistral': 831, 'n_tokens_neox': 798, 'n_words': 209}