Skip to content

Commit

Permalink
Split into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Mar 21, 2024
1 parent 06a3cd8 commit ba491c5
Show file tree
Hide file tree
Showing 11 changed files with 1,721 additions and 1,545 deletions.
153 changes: 153 additions & 0 deletions client/normal_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package client

import (
"time"

"golang.org/x/exp/constraints"
)

type baseArrayNormalValue[T any] struct {
NormalVoid
val T
}

func (v baseArrayNormalValue[T]) Any() any {
return v.val
}

func (v baseArrayNormalValue[T]) Unwrap() any {
return v.val
}

func (v baseArrayNormalValue[T]) IsArray() bool {
return true
}

func newBaseArrayNormalValue[T any](val T) baseArrayNormalValue[T] {
return baseArrayNormalValue[T]{val: val}
}

type normalBoolArray struct {
baseArrayNormalValue[[]bool]
}

func (v normalBoolArray) BoolArray() ([]bool, bool) {
return v.val, true
}

type normalIntArray struct {
baseArrayNormalValue[[]int64]
}

func (v normalIntArray) IntArray() ([]int64, bool) {
return v.val, true
}

type normalFloatArray struct {
baseArrayNormalValue[[]float64]
}

func (v normalFloatArray) FloatArray() ([]float64, bool) {
return v.val, true
}

type normalStringArray struct {
baseArrayNormalValue[[]string]
}

func (v normalStringArray) StringArray() ([]string, bool) {
return v.val, true
}

type normalBytesArray struct {
baseArrayNormalValue[[][]byte]
}

func (v normalBytesArray) BytesArray() ([][]byte, bool) {
return v.val, true
}

type normalTimeArray struct {
baseArrayNormalValue[[]time.Time]
}

func (v normalTimeArray) TimeArray() ([]time.Time, bool) {
return v.val, true
}

type normalDocumentArray struct {
baseArrayNormalValue[[]*Document]
}

func (v normalDocumentArray) DocumentArray() ([]*Document, bool) {
return v.val, true
}

// NewNormalBoolArray creates a new NormalValue that represents a `[]bool` value.
func NewNormalBoolArray(val []bool) NormalValue {
return normalBoolArray{newBaseArrayNormalValue(val)}
}

// NewNormalIntArray creates a new NormalValue that represents a `[]int64` value.
func NewNormalIntArray[T constraints.Integer | constraints.Float](val []T) NormalValue {
return normalIntArray{newBaseArrayNormalValue(normalizeNumArr[int64](val))}
}

// NewNormalFloatArray creates a new NormalValue that represents a `[]float64` value.
func NewNormalFloatArray[T constraints.Integer | constraints.Float](val []T) NormalValue {
return normalFloatArray{newBaseArrayNormalValue(normalizeNumArr[float64](val))}
}

// NewNormalStringArray creates a new NormalValue that represents a `[]string` value.
func NewNormalStringArray[T string | []byte](val []T) NormalValue {
return normalStringArray{newBaseArrayNormalValue(normalizeCharsArr[string](val))}
}

// NewNormalBytesArray creates a new NormalValue that represents a `[][]byte` value.
func NewNormalBytesArray[T string | []byte](val []T) NormalValue {
return normalBytesArray{newBaseArrayNormalValue(normalizeCharsArr[[]byte](val))}
}

// NewNormalTimeArray creates a new NormalValue that represents a `[]time.Time` value.
func NewNormalTimeArray(val []time.Time) NormalValue {
return normalTimeArray{newBaseArrayNormalValue(val)}
}

// NewNormalDocumentArray creates a new NormalValue that represents a `[]*Document` value.
func NewNormalDocumentArray(val []*Document) NormalValue {
return normalDocumentArray{newBaseArrayNormalValue(val)}
}

func normalizeNumArr[R int64 | float64, T constraints.Integer | constraints.Float](val []T) []R {
var v any = val
if arr, ok := v.([]R); ok {
return arr
}
arr := make([]R, len(val))
for i, v := range val {
arr[i] = R(v)
}
return arr
}

func normalizeCharsArr[R string | []byte, T string | []byte](val []T) []R {
var v any = val
if arr, ok := v.([]R); ok {
return arr
}
arr := make([]R, len(val))
for i, v := range val {
arr[i] = R(v)
}
return arr
}
142 changes: 142 additions & 0 deletions client/normal_array_of_nillables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package client

import (
"time"

"github.com/sourcenetwork/immutable"
"golang.org/x/exp/constraints"
)

type normalNillableBoolArray struct {
baseArrayNormalValue[[]immutable.Option[bool]]
}

