Skip to content

Commit

Permalink
Merge pull request #3 from philandstuff/unmarshal-should-fetch-imports
Browse files Browse the repository at this point in the history
[unmarshal] Actually fetch imports in Unmarshal
  • Loading branch information
philandstuff authored Feb 15, 2020
2 parents 5bbd647 + 820a57d commit 56ff509
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
- removed `core.AppValue` from public interface
- renamed various types to remove `-Val` and `-Term` suffixes

### Changed
### Fixed

- `dhall.Unmarshal()` now typechecks before evaluating
- `dhall.Unmarshal()` now resolves imports and typechecks before
evaluating

### Added

Expand Down
1 change: 1 addition & 0 deletions testdata/unmarshal-test.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ Port = 5050, Name = "inetd" }
14 changes: 10 additions & 4 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/philandstuff/dhall-golang/core"
"github.com/philandstuff/dhall-golang/imports"
"github.com/philandstuff/dhall-golang/parser"
)

Expand All @@ -18,18 +19,23 @@ func isMapEntryType(recordType core.RecordType) bool {
return false
}

// Unmarshal takes dhall input as a byte array and parses it,
// evaluates it, and unmarshals it into the given variable.
// Unmarshal takes dhall input as a byte array and parses it, resolves
// imports, typechecks, evaluates, and unmarshals it into the given
// variable.
func Unmarshal(b []byte, out interface{}) error {
term, err := parser.Parse("-", b)
if err != nil {
return err
}
_, err = core.TypeOf(term)
resolved, err := imports.Load(term)
if err != nil {
return err
}
_, err = core.TypeOf(resolved)
if err != nil {
return err
}
Decode(core.Eval(term), out)
Decode(core.Eval(resolved), out)
return nil
}

Expand Down
13 changes: 12 additions & 1 deletion unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,23 @@ var _ = Describe("Decode", func() {
var _ = Describe("Unmarshal", func() {
It("Parses 1 + 2", func() {
var actual uint
Unmarshal([]byte("1 + 2"), &actual)
err := Unmarshal([]byte("1 + 2"), &actual)
Expect(err).ToNot(HaveOccurred())
Expect(actual).To(Equal(uint(3)))
})
It("Throws a type error for `1 + -2`", func() {
var actual uint
err := Unmarshal([]byte("1 + -2"), &actual)
Expect(err).To(HaveOccurred())
})
It("Fetches imports", func() {
type Config struct {
Port int
Name string
}
var actual Config
err := Unmarshal([]byte("./testdata/unmarshal-test.dhall"), &actual)
Expect(err).ToNot(HaveOccurred())
Expect(actual).To(Equal(Config{Port: 5050, Name: "inetd"}))
})
})

0 comments on commit 56ff509

Please sign in to comment.