Files
dolt/nomdl/codegen/test/struct_optional.go
Chris Masone 5ce93dad2e Beginning of import support in NomDL
This patch mostly merges parse.Package and types.Package, though it
can't quite go all the way. A types.Package doesn't have 'using'
declarations, while the parsed representation of a .noms file needs to
have that information. Hence, the parse package is moved to the 'pkg'
package, and pkg.Parsed is introduced. This type embeds types.Package
and adds the necessary additional information.

To make inroads on handling imports, I enhanced ParsePackage() (now
called ParseNomDL()) to actually process the 'alias' and 'import'
statements in the input and go replace namespaced type names in the
package with refs of imported packages. For example, the TypeRef for
'Bar' generated in the following package

alias Foo = import "sha1-ffffffff"

struct Bar {
  f: Foo.RockinStruct
}

will actually return types.Ref{sha1-ffffffff} when you call PackageRef()
on it.

In addition, I've added a function to the new 'pkg' package,
which allows the caller to get the dependencies of a type package
from a chunk store.

Fixes issue #353, towards issue #294
2015-09-28 16:08:22 -07:00

118 lines
3.0 KiB
Go

// This file was generated by nomdl/codegen.
package test
import (
"github.com/attic-labs/noms/ref"
"github.com/attic-labs/noms/types"
)
var __testPackageInFile_struct_optional_CachedRef = __testPackageInFile_struct_optional_Ref()
// This function builds up a Noms value that describes the type
// package implemented by this file and registers it with the global
// type package definition cache.
func __testPackageInFile_struct_optional_Ref() types.Ref {
p := types.PackageDef{
NamedTypes: types.MapOfStringToTypeRefDef{
"OptionalStruct": __typeRefOfOptionalStruct(),
},
}.New()
return types.Ref{R: types.RegisterPackage(&p)}
}
// OptionalStruct
type OptionalStruct struct {
m types.Map
}
func NewOptionalStruct() OptionalStruct {
return OptionalStruct{types.NewMap(
types.NewString("$name"), types.NewString("OptionalStruct"),
types.NewString("$type"), types.MakeTypeRef("OptionalStruct", __testPackageInFile_struct_optional_CachedRef),
)}
}
type OptionalStructDef struct {
S string
B bool
}
func (def OptionalStructDef) New() OptionalStruct {
return OptionalStruct{
types.NewMap(
types.NewString("$name"), types.NewString("OptionalStruct"),
types.NewString("$type"), types.MakeTypeRef("OptionalStruct", __testPackageInFile_struct_optional_CachedRef),
types.NewString("s"), types.NewString(def.S),
types.NewString("b"), types.Bool(def.B),
)}
}
func (s OptionalStruct) Def() (d OptionalStructDef) {
if v, ok := s.m.MaybeGet(types.NewString("s")); ok {
d.S = v.(types.String).String()
}
if v, ok := s.m.MaybeGet(types.NewString("b")); ok {
d.B = bool(v.(types.Bool))
}
return
}
// Creates and returns a Noms Value that describes OptionalStruct.
func __typeRefOfOptionalStruct() types.TypeRef {
return types.MakeStructTypeRef("OptionalStruct",
[]types.Field{
types.Field{"s", types.MakePrimitiveTypeRef(types.StringKind), true},
types.Field{"b", types.MakePrimitiveTypeRef(types.BoolKind), true},
},
types.Choices{},
)
}
func OptionalStructFromVal(val types.Value) OptionalStruct {
// TODO: Validate here
return OptionalStruct{val.(types.Map)}
}
func (s OptionalStruct) NomsValue() types.Value {
return s.m
}
func (s OptionalStruct) Equals(other OptionalStruct) bool {
return s.m.Equals(other.m)
}
func (s OptionalStruct) Ref() ref.Ref {
return s.m.Ref()
}
func (s OptionalStruct) Type() types.TypeRef {
return s.m.Get(types.NewString("$type")).(types.TypeRef)
}
func (s OptionalStruct) S() (v string, ok bool) {
var vv types.Value
if vv, ok = s.m.MaybeGet(types.NewString("s")); ok {
v = vv.(types.String).String()
}
return
}
func (s OptionalStruct) SetS(val string) OptionalStruct {
return OptionalStruct{s.m.Set(types.NewString("s"), types.NewString(val))}
}
func (s OptionalStruct) B() (v bool, ok bool) {
var vv types.Value
if vv, ok = s.m.MaybeGet(types.NewString("b")); ok {
v = bool(vv.(types.Bool))
}
return
}
func (s OptionalStruct) SetB(val bool) OptionalStruct {
return OptionalStruct{s.m.Set(types.NewString("b"), types.Bool(val))}
}