Skip to content

Commit 944e408

Browse files
authored
Merge pull request #29 from engalar/feat/mf-improve
fix: handle System entity types and RETURN keyword formatting in microflows
2 parents ab301d1 + f6d619e commit 944e408

4 files changed

Lines changed: 58 additions & 12 deletions

File tree

mdl/executor/cmd_microflows_create.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import (
1313
"github.com/mendixlabs/mxcli/sdk/mpr"
1414
)
1515

16+
// isBuiltinModuleEntity returns true for modules whose entities are defined
17+
// internally by the Mendix runtime and are therefore not present in the MPR's
18+
// domain models. These types are serialized using the qualified name reference
19+
// ("System.Workflow", "System.User", etc.) and resolved at runtime.
20+
func isBuiltinModuleEntity(moduleName string) bool {
21+
return moduleName == "System"
22+
}
23+
1624
// execCreateMicroflow handles CREATE MICROFLOW statements.
1725
func (e *Executor) execCreateMicroflow(s *ast.CreateMicroflowStmt) error {
1826
if e.writer == nil {
@@ -105,8 +113,10 @@ func (e *Executor) execCreateMicroflow(s *ast.CreateMicroflowStmt) error {
105113

106114
// Validate and add parameters
107115
for _, p := range s.Parameters {
108-
// Validate entity references for List and Entity types
109-
if p.Type.EntityRef != nil {
116+
// Validate entity references for List and Entity types.
117+
// Built-in modules (e.g. System) are not stored in the MPR domain models;
118+
// their types are serialized by qualified name and resolved at runtime.
119+
if p.Type.EntityRef != nil && !isBuiltinModuleEntity(p.Type.EntityRef.Module) {
110120
entityID := entityResolver(*p.Type.EntityRef)
111121
if entityID == "" {
112122
return fmt.Errorf("entity '%s.%s' not found for parameter '%s'",
@@ -133,8 +143,10 @@ func (e *Executor) execCreateMicroflow(s *ast.CreateMicroflowStmt) error {
133143

134144
// Validate and set return type
135145
if s.ReturnType != nil {
136-
// Validate entity references for return type
137-
if s.ReturnType.Type.EntityRef != nil {
146+
// Validate entity references for return type.
147+
// Built-in modules (e.g. System) are not stored in the MPR domain models;
148+
// their types are serialized by qualified name and resolved at runtime.
149+
if s.ReturnType.Type.EntityRef != nil && !isBuiltinModuleEntity(s.ReturnType.Type.EntityRef.Module) {
138150
entityID := entityResolver(*s.ReturnType.Type.EntityRef)
139151
if entityID == "" {
140152
return fmt.Errorf("entity '%s.%s' not found for return type",

mdl/executor/cmd_microflows_format_action.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func (e *Executor) formatActivity(
2323

2424
case *microflows.EndEvent:
2525
if activity.ReturnValue != "" {
26-
returnVal := activity.ReturnValue
27-
if !strings.HasPrefix(returnVal, "$") {
26+
returnVal := strings.TrimSuffix(activity.ReturnValue, "\n")
27+
if !strings.HasPrefix(returnVal, "$") && !isMendixKeyword(returnVal) {
2828
returnVal = "$" + returnVal
2929
}
3030
return fmt.Sprintf("RETURN %s;", returnVal)
@@ -846,3 +846,13 @@ func (e *Executor) formatExecuteDatabaseQueryAction(a *microflows.ExecuteDatabas
846846
sb.WriteString(";")
847847
return sb.String()
848848
}
849+
850+
// isMendixKeyword returns true for Mendix expression keywords that must not be
851+
// prefixed with "$" when serialized as a RETURN value.
852+
func isMendixKeyword(s string) bool {
853+
switch s {
854+
case "empty", "true", "false", "null":
855+
return true
856+
}
857+
return false
858+
}

mdl/executor/roundtrip_microflow_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,32 @@ END;`
583583
)
584584
}
585585

586+
// TestRoundtripMicroflow_SystemEntityParameter tests that microflows with
587+
// System.* built-in entity parameter types can be created (issue #29).
588+
// System entities (System.Workflow, System.User, etc.) are not stored in the
589+
// MPR domain models — they are serialized by qualified name at runtime.
590+
func TestRoundtripMicroflow_SystemEntityParameter(t *testing.T) {
591+
env := setupTestEnv(t)
592+
defer env.teardown()
593+
594+
mfName := testModule + ".RT_SystemEntityParam"
595+
env.registerCleanup("microflow", mfName)
596+
597+
createMDL := `CREATE MICROFLOW ` + mfName + ` (
598+
$Workflow: System.Workflow,
599+
$Count: Integer
600+
) RETURNS List of System.User
601+
BEGIN
602+
RETURN empty;
603+
END;`
604+
605+
assertMicroflowContains(t, env, mfName, createMDL,
606+
// "empty" is a reserved keyword — must round-trip as "RETURN empty", not "$empty"
607+
[]string{"System.Workflow", "System.User", "RETURN empty"},
608+
[]string{"RETURN $empty"},
609+
)
610+
}
611+
586612
// TestRoundtripMicroflow_Position tests @position is emitted in DESCRIBE output.
587613
func TestRoundtripMicroflow_Position(t *testing.T) {
588614
env := setupTestEnv(t)

sdk/mpr/writer_microflow.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,9 @@ func (w *Writer) serializeMicroflow(mf *microflows.Microflow) ([]byte, error) {
136136
}
137137

138138
// Add remaining optional fields
139-
returnVarName := mf.ReturnVariableName
140-
if returnVarName == "" {
141-
returnVarName = "Variable" // Default value used by Mendix
142-
}
143-
doc = append(doc, bson.E{Key: "ReturnVariableName", Value: returnVarName})
139+
// ReturnVariableName is "" by default (Studio Pro convention).
140+
// Only set a custom name when explicitly specified via "RETURNS xxx AS $VarName".
141+
doc = append(doc, bson.E{Key: "ReturnVariableName", Value: mf.ReturnVariableName})
144142
doc = append(doc, bson.E{Key: "StableId", Value: idToBsonBinary(generateUUID())})
145143
doc = append(doc, bson.E{Key: "Url", Value: ""})
146144
doc = append(doc, bson.E{Key: "UrlSearchParameters", Value: bson.A{int32(1)}})
@@ -223,7 +221,7 @@ func serializeMicroflowParameter(p *microflows.MicroflowParameter, posX int) bso
223221
{Key: "$Type", Value: "Microflows$MicroflowParameter"},
224222
{Key: "DefaultValue", Value: ""},
225223
{Key: "Documentation", Value: p.Documentation},
226-
{Key: "HasVariableNameBeenChanged", Value: true},
224+
{Key: "HasVariableNameBeenChanged", Value: false},
227225
{Key: "IsRequired", Value: true},
228226
{Key: "Name", Value: p.Name},
229227
{Key: "RelativeMiddlePoint", Value: relativeMiddlePoint},

0 commit comments

Comments
 (0)