From 18da630fa0d168a7b3cf5ef3a40cf3e501da4931 Mon Sep 17 00:00:00 2001 From: fredgan Date: Thu, 12 Nov 2020 14:49:05 +0800 Subject: [PATCH] adjust the logic and add a testcase --- pkg/yang/types.go | 3 ++- pkg/yang/types_builtin.go | 2 +- pkg/yang/types_test.go | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/yang/types.go b/pkg/yang/types.go index 12977a9..c052c8d 100644 --- a/pkg/yang/types.go +++ b/pkg/yang/types.go @@ -280,7 +280,8 @@ check: y.IdentityBase = resolvedBase.Identity } - if t.Range != nil { + // If parsing fraction-digits of type decimal64 fails, the range should not be parsed + if t.Range != nil && (!isDecimal64 || (isDecimal64 && y.FractionDigits != 0)) { yr, err := parseRanges(t.Range.Name, isDecimal64, uint8(y.FractionDigits)) switch { case err != nil: diff --git a/pkg/yang/types_builtin.go b/pkg/yang/types_builtin.go index 62267a2..40bfc8d 100644 --- a/pkg/yang/types_builtin.go +++ b/pkg/yang/types_builtin.go @@ -623,7 +623,7 @@ func ParseDecimal(s string, fracDigRequired uint8) (n Number, err error) { // numStr must conform to Section 9.3.4. func decimalValueFromString(numStr string, fracDigRequired uint8) (n Number, err error) { if fracDigRequired > MaxFractionDigits || fracDigRequired < 1 { - return n, fmt.Errorf("invalid number of fraction digits %d > max of %d, minimum 1", fracDigRequired, MaxFractionDigits) + return n, fmt.Errorf("invalid number of fraction digits %d: out of range [1..%d]", fracDigRequired, MaxFractionDigits) } s := numStr diff --git a/pkg/yang/types_test.go b/pkg/yang/types_test.go index 58f6cc4..91b14a1 100644 --- a/pkg/yang/types_test.go +++ b/pkg/yang/types_test.go @@ -41,6 +41,13 @@ func TestTypeResolve(t *testing.T) { Range: Int64Range, }, }, + { + in: &Type{ + Name: "int8", + Range: &Range{Name: "-10 .. 128"}, + }, + err: "unknown: bad range: -10..128 not within -128..127", + }, { in: &Type{ Name: "boolean", @@ -67,6 +74,14 @@ func TestTypeResolve(t *testing.T) { }, err: "unknown: value 42 out of range [1..18]", }, + { + in: &Type{ + Name: "decimal64", + FractionDigits: &Value{Name: "42"}, + Range: &Range{Name: "-10 .. 10"}, + }, + err: "unknown: value 42 out of range [1..18]", + }, { in: &Type{ Name: "decimal64",