mirror of
https://github.com/dolthub/dolt.git
synced 2026-02-11 02:59:34 -06:00
Introduce samples/go/hr: a very basic toy person database (#1804)
This commit is contained in:
3
samples/go/hr/README.md
Normal file
3
samples/go/hr/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# HR
|
||||
|
||||
This is a small command line application that manages a very simple hypothetical hr database.
|
||||
109
samples/go/hr/main.go
Normal file
109
samples/go/hr/main.go
Normal file
@@ -0,0 +1,109 @@
|
||||
// Copyright 2016 The Noms Authors. All rights reserved.
|
||||
// Licensed under the Apache License, version 2.0:
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/attic-labs/noms/go/dataset"
|
||||
"github.com/attic-labs/noms/go/spec"
|
||||
"github.com/attic-labs/noms/go/types"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var dsStr = flag.String("ds", "", "noms dataset to read/write from")
|
||||
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s [flags] [command] [command-args]\n\n", os.Args[0])
|
||||
fmt.Fprintln(os.Stderr, "Flags:")
|
||||
flag.PrintDefaults()
|
||||
fmt.Fprintln(os.Stderr, "\nCommands:")
|
||||
fmt.Fprintln(os.Stderr, "\tadd-person <id> <name> <title>")
|
||||
fmt.Fprintln(os.Stderr, "\tlist-persons")
|
||||
}
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if flag.NArg() == 0 {
|
||||
fmt.Fprintln(os.Stderr, "Not enough arguments")
|
||||
return
|
||||
}
|
||||
|
||||
if *dsStr == "" {
|
||||
fmt.Fprintln(os.Stderr, "Required flag '-ds' not set")
|
||||
return
|
||||
}
|
||||
|
||||
sp, err := spec.ParseDatasetSpec(*dsStr)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
return
|
||||
}
|
||||
|
||||
ds, err := sp.Dataset()
|
||||
defer ds.Database().Close()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Could not create dataset: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch flag.Arg(0) {
|
||||
case "add-person":
|
||||
addPerson(ds)
|
||||
case "list-persons":
|
||||
listPersons(ds)
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", flag.Arg(0))
|
||||
}
|
||||
}
|
||||
|
||||
func addPerson(ds dataset.Dataset) {
|
||||
if flag.NArg() != 4 {
|
||||
fmt.Fprintln(os.Stderr, "Not enough arguments for command add-person")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(flag.Arg(1), 10, 64)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Invalid person-id: %s", flag.Arg(1))
|
||||
return
|
||||
}
|
||||
|
||||
np := types.NewStruct("Person", map[string]types.Value{
|
||||
"id": types.Number(id),
|
||||
"name": types.NewString(flag.Arg(2)),
|
||||
"title": types.NewString(flag.Arg(3)),
|
||||
})
|
||||
|
||||
ds.Commit(getPersons(ds).Set(types.Number(id), np))
|
||||
}
|
||||
|
||||
func listPersons(ds dataset.Dataset) {
|
||||
d := getPersons(ds)
|
||||
if d.Empty() {
|
||||
fmt.Println("No people found")
|
||||
return
|
||||
}
|
||||
|
||||
d.IterAll(func(k, v types.Value) {
|
||||
s := v.(types.Struct)
|
||||
fmt.Printf("%s (id: %d, title: %s)\n",
|
||||
s.Get("name").(types.String).String(),
|
||||
uint64(s.Get("id").(types.Number)),
|
||||
s.Get("title").(types.String).String())
|
||||
})
|
||||
}
|
||||
|
||||
func getPersons(ds dataset.Dataset) types.Map {
|
||||
h, ok := ds.MaybeHead()
|
||||
if ok {
|
||||
return h.Get("value").(types.Map)
|
||||
} else {
|
||||
return types.NewMap()
|
||||
}
|
||||
}
|
||||
49
samples/go/hr/main_test.go
Normal file
49
samples/go/hr/main_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright 2016 The Noms Authors. All rights reserved.
|
||||
// Licensed under the Apache License, version 2.0:
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/attic-labs/noms/samples/go/test_util"
|
||||
"github.com/attic-labs/testify/suite"
|
||||
)
|
||||
|
||||
func TestBasics(t *testing.T) {
|
||||
suite.Run(t, &testSuite{})
|
||||
}
|
||||
|
||||
type testSuite struct {
|
||||
test_util.ClientTestSuite
|
||||
}
|
||||
|
||||
func (s *testSuite) TestRoundTrip() {
|
||||
spec := fmt.Sprintf("ldb:%s::hr", s.LdbDir)
|
||||
out := s.Run(main, []string{"-ds", spec, "list-persons"})
|
||||
s.Equal("No people found\n", out)
|
||||
|
||||
out = s.Run(main, []string{"-ds", spec, "add-person", "42", "Benjamin Kalman", "Programmer, Barista"})
|
||||
s.Equal("", out)
|
||||
|
||||
out = s.Run(main, []string{"-ds", spec, "add-person", "43", "Abigail Boodman", "Chief Architect"})
|
||||
s.Equal("", out)
|
||||
|
||||
out = s.Run(main, []string{"-ds", spec, "list-persons"})
|
||||
s.Equal(`Benjamin Kalman (id: 42, title: Programmer, Barista)
|
||||
Abigail Boodman (id: 43, title: Chief Architect)
|
||||
`, out)
|
||||
}
|
||||
|
||||
func (s *testSuite) ReadCanned() {
|
||||
_, p, _, _ := runtime.Caller(0)
|
||||
p = path.Join(path.Dir(p), "test-data")
|
||||
out := s.Run(main, []string{"-ds", fmt.Sprintf("ldb:%s::hr", p), "list-persons"})
|
||||
s.Equal(`Aaron Boodman (id: 7, title: Chief Evangelism Officer)
|
||||
Samuel Boodman (id: 13, title: VP, Culture)
|
||||
`, out)
|
||||
}
|
||||
BIN
samples/go/hr/test-data/000002.ldb
Normal file
BIN
samples/go/hr/test-data/000002.ldb
Normal file
Binary file not shown.
BIN
samples/go/hr/test-data/000005.ldb
Normal file
BIN
samples/go/hr/test-data/000005.ldb
Normal file
Binary file not shown.
0
samples/go/hr/test-data/000008.log
Normal file
0
samples/go/hr/test-data/000008.log
Normal file
1
samples/go/hr/test-data/CURRENT
Normal file
1
samples/go/hr/test-data/CURRENT
Normal file
@@ -0,0 +1 @@
|
||||
MANIFEST-000009
|
||||
0
samples/go/hr/test-data/LOCK
Normal file
0
samples/go/hr/test-data/LOCK
Normal file
BIN
samples/go/hr/test-data/MANIFEST-000009
Normal file
BIN
samples/go/hr/test-data/MANIFEST-000009
Normal file
Binary file not shown.
Reference in New Issue
Block a user