Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions bindings/go/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,15 @@ add_test(
NAME update_bindings_go_src_fdb_generated_go
COMMAND ${CMAKE_COMMAND} -E compare_files ${go_options_file} ${CMAKE_CURRENT_SOURCE_DIR}/src/fdb/generated.go
)

# gofmt should be installed with go, but it's better to check if the binary is available.
find_program(GOFMT_EXECUTABLE gofmt HINTS /usr/local/go/bin/)
if(GOFMT_EXECUTABLE)
add_test(
NAME fdb-go-fmt
# gofmt -d doesn't return a non-zero exit code, so we have to check if the output is empty.
COMMAND bash -c "out=$(\"${GOFMT_EXECUTABLE}\" -d \"${CMAKE_CURRENT_SOURCE_DIR}\"); test -z \"$out\" || { echo \"$out\"; exit 1; }"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
else()
message(WARNING "gofmt not found. The 'fdb-go-fmt' test will not be available.")
endif()
18 changes: 13 additions & 5 deletions bindings/go/src/_util/translate_fdb_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@ type Option struct {
Description string `xml:"description,attr"`
Hidden bool `xml:"hidden,attr"`
}

type Scope struct {
Name string `xml:"name,attr"`
Option []Option
}

type Options struct {
Scope []Scope
}

// sanitize normalizes description text to be gofmt-compliant: replaces RST
// double-backtick spans with Go-style double-quoted strings and removes
// trailing whitespace.
func sanitize(s string) string {
return strings.TrimRight(strings.ReplaceAll(s, "``", "\""), " \t")
}

func writeOptString(w io.Writer, receiver string, function string, opt Option) {
fmt.Fprintf(w, `func (o %s) %s(param string) error {
return o.setOpt(%d, []byte(param))
Expand Down Expand Up @@ -84,9 +93,9 @@ func writeOpt(w io.Writer, receiver string, opt Option) {
fmt.Fprintln(w)

if opt.Description != "" {
fmt.Fprintf(w, "// %s\n", opt.Description)
fmt.Fprintf(w, "// %s\n", sanitize(opt.Description))
if opt.ParamDesc != "" {
fmt.Fprintf(w, "//\n// Parameter: %s\n", opt.ParamDesc)
fmt.Fprintf(w, "//\n// Parameter: %s\n", sanitize(opt.ParamDesc))
}
} else {
fmt.Fprintf(w, "// Not yet implemented.\n")
Expand Down Expand Up @@ -117,14 +126,13 @@ func writeMutation(w io.Writer, opt Option) {
func (t Transaction) %s(key KeyConvertible, param []byte) {
t.atomicOp(key.FDBKey(), param, %d)
}
`, opt.Description, tname, opt.Code)
`, sanitize(opt.Description), tname, opt.Code)
}

func writeEnum(w io.Writer, scope Scope, opt Option, delta int) {
fmt.Fprintln(w)
if opt.Description != "" {
doc.ToText(w, opt.Description, "\t// ", "", 73)
// fmt.Printf(" // %s\n", opt.Description)
doc.ToText(w, sanitize(opt.Description), "\t// ", "", 73)
}
fmt.Fprintf(w, " %s %s = %d\n", scope.Name+translateName(opt.Name), scope.Name, opt.Code+delta)
}
Expand Down
48 changes: 25 additions & 23 deletions bindings/go/src/fdb/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,33 @@ A basic interaction with the FoundationDB API is demonstrated below:
package main

import (
"github.com/apple/foundationdb/bindings/go/src/fdb"
"log"
"fmt"

"github.com/apple/foundationdb/bindings/go/src/fdb"
"log"
"fmt"

)

func main() {
// Different API versions may expose different runtime behaviors.
fdb.MustAPIVersion(800)

// Open the default database from the system cluster
db := fdb.MustOpenDefault()

// Database reads and writes happen inside transactions
ret, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) {
tr.Set(fdb.Key("hello"), []byte("world"))
return tr.Get(fdb.Key("foo")).MustGet(), nil
// db.Transact automatically commits (and if necessary,
// retries) the transaction
})
if err != nil {
log.Fatalf("Unable to perform FDB transaction (%v)", err)
}

fmt.Printf("hello is now world, foo was: %s\n", string(ret.([]byte)))
}
func main() {
// Different API versions may expose different runtime behaviors.
fdb.MustAPIVersion(800)

// Open the default database from the system cluster
db := fdb.MustOpenDefault()

// Database reads and writes happen inside transactions
ret, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) {
tr.Set(fdb.Key("hello"), []byte("world"))
return tr.Get(fdb.Key("foo")).MustGet(), nil
// db.Transact automatically commits (and if necessary,
// retries) the transaction
})
if err != nil {
log.Fatalf("Unable to perform FDB transaction (%v)", err)
}

fmt.Printf("hello is now world, foo was: %s\n", string(ret.([]byte)))
}

# Futures

Expand Down
Loading