Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
ld
go-ld-prime
Commits
fa013972
Commit
fa013972
authored
Jul 27, 2020
by
Eric Myhre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests for keyed unions; some fixes.
parent
605efe83
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
3 deletions
+90
-3
schema/gen/go/genUnionReprKeyed.go
schema/gen/go/genUnionReprKeyed.go
+3
-3
schema/gen/go/testUnions_test.go
schema/gen/go/testUnions_test.go
+87
-0
No files found.
schema/gen/go/genUnionReprKeyed.go
View file @
fa013972
...
...
@@ -88,10 +88,10 @@ func (g unionReprKeyedReprGenerator) EmitNodeMethodLookupByString(w io.Writer) {
if n.tag != {{ add $i 1 }} {
return nil, ipld.ErrNotExists{ipld.PathSegmentOfString(key)}
}
return
&
n.x{{ add $i 1 }}, nil
return n.x{{ add $i 1 }}
.Representation()
, nil
{{- else if (eq (dot.AdjCfg.UnionMemlayout dot.Type) "interface") }}
if n2, ok := n.x.({{ $member | TypeSymbol }}); ok {
return n2, nil
return n2
.Representation()
, nil
} else {
return nil, ipld.ErrNotExists{ipld.PathSegmentOfString(key)}
}
...
...
@@ -351,7 +351,7 @@ func (g unionReprKeyedReprBuilderGenerator) emitMapAssemblerMethods(w io.Writer)
x := &_{{ $member | TypeSymbol }}{}
ma.w.x = x
if ma.ca{{ add $i 1 }} == nil {
ma.ca{{ add $i 1 }} = &_{{ $member | TypeSymbol }}__Assembler{}
ma.ca{{ add $i 1 }} = &_{{ $member | TypeSymbol }}__
Repr
Assembler{}
}
ma.ca{{ add $i 1 }}.w = x
ma.ca{{ add $i 1 }}.m = &ma.cm
...
...
schema/gen/go/testUnions_test.go
View file @
fa013972
...
...
@@ -75,3 +75,90 @@ func TestUnionKeyed(t *testing.T) {
})
})
}
// Test keyed unions again, but this time with more complex types as children.
//
// The previous tests used scalar types as the children; this exercises most things,
// but also has a couple (extremely non-obvious) simplifications:
// namely, because the default representation for strings are "natural" representations,
// the ReprAssemblers are actually aliases of the type-level Assemblers!
// Aaaand that makes a few things "work" by coincidence that wouldn't otherwise fly.
func
TestUnionKeyedComplexChildren
(
t
*
testing
.
T
)
{
ts
:=
schema
.
TypeSystem
{}
ts
.
Init
()
adjCfg
:=
&
AdjunctCfg
{}
ts
.
Accumulate
(
schema
.
SpawnString
(
"String"
))
ts
.
Accumulate
(
schema
.
SpawnStruct
(
"SmolStruct"
,
[]
schema
.
StructField
{
schema
.
SpawnStructField
(
"s"
,
"String"
,
false
,
false
),
},
schema
.
SpawnStructRepresentationMap
(
map
[
string
]
string
{
"s"
:
"q"
,
}),
))
ts
.
Accumulate
(
schema
.
SpawnUnion
(
"WheeUnion"
,
[]
schema
.
TypeName
{
"String"
,
"SmolStruct"
,
},
schema
.
SpawnUnionRepresentationKeyed
(
map
[
string
]
schema
.
TypeName
{
"a"
:
"String"
,
"b"
:
"SmolStruct"
,
}),
))
test
:=
func
(
t
*
testing
.
T
,
getPrototypeByName
func
(
string
)
ipld
.
NodePrototype
)
{
np
:=
getPrototypeByName
(
"WheeUnion"
)
nrp
:=
getPrototypeByName
(
"WheeUnion.Repr"
)
var
n
schema
.
TypedNode
t
.
Run
(
"typed-create"
,
func
(
t
*
testing
.
T
)
{
n
=
fluent
.
MustBuildMap
(
np
,
1
,
func
(
na
fluent
.
MapAssembler
)
{
na
.
AssembleEntry
(
"SmolStruct"
)
.
CreateMap
(
1
,
func
(
na
fluent
.
MapAssembler
)
{
na
.
AssembleEntry
(
"s"
)
.
AssignString
(
"whee"
)
})
})
.
(
schema
.
TypedNode
)
t
.
Run
(
"typed-read"
,
func
(
t
*
testing
.
T
)
{
Require
(
t
,
n
.
ReprKind
(),
ShouldEqual
,
ipld
.
ReprKind_Map
)
Wish
(
t
,
n
.
Length
(),
ShouldEqual
,
1
)
n2
:=
must
.
Node
(
n
.
LookupByString
(
"SmolStruct"
))
Require
(
t
,
n2
.
ReprKind
(),
ShouldEqual
,
ipld
.
ReprKind_Map
)
Wish
(
t
,
must
.
String
(
must
.
Node
(
n2
.
LookupByString
(
"s"
))),
ShouldEqual
,
"whee"
)
})
t
.
Run
(
"repr-read"
,
func
(
t
*
testing
.
T
)
{
nr
:=
n
.
Representation
()
Require
(
t
,
nr
.
ReprKind
(),
ShouldEqual
,
ipld
.
ReprKind_Map
)
Wish
(
t
,
nr
.
Length
(),
ShouldEqual
,
1
)
n2
:=
must
.
Node
(
nr
.
LookupByString
(
"b"
))
Require
(
t
,
n2
.
ReprKind
(),
ShouldEqual
,
ipld
.
ReprKind_Map
)
Wish
(
t
,
must
.
String
(
must
.
Node
(
n2
.
LookupByString
(
"q"
))),
ShouldEqual
,
"whee"
)
})
})
t
.
Run
(
"repr-create"
,
func
(
t
*
testing
.
T
)
{
nr
:=
fluent
.
MustBuildMap
(
nrp
,
2
,
func
(
na
fluent
.
MapAssembler
)
{
na
.
AssembleEntry
(
"b"
)
.
CreateMap
(
1
,
func
(
na
fluent
.
MapAssembler
)
{
na
.
AssembleEntry
(
"q"
)
.
AssignString
(
"whee"
)
})
})
Wish
(
t
,
n
,
ShouldEqual
,
nr
)
})
}
t
.
Run
(
"union-using-embed"
,
func
(
t
*
testing
.
T
)
{
adjCfg
.
CfgUnionMemlayout
=
map
[
schema
.
TypeName
]
string
{
"WheeUnion"
:
"embedAll"
}
prefix
:=
"union-keyed-complex-child-using-embed"
pkgName
:=
"main"
genAndCompileAndTest
(
t
,
prefix
,
pkgName
,
ts
,
adjCfg
,
func
(
t
*
testing
.
T
,
getPrototypeByName
func
(
string
)
ipld
.
NodePrototype
)
{
test
(
t
,
getPrototypeByName
)
})
})
t
.
Run
(
"union-using-interface"
,
func
(
t
*
testing
.
T
)
{
adjCfg
.
CfgUnionMemlayout
=
map
[
schema
.
TypeName
]
string
{
"WheeUnion"
:
"interface"
}
prefix
:=
"union-keyed-complex-child-using-interface"
pkgName
:=
"main"
genAndCompileAndTest
(
t
,
prefix
,
pkgName
,
ts
,
adjCfg
,
func
(
t
*
testing
.
T
,
getPrototypeByName
func
(
string
)
ipld
.
NodePrototype
)
{
test
(
t
,
getPrototypeByName
)
})
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment