From 042c198fcc4ba44ad8fd7eb032e333f33c21dad3 Mon Sep 17 00:00:00 2001 From: nicoblaser02 <88314447+nnnnblaser@users.noreply.github.com> Date: Tue, 25 Jul 2023 16:35:05 +0200 Subject: [PATCH] Add Type names (#697) * Add Type names * Handle TypeNames of Predefined Types --- ttcn3/types/types.go | 57 ++++++++++++++++++++++++++++++++------- ttcn3/types/types_test.go | 10 +++---- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/ttcn3/types/types.go b/ttcn3/types/types.go index 748315ba..0db274e6 100644 --- a/ttcn3/types/types.go +++ b/ttcn3/types/types.go @@ -134,6 +134,7 @@ func (k Kind) String() string { // A Type represents a TTCN-3 type. type Type interface { String() string + descriptionString() string } // A PrimitiveType represents a non-composite type, such as integer, boolean, @@ -146,6 +147,19 @@ type PrimitiveType struct { } func (t *PrimitiveType) String() string { + if _, ok := Predefined[t.Name]; ok { + return t.Name + } + if t.Name == "" { + return t.descriptionString() + } + return t.Name + " [" + t.descriptionString() + "]" +} + +func (t *PrimitiveType) descriptionString() string { + if _, ok := Predefined[t.Name]; ok { + return t.Name + } if t.ValueConstraints == nil { return t.Kind.String() } @@ -179,23 +193,36 @@ type ListType struct { } func (t *ListType) String() string { + if _, ok := Predefined[t.Name]; ok { + return t.Name + } + if t.Name == "" { + return t.descriptionString() + } + return t.Name + " [" + t.descriptionString() + "]" +} + +func (t *ListType) descriptionString() string { + if _, ok := Predefined[t.Name]; ok { + return t.Name + } elem := "any" if t.ElementType != nil && !isString(t.Kind) { - elem = t.ElementType.String() + elem = t.ElementType.descriptionString() } switch t.Kind { case RecordOf, Any: - var lengthConstraint string = " " + var lengthConstraint string = "" if t.LengthConstraint.Expr != nil { - lengthConstraint = " length(" + t.LengthConstraint.String() + ") " + lengthConstraint = "length(" + t.LengthConstraint.String() + ") " } - return "record" + lengthConstraint + "of " + elem + return "record " + lengthConstraint + "of " + elem case SetOf: - var lengthConstraint string = " " + var lengthConstraint string = "" if t.LengthConstraint.Expr != nil { - lengthConstraint = " length(" + t.LengthConstraint.String() + ") " + lengthConstraint = "length(" + t.LengthConstraint.String() + ") " } - return "set" + lengthConstraint + "of " + elem + return "set " + lengthConstraint + "of " + elem case Map: if _, ok := t.ElementType.(*PairType); !ok { return "map from " + elem + " to any" @@ -242,6 +269,10 @@ func (t *StructuredType) String() string { return "" } +func (t *StructuredType) descriptionString() string { + return t.String() +} + // A Field represents a fields in structures types. type Field struct { Type @@ -270,6 +301,10 @@ func (t *BehaviourType) String() string { return "" } +func (t *BehaviourType) descriptionString() string { + return t.String() +} + // A PairType represents a pair. Pairs are not specified by TTCN-3 standard explicitly. It is for modeling map types as // a set of key-value-pairs. type PairType struct { @@ -277,12 +312,16 @@ type PairType struct { } func (t *PairType) String() string { + return t.descriptionString() +} + +func (t *PairType) descriptionString() string { res := []string{"any", "any"} if t.First != nil { - res[0] = t.First.String() + res[0] = t.First.descriptionString() } if t.Second != nil { - res[1] = t.Second.String() + res[1] = t.Second.descriptionString() } return strings.Join(res, " to ") diff --git a/ttcn3/types/types_test.go b/ttcn3/types/types_test.go index 12f4c549..b388f34c 100644 --- a/ttcn3/types/types_test.go +++ b/ttcn3/types/types_test.go @@ -238,7 +238,7 @@ func TestTypeStrings(t *testing.T) { Kind: types.Array, ElementType: &types.ListType{ Kind: types.SetOf, - ElementType: types.Float, + ElementType: types.Predefined["float"], }}, }, }, @@ -247,13 +247,13 @@ func TestTypeStrings(t *testing.T) { // Named types - {skip: true, Output: "foo [any]", + {Output: "foo [any]", Type: &types.PrimitiveType{Name: "foo"}}, - {skip: true, Output: "foo [record of any]", + {Output: "foo [record of any]", Type: &types.ListType{Name: "foo"}}, - {skip: true, Output: "foo [record of integer]", + {Output: "foo [record of integer]", Type: &types.ListType{ Name: "foo", ElementType: &types.PrimitiveType{ @@ -262,7 +262,7 @@ func TestTypeStrings(t *testing.T) { }, }}, - {skip: true, Output: "record of integer", + {Output: "record of integer", Type: &types.ListType{ ElementType: &types.PrimitiveType{ Name: "foo",