Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions pkg/iac/types/base_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package types

import (
"encoding/json"
)

type BaseValue[T any] struct {
metadata Metadata
value T
}

func defaultValue[T any](value T, m Metadata) BaseValue[T] {
m.isDefault = true
return newValue(value, m)
}

func unresolvableValue[T any](m Metadata) BaseValue[T] {
m.isUnresolvable = true
var zero T
return newValue(zero, m)
}

func explicitValue[T any](value T, m Metadata) BaseValue[T] {
m.isExplicit = true
return newValue(value, m)
}

func testValue[T any](value T) BaseValue[T] {
return newValue(value, NewTestMetadata())
}

func newValue[T any](val T, metadata Metadata) BaseValue[T] {
return BaseValue[T]{
metadata: metadata,
value: val,
}
}

func (v BaseValue[T]) GetMetadata() Metadata {
return v.metadata
}

func (v BaseValue[T]) Value() T {
return v.value
}

func (v BaseValue[T]) GetRawValue() any {
return v.value
}

func (v BaseValue[T]) ToRego() any {
m := v.metadata.ToRego().(map[string]any)
m["value"] = v.value
return m
}

type encodedValue[T any] struct {
Value T `json:"value"`
Metadata Metadata `json:"metadata"`
}

func (v BaseValue[T]) MarshalJSON() ([]byte, error) {
ev := encodedValue[T]{
Value: v.value,
Metadata: v.metadata,
}
return json.Marshal(ev)
}

func (v *BaseValue[T]) UnmarshalJSON(data []byte) error {
var ev encodedValue[T]
if err := json.Unmarshal(data, &ev); err != nil {
return err
}

v.value = ev.Value
v.metadata = ev.Metadata
return nil
}
79 changes: 11 additions & 68 deletions pkg/iac/types/bool.go
Original file line number Diff line number Diff line change
@@ -1,81 +1,33 @@
package types

import (
"encoding/json"
"strings"

"github.com/zclconf/go-cty/cty"
)

type BoolValue struct {
BaseAttribute
value bool
BaseValue[bool]
}

func (b BoolValue) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"value": b.value,
"metadata": b.metadata,
})
func Bool(value bool, m Metadata) BoolValue {
return BoolValue{newValue(value, m)}
}

func (b *BoolValue) UnmarshalJSON(data []byte) error {
var keys map[string]any
if err := json.Unmarshal(data, &keys); err != nil {
return err
}
if keys["value"] != nil {
b.value = keys["value"].(bool)
}
if keys["metadata"] != nil {
raw, err := json.Marshal(keys["metadata"])
if err != nil {
return err
}
var m Metadata
if err := json.Unmarshal(raw, &m); err != nil {
return err
}
b.metadata = m
}
return nil
}

func Bool(value bool, metadata Metadata) BoolValue {
return BoolValue{
value: value,
BaseAttribute: BaseAttribute{metadata: metadata},
}
}

func BoolTest(value bool) BoolValue {
return Bool(value, NewTestMetadata())
}

func BoolDefault(value bool, metadata Metadata) BoolValue {
b := Bool(value, metadata)
b.BaseAttribute.metadata.isDefault = true
return b
func BoolDefault(value bool, m Metadata) BoolValue {
return BoolValue{defaultValue(value, m)}
}

func BoolUnresolvable(m Metadata) BoolValue {
b := Bool(false, m)
b.BaseAttribute.metadata.isUnresolvable = true
return b
}

func BoolExplicit(value bool, metadata Metadata) BoolValue {
b := Bool(value, metadata)
b.BaseAttribute.metadata.isExplicit = true
return b
return BoolValue{unresolvableValue[bool](m)}
}

func (b BoolValue) Value() bool {
return b.value
func BoolExplicit(value bool, m Metadata) BoolValue {
return BoolValue{explicitValue(value, m)}
}

func (b BoolValue) GetRawValue() any {
return b.value
func BoolTest(value bool) BoolValue {
return BoolValue{testValue(value)}
}

func (b BoolValue) IsTrue() bool {
Expand All @@ -93,16 +45,7 @@ func (b BoolValue) IsFalse() bool {
}

func (b BoolValue) Invert() BoolValue {
return BoolValue{
BaseAttribute: b.BaseAttribute,
value: !b.value,
}
}

func (b BoolValue) ToRego() any {
m := b.metadata.ToRego().(map[string]any)
m["value"] = b.Value()
return m
return Bool(!b.value, b.metadata)
}

// BoolFromCtyValue converts a cty.Value to iacTypes.BoolValue.
Expand Down
91 changes: 13 additions & 78 deletions pkg/iac/types/bytes.go
Original file line number Diff line number Diff line change
@@ -1,100 +1,35 @@
package types

import (
"encoding/json"
)

type BytesValue struct {
BaseAttribute
value []byte
}

func (b BytesValue) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"value": b.value,
"metadata": b.metadata,
})
}

func (b *BytesValue) UnmarshalJSON(data []byte) error {
var keys map[string]any
if err := json.Unmarshal(data, &keys); err != nil {
return err
}
if keys["value"] != nil {
raw, err := json.Marshal(keys["value"])
if err != nil {
return err
}
var m []byte
if err := json.Unmarshal(raw, &m); err != nil {
return err
}
b.value = m
}
if keys["metadata"] != nil {
raw, err := json.Marshal(keys["metadata"])
if err != nil {
return err
}
var m Metadata
if err := json.Unmarshal(raw, &m); err != nil {
return err
}
b.metadata = m
}
return nil
}

func (b BytesValue) Value() []byte {
return b.value
}

func (b BytesValue) GetRawValue() any {
return b.value
}

func (b BytesValue) Len() int {
return len(b.value)
}

func (b BytesValue) GetMetadata() Metadata {
return b.metadata
BaseValue[[]byte]
}

func Bytes(value []byte, m Metadata) BytesValue {
return BytesValue{
value: value,
BaseAttribute: BaseAttribute{metadata: m},
}
return BytesValue{newValue(value, m)}
}

func BytesDefault(value []byte, m Metadata) BytesValue {
b := Bytes(value, m)
b.BaseAttribute.metadata.isDefault = true
return b
return BytesValue{defaultValue(value, m)}
}

func BytesExplicit(value []byte, m Metadata) BytesValue {
b := Bytes(value, m)
b.BaseAttribute.metadata.isExplicit = true
return b
return BytesValue{explicitValue(value, m)}
}

func BytesUnresolvable(m Metadata) BytesValue {
b := Bytes(nil, m)
b.BaseAttribute.metadata.isUnresolvable = true
return b
return BytesValue{unresolvableValue[[]byte](m)}
}

func BytesTest(value []byte) BytesValue {
b := Bytes(value, NewTestMetadata())
b.BaseAttribute.metadata.isUnresolvable = true
return b
return BytesValue{testValue(value)}
}

func (b BytesValue) ToRego() any {
m := b.metadata.ToRego().(map[string]any)
m["value"] = string(b.Value())
func (v BytesValue) ToRego() any {
m := v.metadata.ToRego().(map[string]any)
m["value"] = string(v.value)
return m
}

func (v BytesValue) Len() int {
return len(v.value)
}
Loading
Loading