Skip to content

Commit

Permalink
refactor: env -> value
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 1, 2023
1 parent c2b603d commit c210469
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 196 deletions.
115 changes: 0 additions & 115 deletions pkg/environment/apply.go

This file was deleted.

47 changes: 0 additions & 47 deletions pkg/environment/appy_test.go

This file was deleted.

50 changes: 25 additions & 25 deletions pkg/environment/environment.go → pkg/template/values.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package template

import (
"fmt"
Expand All @@ -7,30 +7,30 @@ import (
"sync"
)

// Environment represents the environment of your application.
type Environment struct {
// Values represents the environment of your application.
type Values struct {
data map[string]any
mu sync.RWMutex
}

var typeAny = reflect.TypeOf((*any)(nil)).Elem()

// New creates a new environment instance.
func New() *Environment {
return &Environment{
// NewValues creates a new environment instance.
func NewValues() *Values {
return &Values{
data: make(map[string]any),
}
}

// Set sets the value for the specified key in the environment.
func (c *Environment) Set(key string, val any) error {
c.mu.Lock()
defer c.mu.Unlock()
func (vl *Values) Set(key string, val any) error {
vl.mu.Lock()
defer vl.mu.Unlock()

tokens := strings.Split(key, ".")
v := reflect.ValueOf(val)

node := reflect.ValueOf(c.data)
node := reflect.ValueOf(vl.data)
for i, token := range tokens {
t := reflect.ValueOf(token)

Expand All @@ -55,13 +55,13 @@ func (c *Environment) Set(key string, val any) error {

// Get retrieves the value for the specified key from the environment.
// If the key is not present, it returns a default value and a boolean indicating the key's existence.
func (c *Environment) Get(key string) (any, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
func (vl *Values) Get(key string) (any, bool) {
vl.mu.RLock()
defer vl.mu.RUnlock()

tokens := strings.Split(key, ".")

node := reflect.ValueOf(c.data)
node := reflect.ValueOf(vl.data)
for _, token := range tokens {
t := reflect.ValueOf(token)

Expand All @@ -80,27 +80,27 @@ func (c *Environment) Get(key string) (any, bool) {
}

// Delete removes the specified key from the environment.
func (c *Environment) Delete(key string) {
c.mu.Lock()
defer c.mu.Unlock()
func (vl *Values) Delete(key string) {
vl.mu.Lock()
defer vl.mu.Unlock()

tokens := strings.Split(key, ".")
c.deleteRecursive(reflect.ValueOf(c.data), tokens)
vl.deleteRecursive(reflect.ValueOf(vl.data), tokens)
}

// GetData returns a read-only copy of the environment data.
func (c *Environment) GetData() map[string]any {
c.mu.RLock()
defer c.mu.RUnlock()
func (vl *Values) GetData() map[string]any {
vl.mu.RLock()
defer vl.mu.RUnlock()

result := make(map[string]any, len(c.data))
for k, v := range c.data {
result := make(map[string]any, len(vl.data))
for k, v := range vl.data {
result[k] = v
}
return result
}

func (c *Environment) deleteRecursive(node reflect.Value, tokens []string) bool {
func (vl *Values) deleteRecursive(node reflect.Value, tokens []string) bool {
t := reflect.ValueOf(tokens[0])

if node.Kind() != reflect.Map || node.Type().Key().Kind() != reflect.String {
Expand All @@ -121,7 +121,7 @@ func (c *Environment) deleteRecursive(node reflect.Value, tokens []string) bool
return false
}
child = reflect.ValueOf(child.Interface())
if c.deleteRecursive(child, tokens[1:]) {
if vl.deleteRecursive(child, tokens[1:]) {
if child.Len() == 0 {
node.SetMapIndex(t, reflect.Value{})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package template

import (
"fmt"
Expand All @@ -8,30 +8,30 @@ import (
"github.com/stretchr/testify/assert"
)

func TestEnvironment_GetAndSetAndDelete(t *testing.T) {
env := New()
func TestValues_GetAndSetAndDelete(t *testing.T) {
vl := NewValues()

key1 := faker.Word()
key2 := faker.Word()

key := fmt.Sprintf("%s.%s", key1, key2)
value := faker.Word()

env.Set(key, value)
vl.Set(key, value)

r1, ok := env.Get(key)
r1, ok := vl.Get(key)
assert.True(t, ok)
assert.Equal(t, value, r1)

_, ok = env.Get(key1)
_, ok = vl.Get(key1)
assert.True(t, ok)

env.Delete(key)
vl.Delete(key)

r2, ok := env.Get(key)
r2, ok := vl.Get(key)
assert.False(t, ok)
assert.Nil(t, r2)

_, ok = env.Get(key1)
_, ok = vl.Get(key1)
assert.False(t, ok)
}

0 comments on commit c210469

Please sign in to comment.