func (v normalNillableBoolArray) NillableBoolArray() ([]immutable.Option[bool], bool) {
return v.val, true
}

type normalNillableIntArray struct {
baseArrayNormalValue[[]immutable.Option[int64]]
}

func (v normalNillableIntArray) NillableIntArray() ([]immutable.Option[int64], bool) {
return v.val, true
}

type normalNillableFloatArray struct {
baseArrayNormalValue[[]immutable.Option[float64]]
}

func (v normalNillableFloatArray) NillableFloatArray() ([]immutable.Option[float64], bool) {
return v.val, true
}

type normalNillableStringArray struct {
baseArrayNormalValue[[]immutable.Option[string]]
}

func (v normalNillableStringArray) NillableStringArray() ([]immutable.Option[string], bool) {
return v.val, true
}

type normalNillableBytesArray struct {
baseArrayNormalValue[[]immutable.Option[[]byte]]
}

func (v normalNillableBytesArray) NillableBytesArray() ([]immutable.Option[[]byte], bool) {
return v.val, true
}

type normalNillableTimeArray struct {
baseArrayNormalValue[[]immutable.Option[time.Time]]
}

func (v normalNillableTimeArray) NillableTimeArray() ([]immutable.Option[time.Time], bool) {
return v.val, true
}

type normalNillableDocumentArray struct {
baseArrayNormalValue[[]immutable.Option[*Document]]
}

func (v normalNillableDocumentArray) NillableDocumentArray() ([]immutable.Option[*Document], bool) {
return v.val, true
}

// NewNormalNillableBoolNillableArray creates a new NormalValue that represents a
// `immutable.Option[[]immutable.Option[bool]]` value.
func NewNormalNillableBoolArray(val []immutable.Option[bool]) NormalValue {
return normalNillableBoolArray{newBaseArrayNormalValue(val)}
}

// NewNormalNillableIntArray creates a new NormalValue that represents a `[]immutable.Option[int64]` value.
func NewNormalNillableIntArray[T constraints.Integer | constraints.Float](val []immutable.Option[T]) NormalValue {
return normalNillableIntArray{newBaseArrayNormalValue(normalizeNillableNumArr[int64](val))}
}

// NewNormalNillableFloatArray creates a new NormalValue that represents a `[]immutable.Option[float64]` value.
func NewNormalNillableFloatArray[T constraints.Integer | constraints.Float](
val []immutable.Option[T],
) NormalValue {
return normalNillableFloatArray{newBaseArrayNormalValue(normalizeNillableNumArr[float64](val))}
}

// NewNormalNillableStringArray creates a new NormalValue that represents a `[]immutable.Option[string]` value.
func NewNormalNillableStringArray[T string | []byte](val []immutable.Option[T]) NormalValue {
return normalNillableStringArray{newBaseArrayNormalValue(normalizeNillableCharsArr[string](val))}
}

// NewNormalNillableBytesArray creates a new NormalValue that represents a `[]immutable.Option[[]byte]` value.
func NewNormalNillableBytesArray[T string | []byte](val []immutable.Option[T]) NormalValue {
return normalNillableBytesArray{newBaseArrayNormalValue(normalizeNillableCharsArr[[]byte](val))}
}

// NewNormalNillableTimeArray creates a new NormalValue that represents a `[]immutable.Option[time.Time]` value.
func NewNormalNillableTimeArray(val []immutable.Option[time.Time]) NormalValue {
return normalNillableTimeArray{newBaseArrayNormalValue(val)}
}

// NewNormalNillableDocumentArray creates a new NormalValue that represents a `[]immutable.Option[*Document]` value.
func NewNormalNillableDocumentArray(val []immutable.Option[*Document]) NormalValue {
return normalNillableDocumentArray{newBaseArrayNormalValue(val)}
}

func normalizeNillableNumArr[R int64 | float64, T constraints.Integer | constraints.Float](
val []immutable.Option[T],
) []immutable.Option[R] {
var v any = val
if arr, ok := v.([]immutable.Option[R]); ok {
return arr
}
arr := make([]immutable.Option[R], len(val))
for i, v := range val {
arr[i] = normalizeNillableNum[R](v)
}
return arr
}

func normalizeNillableCharsArr[R string | []byte, T string | []byte](val []immutable.Option[T]) []immutable.Option[R] {
var v any = val
if arr, ok := v.([]immutable.Option[R]); ok {
return arr
}
arr := make([]immutable.Option[R], len(val))
for i, v := range val {
if v.HasValue() {
arr[i] = immutable.Some(R(v.Value()))
} else {
arr[i] = immutable.None[R]()
}
}
return arr
}
Loading

0 comments on commit ba491c5

Please sign in to comment.