dhall-golang: github.com/philandstuff/dhall-golang Index | Examples | Files | Directories

package dhall

import "github.com/philandstuff/dhall-golang"

Package dhall implements routines for deserializing and evaluating Dhall configuration into Go data structures.

For more on the Dhall language, see https://dhall-lang.org/

If you have the following struct:

 type Message struct {
	 Name string
	 Body string
	 Time int64
 }

And a file called foo.dhall containing the following Dhall configuration:

{ Name = "Alice", Body = "Hello", Time = 1294706395881547000 }

You can deserialize the Dhall into the struct as follows (error handling skipped for brevity):

var m Message
dhallBytes, err := ioutil.ReadFile("foo.dhall")
err = dhall.Unmarshal(dhallBytes, &m)

This version supports Dhall standard 11.1.0, except that it doesn't support `using` directives.

Code:

package main

import (
    "fmt"

    "github.com/philandstuff/dhall-golang"
)

// Message is the struct we want to unmarshal from Dhall
type Message struct {
    Name string
    Body string
    Time int64
}

// dhallMessage is the Dhall source we want to unmarshal
const dhallMessage = `
{ Name = "Alice", Body = "Hello", Time = 1294706395881547000 }
`

func main() {
    var m Message
    err := dhall.Unmarshal([]byte(dhallMessage), &m)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%+v", m)
}

Code:

// you can also unmarshal Dhall functions to Go functions
var greet func(string) string
err := dhall.Unmarshal([]byte(`λ(name : Text) → "Howdy, ${name}!"`), &greet)
if err != nil {
    panic(err)
}
fmt.Println(greet("Alice"))

Output:

Howdy, Alice!

Code:

var i int
err := dhall.Unmarshal([]byte(`5`), &i)
if err != nil {
    panic(err)
}
fmt.Println(i)

Output:

5

Code:

package main

import (
    "fmt"

    "github.com/philandstuff/dhall-golang"
)

// NestedConfig is the struct we want to unmarshal from Dhall
type NestedConfig struct {
    Name     string
    DBConfig struct {
        Username string
        Password string
    }
}

const nestedDhallMessage = `
{ Name = "Alice", DBConfig = { Username = "foo", Password = "bar" } }
`

func main() {
    var m NestedConfig
    err := dhall.Unmarshal([]byte(nestedDhallMessage), &m)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%+v", m)
}

Code:

var s string
err := dhall.Unmarshal([]byte(`"foo"`), &s)
if err != nil {
    panic(err)
}
fmt.Println(s)

Output:

foo

Index

Examples

Package Files

doc.go generate.go unmarshal.go

func Decode Uses

func Decode(e core.Value, out interface{})

Decode takes a core.Value and unmarshals it into the given variable.

func Unmarshal Uses

func Unmarshal(b []byte, out interface{}) error

Unmarshal takes dhall input as a byte array and parses it, evaluates it, and unmarshals it into the given variable.

Directories

PathSynopsis
binaryPackage binary defines the CBOR representation of Dhall terms.
cmd
corePackage core contains the core Dhall language implementation.
importsPackage imports defines how to resolve Dhall imports.
internalPackage internal contains internal names, not for use by library consumers.
parserPackage parser enables parsing Dhall source into Terms.

Package dhall imports 6 packages (graph). Updated 2019-11-26. Refresh now. Tools for package owners.