xiaobaicai66695 commented on code in PR #1552: URL: https://github.com/apache/dubbo-admin/pull/1552#discussion_r4144361811
########## pkg/core/resource/apis/mesh/v1alpha1/router_rule_codec.go: ########## @@ -0,0 +1,239 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package v1alpha1 + +import ( + "fmt" + "strings" + + "sigs.k8s.io/yaml" + + meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1" + "github.com/apache/dubbo-admin/pkg/common/bizerror" + "github.com/apache/dubbo-admin/pkg/common/constants" + coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model" +) + +// The protobuf field is named affinity for historical/internal reasons. The +// public Dubbo 3 contract is affinityAware, so never marshal the protobuf +// message directly for these resources. +type affinityRouteYAML struct { + ConfigVersion string `json:"configVersion"` + Scope string `json:"scope"` + Key string `json:"key"` + Runtime bool `json:"runtime"` + Enabled bool `json:"enabled"` + AffinityAware *affinityAwareYAML `json:"affinityAware"` +} + +type affinityAwareYAML struct { + Key string `json:"key"` + Ratio int32 `json:"ratio"` +} + +type scriptRouteYAML struct { + ConfigVersion string `json:"configVersion"` + Scope string `json:"scope"` + Key string `json:"key"` + Enabled bool `json:"enabled"` + Type string `json:"type"` + Script string `json:"script"` +} + +// EncodeRule serializes a rule resource using the public Dubbo YAML contract. +// Existing rule kinds fall back to their historical protobuf/YAML encoding; +// Affinity and Script use explicit codecs because their public field names and +// boolean defaults differ from the internal protobuf shape. +func EncodeRule(r coremodel.Resource) ([]byte, error) { + if err := ValidateRule(r); err != nil { + return nil, err + } + var value any + switch typed := r.(type) { + case *AffinityRouteResource: + var aware *affinityAwareYAML + if typed.Spec.Affinity != nil { + aware = &affinityAwareYAML{Key: typed.Spec.Affinity.Key, Ratio: typed.Spec.Affinity.Ratio} + } + value = affinityRouteYAML{ + ConfigVersion: typed.Spec.ConfigVersion, + Scope: typed.Spec.Scope, + Key: typed.Spec.Key, + Runtime: typed.Spec.Runtime, + Enabled: typed.Spec.Enabled, + AffinityAware: aware, + } + case *ScriptRouteResource: + value = scriptRouteYAML{ + ConfigVersion: typed.Spec.ConfigVersion, + Scope: typed.Spec.Scope, + Key: typed.Spec.Key, + Enabled: typed.Spec.Enabled, + Type: typed.Spec.Type, + Script: typed.Spec.Script, + } + default: + if r == nil || r.ResourceSpec() == nil { + return nil, invalidRule("rule spec is required") + } + value = r.ResourceSpec() + } + data, err := yaml.Marshal(value) + if err != nil { + return nil, bizerror.Wrap(err, bizerror.YamlError, "failed to marshal rule") + } + return data, nil +} + +// DecodeRule parses a public rule YAML document into a typed resource. It is +// used by both registry subscribers and governor read-back paths. +func DecodeRule(kind coremodel.ResourceKind, mesh, name, data string) (coremodel.Resource, error) { + if strings.TrimSpace(data) == "" { + switch kind { + case AffinityRouteKind: + return NewAffinityRouteResourceWithAttributes(name, mesh), nil + case ScriptRouteKind: + return NewScriptRouteResourceWithAttributes(name, mesh), nil + } + } + switch kind { + case AffinityRouteKind: + external := affinityRouteYAML{} + if err := yaml.Unmarshal([]byte(data), &external); err != nil { + return nil, ruleYAMLError(name, err) + } + res := NewAffinityRouteResourceWithAttributes(name, mesh) + if external.AffinityAware != nil { + res.Spec.Affinity = &meshproto.AffinityAware{ + Key: external.AffinityAware.Key, + Ratio: external.AffinityAware.Ratio, + } + } + res.Spec.ConfigVersion = external.ConfigVersion + res.Spec.Scope = external.Scope + res.Spec.Key = external.Key + res.Spec.Runtime = external.Runtime + res.Spec.Enabled = external.Enabled + return res, nil Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
