// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package extensions

import (
	
	
	
	
	

	
	
	
	
)

// Bool8Type represents a logical boolean that is stored using 8 bits.
type Bool8Type struct {
	arrow.ExtensionBase
}

// NewBool8Type creates a new Bool8Type with the underlying storage type set correctly to Int8.
func () *Bool8Type {
	return &Bool8Type{ExtensionBase: arrow.ExtensionBase{Storage: arrow.PrimitiveTypes.Int8}}
}

func ( *Bool8Type) () reflect.Type { return reflect.TypeOf(Bool8Array{}) }

func ( *Bool8Type) ( arrow.DataType,  string) (arrow.ExtensionType, error) {
	if !arrow.TypeEqual(, arrow.PrimitiveTypes.Int8) {
		return nil, fmt.Errorf("invalid storage type for Bool8Type: %s", .Name())
	}
	return NewBool8Type(), nil
}

func ( *Bool8Type) ( arrow.ExtensionType) bool {
	return .ExtensionName() == .ExtensionName()
}

func ( *Bool8Type) () string { return "arrow.bool8" }

func ( *Bool8Type) () string { return "" }

func ( *Bool8Type) () string { return fmt.Sprintf("extension<%s>", .ExtensionName()) }

func (*Bool8Type) ( memory.Allocator) array.Builder {
	return NewBool8Builder()
}

// Bool8Array is logically an array of boolean values but uses
// 8 bits to store values instead of 1 bit as in the native BooleanArray.
type Bool8Array struct {
	array.ExtensionArrayBase
}

func ( *Bool8Array) () string {
	var  strings.Builder
	.WriteString("[")
	for  := 0;  < .Len(); ++ {
		if  > 0 {
			.WriteString(" ")
		}
		switch {
		case .IsNull():
			.WriteString(array.NullValueStr)
		default:
			fmt.Fprintf(&, "%v", .Value())
		}
	}
	.WriteString("]")
	return .String()
}

func ( *Bool8Array) ( int) bool {
	return .Storage().(*array.Int8).Value() != 0
}

func ( *Bool8Array) () []bool {
	 := .Storage().(*array.Int8).Int8Values()
	return unsafe.Slice((*bool)(unsafe.Pointer(unsafe.SliceData())), len())
}

func ( *Bool8Array) ( int) string {
	switch {
	case .IsNull():
		return array.NullValueStr
	default:
		return fmt.Sprint(.Value())
	}
}

func ( *Bool8Array) () ([]byte, error) {
	 := make([]interface{}, .Len())
	for  := 0;  < .Len(); ++ {
		if .IsValid() {
			[] = .Value()
		}
	}
	return json.Marshal()
}

func ( *Bool8Array) ( int) interface{} {
	if .IsNull() {
		return nil
	}
	return .Value()
}

// boolToInt8 performs the simple scalar conversion of bool to the canonical int8
// value for the Bool8Type.
func boolToInt8( bool) int8 {
	var  int8
	if  {
		 = 1
	}
	return 
}

// Bool8Builder is a convenience builder for the Bool8 extension type,
// allowing arrays to be built with boolean values rather than the underlying storage type.
type Bool8Builder struct {
	*array.ExtensionBuilder
}

// NewBool8Builder creates a new Bool8Builder, exposing a convenient and efficient interface
// for writing boolean values to the underlying int8 storage array.
func ( memory.Allocator) *Bool8Builder {
	return &Bool8Builder{ExtensionBuilder: array.NewExtensionBuilder(, NewBool8Type())}
}

func ( *Bool8Builder) ( bool) {
	.ExtensionBuilder.Builder.(*array.Int8Builder).Append(boolToInt8())
}

func ( *Bool8Builder) ( bool) {
	.ExtensionBuilder.Builder.(*array.Int8Builder).UnsafeAppend(boolToInt8())
}

func ( *Bool8Builder) ( string) error {
	if  == array.NullValueStr {
		.AppendNull()
		return nil
	}

	,  := strconv.ParseBool()
	if  != nil {
		return 
	}

	.Append()
	return nil
}

func ( *Bool8Builder) ( []bool,  []bool) {
	 := unsafe.Slice((*int8)(unsafe.Pointer(unsafe.SliceData())), len())
	.ExtensionBuilder.Builder.(*array.Int8Builder).AppendValues(, )
}

func ( *Bool8Builder) ( *json.Decoder) error {
	,  := .Token()
	if  != nil {
		return 
	}

	switch v := .(type) {
	case bool:
		.Append()
		return nil
	case string:
		return .AppendValueFromString()
	case int8:
		.ExtensionBuilder.Builder.(*array.Int8Builder).Append()
		return nil
	case nil:
		.AppendNull()
		return nil
	default:
		return &json.UnmarshalTypeError{
			Value:  fmt.Sprint(),
			Type:   reflect.TypeOf([]byte{}),
			Offset: .InputOffset(),
			Struct: "Bool8Builder",
		}
	}
}

func ( *Bool8Builder) ( *json.Decoder) error {
	for .More() {
		if  := .UnmarshalOne();  != nil {
			return 
		}
	}
	return nil
}

var (
	_ arrow.ExtensionType          = (*Bool8Type)(nil)
	_ array.CustomExtensionBuilder = (*Bool8Type)(nil)
	_ array.ExtensionArray         = (*Bool8Array)(nil)
	_ array.Builder                = (*Bool8Builder)(nil)
)