From 7996c6a90ab6c8597532a724fb785b5d66d66120 Mon Sep 17 00:00:00 2001 From: nicoblaser02 <88314447+nnnnblaser@users.noreply.github.com> Date: Fri, 21 Jul 2023 13:21:36 +0200 Subject: [PATCH] Map string (#696) --- ttcn3/types/types.go | 39 ++++++++++++++++++++++------ ttcn3/types/types_test.go | 54 +++++++++++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/ttcn3/types/types.go b/ttcn3/types/types.go index ac9797af..748315ba 100644 --- a/ttcn3/types/types.go +++ b/ttcn3/types/types.go @@ -179,19 +179,28 @@ type ListType struct { } func (t *ListType) String() string { + elem := "any" + if t.ElementType != nil && !isString(t.Kind) { + elem = t.ElementType.String() + } switch t.Kind { - case RecordOf: + case RecordOf, Any: var lengthConstraint string = " " if t.LengthConstraint.Expr != nil { lengthConstraint = " length(" + t.LengthConstraint.String() + ") " } - return "record" + lengthConstraint + "of " + t.ElementType.String() + return "record" + lengthConstraint + "of " + elem case SetOf: var lengthConstraint string = " " if t.LengthConstraint.Expr != nil { lengthConstraint = " length(" + t.LengthConstraint.String() + ") " } - return "set" + lengthConstraint + "of " + t.ElementType.String() + return "set" + lengthConstraint + "of " + elem + case Map: + if _, ok := t.ElementType.(*PairType); !ok { + return "map from " + elem + " to any" + } + return "map from " + elem case Charstring, Octetstring, Hexstring, Bitstring, UniversalCharstring: var lengthConstraint string = "" if t.LengthConstraint.Expr != nil { @@ -199,18 +208,24 @@ func (t *ListType) String() string { } return t.Kind.String() + lengthConstraint case Array: - var arrayType string = t.ElementType.String() if eleType, ok := t.ElementType.(*ListType); ok && (eleType.Kind == RecordOf || eleType.Kind == SetOf || eleType.Kind == Map) { - arrayType = "(" + arrayType + ")" + elem = "(" + elem + ")" } if t.LengthConstraint.Expr == nil { - return arrayType + "[]" + return elem + "[]" } - return arrayType + "[" + t.LengthConstraint.String() + "]" + return elem + "[" + t.LengthConstraint.String() + "]" } return "" } +func isString(t Kind) bool { + if t == Bitstring || t == Charstring || t == Octetstring || t == Hexstring || t == UniversalCharstring { + return true + } + return false +} + // A StructuredType represents structured types, such as record, set, union, // class, ... type StructuredType struct { @@ -262,7 +277,15 @@ type PairType struct { } func (t *PairType) String() string { - return "" + res := []string{"any", "any"} + if t.First != nil { + res[0] = t.First.String() + } + if t.Second != nil { + res[1] = t.Second.String() + } + + return strings.Join(res, " to ") } // A Value represents a single value constraint, such as '1' or '10..20'. diff --git a/ttcn3/types/types_test.go b/ttcn3/types/types_test.go index c53ef928..12f4c549 100644 --- a/ttcn3/types/types_test.go +++ b/ttcn3/types/types_test.go @@ -81,10 +81,10 @@ func TestTypeStrings(t *testing.T) { // List types - {skip: true, Output: "record of any", + {Output: "record of any", Type: &types.ListType{}}, - {skip: true, Output: "record of any", + {Output: "record of any", Type: &types.ListType{Kind: types.RecordOf}}, {Output: "hexstring", @@ -135,12 +135,12 @@ func TestTypeStrings(t *testing.T) { // Arrays - {skip: true, Output: "any[]", + {Output: "any[]", Type: &types.ListType{ Kind: types.Array, }}, - {skip: true, Output: "any[1..10]", + {Output: "any[1..10]", Type: &types.ListType{ Kind: types.Array, LengthConstraint: types.Value{Expr: rangeVal}, @@ -183,22 +183,22 @@ func TestTypeStrings(t *testing.T) { }, }}, - {skip: true, Output: "map from any to any", + {Output: "map from any to any", Type: &types.ListType{Kind: types.Map}}, - {skip: true, Output: "map from hexstring to any", + {Output: "map from integer to any", Type: &types.ListType{ Kind: types.Map, ElementType: types.Predefined["integer"], }}, - {skip: true, Output: "map from any to any", + {Output: "map from any to any", Type: &types.ListType{ Kind: types.Map, ElementType: &types.PairType{}, }}, - {skip: true, Output: "map from integer to charstring", + {Output: "map from integer to charstring", Type: &types.ListType{ Kind: types.Map, ElementType: &types.PairType{ @@ -207,6 +207,44 @@ func TestTypeStrings(t *testing.T) { }, }}, + {Output: "(map from integer to charstring)[]", + Type: &types.ListType{ + Kind: types.Array, + ElementType: &types.ListType{ + Kind: types.Map, + ElementType: &types.PairType{ + First: types.Predefined["integer"], + Second: types.Predefined["charstring"], + }, + }, + }}, + + {Output: "map from record of integer[] to map from charstring to (set of float)[]", + Type: &types.ListType{ + Kind: types.Map, + ElementType: &types.PairType{ + First: &types.ListType{ + Kind: types.RecordOf, + ElementType: &types.ListType{ + Kind: types.Array, + ElementType: types.Predefined["integer"], + }, + }, + Second: &types.ListType{ + Kind: types.Map, + ElementType: &types.PairType{ + First: types.Predefined["charstring"], + Second: &types.ListType{ + Kind: types.Array, + ElementType: &types.ListType{ + Kind: types.SetOf, + ElementType: types.Float, + }}, + }, + }, + }, + }}, + // Named types {skip: true, Output: "foo [any]",