From 8b7c1db43836b90fda9cf7af414ad3fbde3836f6 Mon Sep 17 00:00:00 2001 From: atauov Date: Thu, 31 Jul 2025 12:38:16 +0500 Subject: [PATCH 1/2] get types --- statefun/function_type.go | 45 +++++++++++++++++++++++++++++++++++++ statefun/plugins/plugins.go | 18 +++++++-------- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/statefun/function_type.go b/statefun/function_type.go index 35a4b5b..585a257 100644 --- a/statefun/function_type.go +++ b/statefun/function_type.go @@ -185,6 +185,7 @@ func (ft *FunctionType) workerTaskExecutor(id string, msg FunctionTypeMsg) { SetContextExpirationAfter: func(after time.Duration) { ft.setContextExpirationAfter(ft.name+"."+id, after) }, GetObjectContext: func() *easyjson.JSON { return ft.getContext(id) }, SetObjectContext: func(context *easyjson.JSON) { ft.setContext(id, context) }, + GetObjectImplTypes: func() (types []string) { return ft.getObjectImplTypes(id) }, Domain: ft.runtime.Domain, Self: sfPlugins.StatefunAddress{Typename: ft.name, ID: id}, Signal: func(signalProvider sfPlugins.SignalProvider, targetTypename string, targetID string, j *easyjson.JSON, o *easyjson.JSON) error { @@ -403,3 +404,47 @@ func (ft *FunctionType) setContextExpirationAfter(keyValueID string, after time. func (ft *FunctionType) getStreamName() string { return fmt.Sprintf("%s_stream", system.GetHashStr(ft.subject)) } + +func (ft *FunctionType) getObjectImplTypes(id string) []string { + objectType, err := ft.findObjectType(id) + if err != nil || objectType == "" { + return nil + } + + types := []string{objectType} + + result := map[string]struct{}{} + result[objectType] = struct{}{} + + response, err := ft.runtime.Request(sfPlugins.AutoRequestSelect, "functions.cmdb.api.type.read", objectType, nil, nil) + if err != nil { + return types + } + + if response.PathExists("data.body.cache.parent_types") { + parentTypes := response.GetByPath("data.body.cache.parent_types") + for i := 0; i < parentTypes.ArraySize(); i++ { + parentType := parentTypes.ArrayElement(i).AsStringDefault("") + parentType = ft.runtime.Domain.CreateObjectIDWithHubDomain(parentType, true) + if len(parentType) > 0 { + result[parentType] = struct{}{} + } + } + } + + for _type := range result { + if _type != objectType { + types = append(types, _type) + } + } + + return types +} + +func (ft *FunctionType) findObjectType(id string) (string, error) { + response, err := ft.runtime.Request(sfPlugins.AutoRequestSelect, "functions.cmdb.api.object.read", id, nil, nil) + if err != nil { + return "", err + } + return response.GetByPath("data.type").AsStringDefault(""), nil +} diff --git a/statefun/plugins/plugins.go b/statefun/plugins/plugins.go index d60a247..391035b 100644 --- a/statefun/plugins/plugins.go +++ b/statefun/plugins/plugins.go @@ -94,18 +94,18 @@ type StatefunContextProcessor struct { SetContextExpirationAfter func(time.Duration) GetObjectContext func() *easyjson.JSON SetObjectContext func(*easyjson.JSON) + GetObjectImplTypes func() (types []string) ObjectMutexLock func(objectId string, errorOnLocked bool) error ObjectMutexUnlock func(objectId string) error Domain Domain - // TODO: DownstreamSignal(, , , ) - Signal SFSignalFunc - Request SFRequestFunc - Egress SFEgressFunc - Self StatefunAddress - Caller StatefunAddress - Payload *easyjson.JSON - Options *easyjson.JSON - Reply *SyncReply // when requested in function: nil - function was signaled, !nil - function was requested + Signal SFSignalFunc + Request SFRequestFunc + Egress SFEgressFunc + Self StatefunAddress + Caller StatefunAddress + Payload *easyjson.JSON + Options *easyjson.JSON + Reply *SyncReply // when requested in function: nil - function was signaled, !nil - function was requested } type StatefunExecutor interface { From 119550da13bcf43ecfb6a2f88c6d0b0ef4ad272d Mon Sep 17 00:00:00 2001 From: atauov Date: Thu, 31 Jul 2025 13:06:45 +0500 Subject: [PATCH 2/2] return err --- statefun/function_type.go | 15 +++++++++------ statefun/plugins/plugins.go | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/statefun/function_type.go b/statefun/function_type.go index 585a257..3d95831 100644 --- a/statefun/function_type.go +++ b/statefun/function_type.go @@ -185,7 +185,7 @@ func (ft *FunctionType) workerTaskExecutor(id string, msg FunctionTypeMsg) { SetContextExpirationAfter: func(after time.Duration) { ft.setContextExpirationAfter(ft.name+"."+id, after) }, GetObjectContext: func() *easyjson.JSON { return ft.getContext(id) }, SetObjectContext: func(context *easyjson.JSON) { ft.setContext(id, context) }, - GetObjectImplTypes: func() (types []string) { return ft.getObjectImplTypes(id) }, + GetObjectImplTypes: func() (types []string, err error) { return ft.getObjectImplTypes(id) }, Domain: ft.runtime.Domain, Self: sfPlugins.StatefunAddress{Typename: ft.name, ID: id}, Signal: func(signalProvider sfPlugins.SignalProvider, targetTypename string, targetID string, j *easyjson.JSON, o *easyjson.JSON) error { @@ -405,10 +405,13 @@ func (ft *FunctionType) getStreamName() string { return fmt.Sprintf("%s_stream", system.GetHashStr(ft.subject)) } -func (ft *FunctionType) getObjectImplTypes(id string) []string { +func (ft *FunctionType) getObjectImplTypes(id string) ([]string, error) { objectType, err := ft.findObjectType(id) - if err != nil || objectType == "" { - return nil + if err != nil { + return nil, err + } + if objectType == "" { + return nil, fmt.Errorf("object type is empty for id: %s", id) } types := []string{objectType} @@ -418,7 +421,7 @@ func (ft *FunctionType) getObjectImplTypes(id string) []string { response, err := ft.runtime.Request(sfPlugins.AutoRequestSelect, "functions.cmdb.api.type.read", objectType, nil, nil) if err != nil { - return types + return nil, fmt.Errorf("failed to read type %s: %s", objectType, err.Error()) } if response.PathExists("data.body.cache.parent_types") { @@ -438,7 +441,7 @@ func (ft *FunctionType) getObjectImplTypes(id string) []string { } } - return types + return types, nil } func (ft *FunctionType) findObjectType(id string) (string, error) { diff --git a/statefun/plugins/plugins.go b/statefun/plugins/plugins.go index 391035b..2f1d999 100644 --- a/statefun/plugins/plugins.go +++ b/statefun/plugins/plugins.go @@ -94,7 +94,7 @@ type StatefunContextProcessor struct { SetContextExpirationAfter func(time.Duration) GetObjectContext func() *easyjson.JSON SetObjectContext func(*easyjson.JSON) - GetObjectImplTypes func() (types []string) + GetObjectImplTypes func() (types []string, err error) ObjectMutexLock func(objectId string, errorOnLocked bool) error ObjectMutexUnlock func(objectId string) error Domain Domain