Navigation Menu

Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
Enable bare strings for noms (struct|set|list|map) arguments.
Browse files Browse the repository at this point in the history
E.g., `noms map set foo bar` rather than `noms map set '"foo"' '"bar"'`.

This required changing tons.
  • Loading branch information
aboodman committed Mar 12, 2018
1 parent 8c1eca4 commit 15c4e45
Show file tree
Hide file tree
Showing 51 changed files with 119 additions and 79 deletions.
5 changes: 4 additions & 1 deletion cmd/noms/noms.go
Expand Up @@ -12,13 +12,15 @@ import (
"strings"
"time"

"github.com/attic-labs/kingpin"

"github.com/attic-labs/noms/cmd/noms/splore"
"github.com/attic-labs/noms/cmd/util"
"github.com/attic-labs/noms/go/util/exit"
"github.com/attic-labs/noms/go/util/profile"
"github.com/attic-labs/noms/go/util/verbose"

flag "github.com/juju/gnuflag"
"gopkg.in/alecthomas/kingpin.v2"
)

var commands = []*util.Command{
Expand Down Expand Up @@ -65,6 +67,7 @@ func usageString() string {

func main() {
// allow short (-h) help
kingpin.EnableFileExpansion = false
kingpin.CommandLine.HelpFlag.Short('h')
noms := kingpin.New("noms", usageString())

Expand Down
3 changes: 2 additions & 1 deletion cmd/noms/noms_blob.go
Expand Up @@ -8,9 +8,10 @@ import (
"runtime"
"strconv"

"github.com/attic-labs/kingpin"

"github.com/attic-labs/noms/cmd/util"
"github.com/attic-labs/noms/go/d"
"gopkg.in/alecthomas/kingpin.v2"
)

func nomsBlob(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
Expand Down
10 changes: 3 additions & 7 deletions cmd/noms/noms_list.go
Expand Up @@ -7,13 +7,13 @@ package main
import (
"fmt"

"github.com/attic-labs/kingpin"

"github.com/attic-labs/noms/cmd/util"
"github.com/attic-labs/noms/go/d"
"github.com/attic-labs/noms/go/diff"
"github.com/attic-labs/noms/go/spec"
"github.com/attic-labs/noms/go/types"

"gopkg.in/alecthomas/kingpin.v2"
)

func nomsList(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
Expand Down Expand Up @@ -106,14 +106,10 @@ func applyListInserts(sp spec.Spec, rootVal types.Value, basePath types.Path, po
db := sp.GetDatabase()
patch := diff.Patch{}
for i := 0; i < len(args); i++ {
vp, err := spec.NewAbsolutePath(args[i])
vv, err := argumentToValue(args[i], db)
if err != nil {
d.CheckError(fmt.Errorf("Invalid value: %s at position %d: %s", args[i], i, err))
}
vv := vp.Resolve(db)
if vv == nil {
d.CheckError(fmt.Errorf("Invalid value: %s at position %d", args[i], i))
}
patch = append(patch, diff.Difference{
Path: append(basePath, types.NewIndexPath(types.Number(pos+uint64(i)))),
ChangeType: types.DiffChangeAdded,
Expand Down
10 changes: 3 additions & 7 deletions cmd/noms/noms_map.go
Expand Up @@ -7,13 +7,13 @@ package main
import (
"fmt"

"github.com/attic-labs/kingpin"

"github.com/attic-labs/noms/cmd/util"
"github.com/attic-labs/noms/go/d"
"github.com/attic-labs/noms/go/diff"
"github.com/attic-labs/noms/go/spec"
"github.com/attic-labs/noms/go/types"

"gopkg.in/alecthomas/kingpin.v2"
)

func nomsMap(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
Expand Down Expand Up @@ -90,14 +90,10 @@ func applyMapEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, args
patch := diff.Patch{}
for i := 0; i < len(args); i += 2 {
kp := parseKeyPart(args, i)
vp, err := spec.NewAbsolutePath(args[i+1])
vv, err := argumentToValue(args[i+1], db)
if err != nil {
d.CheckError(fmt.Errorf("Invalid value: %s at position %d: %s", args[i+1], i+1, err))
}
vv := vp.Resolve(db)
if vv == nil {
d.CheckError(fmt.Errorf("Invalid value: %s at position %d", args[i+1], i+1))
}
patch = append(patch, diff.Difference{
Path: append(basePath, kp),
ChangeType: types.DiffChangeModified,
Expand Down
57 changes: 49 additions & 8 deletions cmd/noms/noms_set.go
Expand Up @@ -5,15 +5,18 @@
package main

import (
"bytes"
"fmt"
"strconv"

"github.com/attic-labs/kingpin"

"github.com/attic-labs/noms/cmd/util"
"github.com/attic-labs/noms/go/d"
"github.com/attic-labs/noms/go/datas"
"github.com/attic-labs/noms/go/diff"
"github.com/attic-labs/noms/go/spec"
"github.com/attic-labs/noms/go/types"

"gopkg.in/alecthomas/kingpin.v2"
)

func nomsSet(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
Expand Down Expand Up @@ -76,13 +79,9 @@ func applySetEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, ct ty
db := sp.GetDatabase()
patch := diff.Patch{}
for i := 0; i < len(args); i++ {
vp, err := spec.NewAbsolutePath(args[i])
vv, err := argumentToValue(args[i], db)
if err != nil {
d.CheckError(fmt.Errorf("Invalid value: %s at position %d: %s", args[i], i, err))
}
vv := vp.Resolve(db)
if vv == nil {
d.CheckError(fmt.Errorf("Invalid value: %s at position %d", args[i], i))
d.CheckErrorNoUsage(err)
}
var pp types.PathPart
if types.ValueCanBePathIndex(vv) {
Expand All @@ -102,3 +101,45 @@ func applySetEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, ct ty
}
appplyPatch(sp, rootVal, basePath, patch)
}

func argumentToValue(arg string, db datas.Database) (types.Value, error) {
d.PanicIfTrue(arg == "")

if arg == "true" {
return types.Bool(true), nil
}
if arg == "false" {
return types.Bool(false), nil
}
if arg[0] == '"' {
buf := bytes.Buffer{}
for i := 1; i < len(arg); i++ {
c := arg[i]
if c == '"' {
if i != len(arg)-1 {
break
}
return types.String(buf.String()), nil
}
if c == '\\' {
i++
c = arg[i]
if c != '\\' && c != '"' {
return nil, fmt.Errorf("Invalid string argument: %s: Only '\\' and '\"' can be escaped")
}
}
buf.WriteByte(c)
}
return nil, fmt.Errorf("Invalid string argument: %s", arg)
}
if arg[0] == '@' {
p, err := spec.NewAbsolutePath(arg[1:])
d.PanicIfError(err)
return p.Resolve(db), nil
}
if n, err := strconv.ParseFloat(arg, 64); err == nil {
return types.Number(n), nil
}

return types.String(arg), nil
}
2 changes: 1 addition & 1 deletion cmd/noms/noms_stats.go
Expand Up @@ -7,7 +7,7 @@ package main
import (
"fmt"

kingpin "gopkg.in/alecthomas/kingpin.v2"
"github.com/attic-labs/kingpin"

"github.com/attic-labs/noms/cmd/util"
"github.com/attic-labs/noms/go/config"
Expand Down
10 changes: 3 additions & 7 deletions cmd/noms/noms_struct.go
Expand Up @@ -7,13 +7,13 @@ package main
import (
"fmt"

"github.com/attic-labs/kingpin"

"github.com/attic-labs/noms/cmd/util"
"github.com/attic-labs/noms/go/d"
"github.com/attic-labs/noms/go/diff"
"github.com/attic-labs/noms/go/spec"
"github.com/attic-labs/noms/go/types"

"gopkg.in/alecthomas/kingpin.v2"
)

func nomsStruct(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
Expand Down Expand Up @@ -109,14 +109,10 @@ func applyStructEdits(sp spec.Spec, rootVal types.Value, basePath types.Path, ar
if !types.IsValidStructFieldName(args[i]) {
d.CheckError(fmt.Errorf("Invalid field name: %s at position: %d", args[i], i))
}
p, err := spec.NewAbsolutePath(args[i+1])
nv, err := argumentToValue(args[i+1], db)
if err != nil {
d.CheckError(fmt.Errorf("Invalid field value: %s at position %d: %s", args[i+1], i+1, err))
}
nv := p.Resolve(db)
if nv == nil {
d.CheckError(fmt.Errorf("Invalid field value: %s at position: %d", args[i+1], i+1))
}
patch = append(patch, diff.Difference{
Path: append(basePath, types.FieldPath{Name: args[i]}),
ChangeType: types.DiffChangeModified,
Expand Down
2 changes: 1 addition & 1 deletion cmd/noms/splore/noms_splore.go
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"reflect"

"github.com/attic-labs/kingpin"
"github.com/attic-labs/noms/cmd/util"
"github.com/attic-labs/noms/go/config"
"github.com/attic-labs/noms/go/d"
Expand All @@ -19,7 +20,6 @@ import (
"github.com/attic-labs/noms/go/util/verbose"
humanize "github.com/dustin/go-humanize"
"github.com/skratchdot/open-golang/open"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

var httpServe = http.Serve // override for tests
Expand Down
2 changes: 1 addition & 1 deletion cmd/util/kingpin_command.go
Expand Up @@ -4,7 +4,7 @@

package util

import kingpin "gopkg.in/alecthomas/kingpin.v2"
import "github.com/attic-labs/kingpin"

type KingpinHandler func(input string) (exitCode int)
type KingpinCommand func(*kingpin.Application) (*kingpin.CmdClause, KingpinHandler)
20 changes: 1 addition & 19 deletions go/spec/absolute_path.go
Expand Up @@ -24,10 +24,6 @@ var datasetCapturePrefixRe = regexp.MustCompile("^(" + datas.DatasetRe.String()
// For more on paths, absolute paths, and specs, see:
// https://github.com/attic-labs/noms/blob/master/doc/spelling.md.
type AbsolutePath struct {
// A literal value, like `true` or `42`, or `"foo"`. Only kinds Bool,
// Number, and String are supported. This allows absolute paths to be used
// to specify primitive new data in addition to referencing existing data.
Value types.Value
// Dataset is the dataset this AbsolutePath is rooted at. Only one of
// Dataset and Hash should be set.
Dataset string
Expand Down Expand Up @@ -65,12 +61,6 @@ func NewAbsolutePath(str string) (AbsolutePath, error) {

pathStr = tail[hash.StringLen:]
} else {
idx, h, rem, err := types.ParsePathIndex(str)
if idx != nil && h.IsEmpty() && rem == "" && err == nil {
return AbsolutePath{
Value: idx,
}, nil
}
datasetParts := datasetCapturePrefixRe.FindStringSubmatch(str)
if datasetParts == nil {
return AbsolutePath{}, fmt.Errorf("Invalid dataset name: %s", str)
Expand All @@ -94,10 +84,6 @@ func NewAbsolutePath(str string) (AbsolutePath, error) {

// Resolve returns the Value reachable by 'p' in 'db'.
func (p AbsolutePath) Resolve(db datas.Database) (val types.Value) {
if p.Value != nil {
return p.Value
}

if len(p.Dataset) > 0 {
var ok bool
ds := db.GetDataset(p.Dataset)
Expand All @@ -117,18 +103,14 @@ func (p AbsolutePath) Resolve(db datas.Database) (val types.Value) {
}

func (p AbsolutePath) IsEmpty() bool {
return p.Value == nil && p.Dataset == "" && p.Hash.IsEmpty()
return p.Dataset == "" && p.Hash.IsEmpty()
}

func (p AbsolutePath) String() (str string) {
if p.IsEmpty() {
return ""
}

if p.Value != nil {
return types.EncodedIndexValue(p.Value)
}

if len(p.Dataset) > 0 {
str = p.Dataset
} else if !p.Hash.IsEmpty() {
Expand Down
8 changes: 1 addition & 7 deletions go/spec/absolute_path_test.go
Expand Up @@ -35,7 +35,7 @@ func TestAbsolutePaths(t *testing.T) {
storage := &chunks.MemoryStorage{}
db := datas.NewDatabase(storage.NewView())

s0, s1, s2 := types.String("foo"), types.String("bar"), types.String("")
s0, s1 := types.String("foo"), types.String("bar")
list := types.NewList(db, s0, s1)
emptySet := types.NewSet(db)

Expand Down Expand Up @@ -72,12 +72,6 @@ func TestAbsolutePaths(t *testing.T) {
resolvesTo(s1, "#"+s1.Hash().String())
resolvesTo(s0, "#"+list.Hash().String()+"[0]")
resolvesTo(s1, "#"+list.Hash().String()+"[1]")
resolvesTo(s0, "\"foo\"")
resolvesTo(s2, "\"\"")
resolvesTo(types.Bool(true), "true")
resolvesTo(types.Bool(false), "false")
resolvesTo(types.Number(42), "42")
resolvesTo(types.Number(88.8), "88.8")

resolvesTo(nil, "foo")
resolvesTo(nil, "foo.parents")
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 15c4e45

Please sign in to comment.