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 14.0.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:

package main

import (
    "fmt"

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

// Config is the struct we want to unmarshal from Dhall
type Config struct {
    Name string
    // Dhall lets you unmarshal functions as well as data
    Greet func(string) string
}

// configMessage is the Dhall source we want to unmarshal
const configMessage = `
{ Name = "Alice", Greet = λ(name : Text) → "Howdy, ${name}!" }
`

func main() {
    // 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"))
}

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:

package main

import (
    "fmt"

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

// TaggedMessage is the struct we want to unmarshal from Dhall
type TaggedMessage struct {
    Name string `dhall:"name"`
    Body string `dhall:"entity"`
    Time int64  `dhall:"instant"`
}

// dhallTaggedMessage is the Dhall source we want to unmarshal
const dhallTaggedMessage = `
{ name = "Alice", entity = "Hello", instant = 1294706395881547000 }
`

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

Index

Examples

Package Files

doc.go unmarshal.go

func Decode Uses

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

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, resolves imports, typechecks, evaluates, 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.
parser/internal
termPackage term contains data types for Dhall terms.

Package dhall imports 6 packages (graph) and is imported by 1 packages. Updated 2020-03-22. Refresh now. Tools for package owners.