Skip to content
Snippets Groups Projects
Commit 727fc768 authored by bryanl's avatar bryanl
Browse files

pulling in up to ksonnet-lib


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent e570bff8
No related branches found
No related tags found
No related merge requests found
......@@ -232,7 +232,7 @@
"ksonnet-gen/nodemaker",
"ksonnet-gen/printer"
]
revision = "9f27c242ff65da0620f8324cc0cd50c8ef00bf5b"
revision = "152e1979764f713239ca25bcb0f3315e69215119"
[[projects]]
branch = "master"
......
......@@ -54,7 +54,7 @@
[[constraint]]
name = "github.com/ksonnet/ksonnet-lib"
revision = "9f27c242ff65da0620f8324cc0cd50c8ef00bf5b"
revision = "152e1979764f713239ca25bcb0f3315e69215119"
[[constraint]]
name = "github.com/mattn/go-isatty"
......
package astext
import "github.com/google/go-jsonnet/ast"
import (
"regexp"
"github.com/google/go-jsonnet/ast"
"github.com/pkg/errors"
)
// ObjectFields is a slice of ObjectField.
type ObjectFields []ObjectField
......@@ -26,3 +31,32 @@ type Object struct {
// Oneline prints this field on a single line.
Oneline bool
}
var (
// reFieldStr matches a field id that should be enclosed in quotes.
reFieldStr = regexp.MustCompile(`^[A-Za-z]+[A-Za-z0-9\-]*$`)
// reField matches a field id.
reField = regexp.MustCompile(`^[A-Za-z]+[A-Za-z0-9]*$`)
)
// CreateField creates an ObjectField with a name. If the name matches `reFieldStr`, it will
// create an ObjectField with Kind `ObjectFieldStr`. If not, it will create an identifier
// and set the ObjectField kind to `ObjectFieldId`.
func CreateField(name string) (*ObjectField, error) {
of := ObjectField{ObjectField: ast.ObjectField{}}
if reField.MatchString(name) {
id := ast.Identifier(name)
of.Kind = ast.ObjectFieldID
of.Id = &id
} else if reFieldStr.MatchString(name) {
of.Expr1 = &ast.LiteralString{
Value: name,
Kind: ast.StringDouble,
}
of.Kind = ast.ObjectFieldStr
} else {
return nil, errors.Errorf("invalid field name %q", name)
}
return &of, nil
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment