Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New YAML loader to support configuration location #828

Merged
merged 6 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ require (
gopkg.in/ini.v1 v1.67.0 // Apache 2.0
)

require gopkg.in/yaml.v3 v3.0.1

require (
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
Expand Down Expand Up @@ -60,5 +62,4 @@ require (
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
13 changes: 13 additions & 0 deletions libs/config/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config

import "fmt"

type Location struct {
File string
Line int
Column int
}

func (l Location) String() string {
return fmt.Sprintf("%s:%d:%d", l.File, l.Line, l.Column)
}
13 changes: 13 additions & 0 deletions libs/config/location_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config_test

import (
"testing"

"github.com/databricks/cli/libs/config"
"github.com/stretchr/testify/assert"
)

func TestLocation(t *testing.T) {
loc := config.Location{File: "file", Line: 1, Column: 2}
assert.Equal(t, "file:1:2", loc.String())
}
109 changes: 109 additions & 0 deletions libs/config/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package config

import "time"

type Value struct {
v any
l Location

// Whether or not this value is an anchor.
// If this node doesn't map to a type, we don't need to warn about it.
anchor bool
}

// NilValue is equal to the zero-value of Value.
var NilValue = Value{}

// NewValue constructs a new Value with the given value and location.
func NewValue(v any, loc Location) Value {
return Value{
v: v,
l: loc,
}
}

func (v Value) AsMap() (map[string]Value, bool) {
m, ok := v.v.(map[string]Value)
return m, ok
}

func (v Value) Location() Location {
return v.l
}

func (v Value) AsAny() any {
switch vv := v.v.(type) {
case map[string]Value:
m := make(map[string]any)
for k, v := range vv {
m[k] = v.AsAny()
}
return m
case []Value:
a := make([]any, len(vv))
for i, v := range vv {
a[i] = v.AsAny()
}
return a
case string:
return vv
case bool:
return vv
case int:
return vv
case int32:
return vv
case int64:
return vv
case float32:
return vv
case float64:
return vv
case time.Time:
return vv
case nil:
return nil
default:
panic("not handled")
}
pietern marked this conversation as resolved.
Show resolved Hide resolved
}

func (v Value) Get(key string) Value {
m, ok := v.AsMap()
if !ok {
return NilValue
}

vv, ok := m[key]
if !ok {
return NilValue
}

return vv
}

func (v Value) Index(i int) Value {
s, ok := v.v.([]Value)
if !ok {
return NilValue
}

if i < 0 || i >= len(s) {
return NilValue
}

return s[i]
}

func (v Value) MarkAnchor() Value {
return Value{
v: v.v,
l: v.l,

anchor: true,
}
}

func (v Value) IsAnchor() bool {
return v.anchor
}
37 changes: 37 additions & 0 deletions libs/config/value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package config_test

import (
"testing"

"github.com/databricks/cli/libs/config"
"github.com/stretchr/testify/assert"
)

func TestValueIsAnchor(t *testing.T) {
var zero config.Value
assert.False(t, zero.IsAnchor())
mark := zero.MarkAnchor()
assert.True(t, mark.IsAnchor())
}

func TestValueAsMap(t *testing.T) {
var zeroValue config.Value
m, ok := zeroValue.AsMap()
assert.False(t, ok)
assert.Nil(t, m)

var intValue = config.NewValue(1, config.Location{})
m, ok = intValue.AsMap()
assert.False(t, ok)
assert.Nil(t, m)

var mapValue = config.NewValue(
map[string]config.Value{
"key": config.NewValue("value", config.Location{File: "file", Line: 1, Column: 2}),
},
config.Location{File: "file", Line: 1, Column: 2},
)
m, ok = mapValue.AsMap()
assert.True(t, ok)
assert.Len(t, m, 1)
}
Loading