Skip to content

Commit 144a19b

Browse files
“Ken.li”“Ken.li”
authored andcommitted
refactor(object_type/relation): 优化条件序列化与校验逻辑
1. 在对象类型插入数据集数据时序列化condition为JSON字符串 2. 重构scope_binding映射规则校验,新增strictMode参数控制校验严格程度
1 parent f6f288a commit 144a19b

2 files changed

Lines changed: 47 additions & 30 deletions

File tree

adp/bkn/bkn-backend/server/driveradapters/validate_relation_type.go

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func validateRelationTypeCondition(ctx context.Context, cfg *cond.CondCfg, prefi
390390
}
391391

392392
// validateScopeBindingMappingRules 校验 scope_binding 类型的 mapping_rules
393-
func validateScopeBindingMappingRules(ctx context.Context, mappingRules any, _ bool) (*interfaces.ScopeBindingMapping, error) {
393+
func validateScopeBindingMappingRules(ctx context.Context, mappingRules any, strictMode bool) (*interfaces.ScopeBindingMapping, error) {
394394
var mapping interfaces.ScopeBindingMapping
395395
if err := mapstructure.Decode(mappingRules, &mapping); err != nil {
396396
return nil, rest.NewHTTPError(ctx, http.StatusBadRequest, berrors.BknBackend_RelationType_InvalidParameter).
@@ -399,44 +399,50 @@ func validateScopeBindingMappingRules(ctx context.Context, mappingRules any, _ b
399399

400400
// 校验 source 不能为空
401401
if mapping.Source == nil {
402-
return nil, rest.NewHTTPError(ctx, http.StatusBadRequest, berrors.BknBackend_RelationType_InvalidParameter).
403-
WithErrorDetails("scope_binding 的 source 不能为空")
404-
}
405-
406-
// 校验 source.object_type_id 不能为空
407-
if mapping.Source.ObjectTypeID == "" {
408-
return nil, rest.NewHTTPError(ctx, http.StatusBadRequest, berrors.BknBackend_RelationType_InvalidParameter).
409-
WithErrorDetails("scope_binding 的 source.object_type_id 不能为空")
410-
}
402+
if strictMode {
403+
return nil, rest.NewHTTPError(ctx, http.StatusBadRequest, berrors.BknBackend_RelationType_InvalidParameter).
404+
WithErrorDetails("scope_binding 的 source 不能为空")
405+
}
406+
} else {
407+
// 校验 source.object_type_id 不能为空
408+
if mapping.Source.ObjectTypeID == "" {
409+
if strictMode {
410+
return nil, rest.NewHTTPError(ctx, http.StatusBadRequest, berrors.BknBackend_RelationType_InvalidParameter).
411+
WithErrorDetails("scope_binding 的 source.object_type_id 不能为空")
412+
}
413+
}
411414

412-
// 校验 source.filter(如果存在)
413-
if mapping.Source.Condition != nil {
414-
if err := validateRelationTypeCondition(ctx, mapping.Source.Condition, "source.filter"); err != nil {
415-
return nil, err
415+
// 校验 source.filter(如果存在)
416+
if mapping.Source.Condition != nil {
417+
if err := validateRelationTypeCondition(ctx, mapping.Source.Condition, "source.filter"); err != nil {
418+
return nil, err
419+
}
416420
}
417421
}
418422

419423
// 校验 target_rules 不能为空
420424
if len(mapping.TargetRules) == 0 {
421-
return nil, rest.NewHTTPError(ctx, http.StatusBadRequest, berrors.BknBackend_RelationType_InvalidParameter).
422-
WithErrorDetails("scope_binding 的 target_rules 不能为空")
423-
}
424-
425-
// 校验每个 target_rule
426-
for i, targetRule := range mapping.TargetRules {
427-
if err := validateScopeBindingTargetRule(ctx, targetRule, i); err != nil {
428-
return nil, err
425+
if strictMode {
426+
return nil, rest.NewHTTPError(ctx, http.StatusBadRequest, berrors.BknBackend_RelationType_InvalidParameter).
427+
WithErrorDetails("scope_binding 的 target_rules 不能为空")
429428
}
430-
431-
// 校验 condition(如果存在)
432-
if targetRule.Condition != nil {
433-
if targetRule.Scope != interfaces.SCOPE_BINDING_SCOPE_OBJECT_TYPE {
434-
return nil, rest.NewHTTPError(ctx, http.StatusBadRequest, berrors.BknBackend_RelationType_InvalidParameter).
435-
WithErrorDetails(fmt.Sprintf("scope_binding 的 target_rules[%d].condition 仅支持 scope 为 object_type", i))
436-
}
437-
if err := validateRelationTypeCondition(ctx, targetRule.Condition, fmt.Sprintf("target_rules[%d].condition", i)); err != nil {
429+
} else {
430+
// 校验每个 target_rule
431+
for i, targetRule := range mapping.TargetRules {
432+
if err := validateScopeBindingTargetRule(ctx, targetRule, i); err != nil {
438433
return nil, err
439434
}
435+
436+
// 校验 condition(如果存在)
437+
if targetRule.Condition != nil {
438+
if targetRule.Scope != interfaces.SCOPE_BINDING_SCOPE_OBJECT_TYPE {
439+
return nil, rest.NewHTTPError(ctx, http.StatusBadRequest, berrors.BknBackend_RelationType_InvalidParameter).
440+
WithErrorDetails(fmt.Sprintf("scope_binding 的 target_rules[%d].condition 仅支持 scope 为 object_type", i))
441+
}
442+
if err := validateRelationTypeCondition(ctx, targetRule.Condition, fmt.Sprintf("target_rules[%d].condition", i)); err != nil {
443+
return nil, err
444+
}
445+
}
440446
}
441447
}
442448

adp/bkn/bkn-backend/server/logics/object_type/object_type_service.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,17 @@ func (ots *objectTypeService) InsertDatasetData(ctx context.Context, objectTypes
14281428
}
14291429
}
14301430

1431+
// Serialize condition to JSON string
1432+
if cond, exists := doc["condition"]; exists && cond != nil {
1433+
condBytes, err := sonic.Marshal(cond)
1434+
if err != nil {
1435+
logger.Errorf("Failed to marshal action_type condition: %s", err.Error())
1436+
span.SetStatus(codes.Error, "序列化对象类条件失败")
1437+
return err
1438+
}
1439+
doc["condition"] = string(condBytes)
1440+
}
1441+
14311442
// Set document ID
14321443
doc["_id"] = docid
14331444
documents = append(documents, doc)

0 commit comments

Comments
 (0)