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
bfb8b49e
Commit
bfb8b49e
authored
Nov 14, 2020
by
Will Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Return error rather than panic
parent
a96a7f02
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
10 deletions
+23
-10
errors.go
errors.go
+11
-1
schema/gen/go/genStruct.go
schema/gen/go/genStruct.go
+9
-1
schema/gen/go/testStructsContainingMaybe_test.go
schema/gen/go/testStructsContainingMaybe_test.go
+3
-8
No files found.
errors.go
View file @
bfb8b49e
...
...
@@ -2,6 +2,7 @@ package ipld
import
(
"fmt"
"strings"
)
// ErrWrongKind may be returned from functions on the Node interface when
...
...
@@ -152,6 +153,15 @@ func (e ErrIteratorOverread) Error() string {
type
ErrCannotBeNull
struct
{}
// Review: arguably either ErrInvalidKindForNodePrototype.
type
ErrMissingRequiredField
struct
{}
// only possible for typed nodes -- specifically, struct types.
// ErrMissingRequiredField is returned when calling 'Finish' on a NodeAssembler
// for a Struct that has not has all required fields set.
type
ErrMissingRequiredField
struct
{
Missing
[]
string
}
func
(
e
ErrMissingRequiredField
)
Error
()
string
{
return
"missing required fields: "
+
strings
.
Join
(
e
.
Missing
,
","
)
}
type
ErrListOverrun
struct
{}
// only possible for typed nodes -- specifically, struct types with list (aka tuple) representations.
type
ErrInvalidUnionDiscriminant
struct
{}
// only possible for typed nodes -- specifically, union types.
schema/gen/go/genStruct.go
View file @
bfb8b49e
...
...
@@ -508,7 +508,15 @@ func (g structBuilderGenerator) emitMapAssemblerMethods(w io.Writer) {
panic("invalid state: Finish cannot be called on an assembler that's already finished")
}
if ma.s & fieldBits__{{ $type | TypeSymbol }}_sufficient != fieldBits__{{ $type | TypeSymbol }}_sufficient {
panic("invalid state: Not all required fields set")
err := ipld.ErrMissingRequiredField{Missing: make([]string, 0)}
{{- range $i, $field := .Type.Fields }}
{{- if not $field.IsMaybe}}
if ma.s & fieldBit__{{ $type | TypeSymbol }}_{{ $field | FieldSymbolUpper }} == 0 {
err.Missing = append(err.Missing, "{{ $field.Name }}")
}
{{- end}}
{{- end}}
return err
}
ma.state = maState_finished
*ma.m = schema.Maybe_Value
...
...
schema/gen/go/testStructsContainingMaybe_test.go
View file @
bfb8b49e
...
...
@@ -187,15 +187,10 @@ func TestStructsContainingMaybe(t *testing.T) {
}
v
.
AssignString
(
"v1"
)
shouldPanicOnBuild
:=
func
(
t
*
testing
.
T
)
{
defer
func
()
{
if
r
:=
recover
();
r
==
nil
{
t
.
Errorf
(
"The code did not panic"
)
}
}()
_
=
b
.
Build
()
err
=
mb
.
Finish
()
if
_
,
ok
:=
err
.
(
ipld
.
ErrMissingRequiredField
);
!
ok
{
t
.
Fatalf
(
"Expected error for missing field, got %v"
,
err
)
}
shouldPanicOnBuild
(
t
)
})
})
})
...
...
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