package main

import "fmt"

// DataContainer结构体用于包含和操作map[string][]interface{}
type DataContainer struct {
	Data map[string][]interface{}
}

// NewDataContainer创建DataContainer的实例
func NewDataContainer() *DataContainer {
	return &DataContainer{
		Data: make(map[string][]interface{}),
	}
}

// AppendToSlice向指定key对应的切片中追加元素
func (dc *DataContainer) AppendToSlice(key string, elements ...interface{}) {
	if _, ok := dc.Data[key]; !ok {
		dc.Data[key] = []interface{}{}
	}
	dc.Data[key] = append(dc.Data[key], elements...)
}

// UpdateSliceElement更新指定key对应的切片中指定索引位置的元素
func (dc *DataContainer) UpdateSliceElement(key string, index int, newElement interface{}) bool {
	if slice, ok := dc.Data[key]; ok {
		if index >= 0 && index < len(slice) {
			slice[index] = newElement
			dc.Data[key] = slice
			return true
		}
	}
	return false
}

// DeleteSliceElement删除指定key对应的切片中指定索引位置的元素
func (dc *DataContainer) DeleteSliceElement(key string, index int) bool {
	if slice, ok := dc.Data[key]; ok {
		if index >= 0 && index < len(slice) {
			dc.Data[key] = append(slice[:index], slice[index+1:]...)
			return true
		}
	}
	return false
}

// DeleteValue从指定key对应的切片中删除指定的interface{}值
func (dc *DataContainer) DeleteValue(key string, target interface{}) bool {
	if slice, ok := dc.Data[key]; ok {
		var newSlice []interface{}
		for _, element := range slice {
			if element != target {
				newSlice = append(newSlice, element)
			}
		}
		if len(newSlice) < len(slice) {
			dc.Data[key] = newSlice
			return true
		}
	}
	return false
}
func main() {
	container := NewDataContainer()
	// 向名为"list1"的切片中追加元素
	container.AppendToSlice("list1", 1, 2, 3, 2, 4)
	container.AppendToSlice("list1", 5)
	container.AppendToSlice("list1", "aaaa")
	fmt.Println("After appending:", container.Data)

	// 删除"list1"切片中的值2
	deleted := container.DeleteValue("list1", "aaaa")
	if deleted {
		fmt.Println("After deleting value:", container.Data)
	}

	fmt.Println(container.Data)
}
