func()

in pkg/log/structure/structuredata/structure.go [113:162]


func (y *YamlNodeStructuredData) Value(fieldName string) (any, error) {
	t, err := y.Type()
	if err != nil {
		return nil, err
	}
	switch t {
	case StructuredTypeScalar:
		if fieldName == "" {
			var data any
			err := y.Node.Decode(&data)
			if err != nil {
				return nil, err
			}
			return data, nil
		} else {
			return nil, fmt.Errorf("attempted to read a field %s from a scalar", fieldName)
		}
	case StructuredTypeMap:
		if fieldName == "" {
			return y, nil
		}
		if len(y.Node.Content)%2 == 1 {
			return nil, fmt.Errorf("map node content count must be even")
		}
		for i := 0; i < len(y.Node.Content); i += 2 {
			keyValue := y.Node.Content[i]
			if keyValue.Value == fieldName {
				return &YamlNodeStructuredData{Node: y.Node.Content[i+1]}, nil
			}
		}
		return nil, fmt.Errorf("field not found:%s", fieldName)
	case StructuredTypeArray:
		if fieldName == "" {
			return y, nil
		}
		index, err := strconv.Atoi(fieldName)
		if err != nil {
			return nil, fmt.Errorf("index can't be parsed as an integer")
		}
		if index < 0 {
			return nil, fmt.Errorf("index must not be negative")
		}
		if index >= len(y.Node.Content) {
			return nil, fmt.Errorf("index out of bounds")
		}
		return &YamlNodeStructuredData{Node: y.Node.Content[index]}, nil
	default:
		return nil, fmt.Errorf("unsupported strcuture type %s", t)
	}
}