github.com/cloudwan/edgelq-sdk@v1.15.4/limits/resources/v1alpha2/plan/plan.pb.fieldmask.go (about)

     1  // Code generated by protoc-gen-goten-object
     2  // File: edgelq/limits/proto/v1alpha2/plan.proto
     3  // DO NOT EDIT!!!
     4  
     5  package plan
     6  
     7  import (
     8  	"encoding/json"
     9  	"strings"
    10  
    11  	"google.golang.org/grpc/codes"
    12  	"google.golang.org/grpc/status"
    13  	"google.golang.org/protobuf/proto"
    14  	preflect "google.golang.org/protobuf/reflect/protoreflect"
    15  	googlefieldmaskpb "google.golang.org/protobuf/types/known/fieldmaskpb"
    16  
    17  	gotenobject "github.com/cloudwan/goten-sdk/runtime/object"
    18  )
    19  
    20  // proto imports
    21  import (
    22  	iam_iam_common "github.com/cloudwan/edgelq-sdk/iam/resources/v1alpha2/common"
    23  	common "github.com/cloudwan/edgelq-sdk/limits/resources/v1alpha2/common"
    24  	meta_service "github.com/cloudwan/edgelq-sdk/meta/resources/v1alpha2/service"
    25  	meta "github.com/cloudwan/goten-sdk/types/meta"
    26  )
    27  
    28  // ensure the imports are used
    29  var (
    30  	_ = new(json.Marshaler)
    31  	_ = strings.Builder{}
    32  
    33  	_ = codes.NotFound
    34  	_ = status.Status{}
    35  	_ = new(proto.Message)
    36  	_ = new(preflect.Message)
    37  	_ = googlefieldmaskpb.FieldMask{}
    38  
    39  	_ = new(gotenobject.FieldMask)
    40  )
    41  
    42  // make sure we're using proto imports
    43  var (
    44  	_ = &iam_iam_common.PCR{}
    45  	_ = &common.Allowance{}
    46  	_ = &meta_service.Service{}
    47  	_ = &meta.Meta{}
    48  )
    49  
    50  type Plan_FieldMask struct {
    51  	Paths []Plan_FieldPath
    52  }
    53  
    54  func FullPlan_FieldMask() *Plan_FieldMask {
    55  	res := &Plan_FieldMask{}
    56  	res.Paths = append(res.Paths, &Plan_FieldTerminalPath{selector: Plan_FieldPathSelectorName})
    57  	res.Paths = append(res.Paths, &Plan_FieldTerminalPath{selector: Plan_FieldPathSelectorDisplayName})
    58  	res.Paths = append(res.Paths, &Plan_FieldTerminalPath{selector: Plan_FieldPathSelectorService})
    59  	res.Paths = append(res.Paths, &Plan_FieldTerminalPath{selector: Plan_FieldPathSelectorResourceLimits})
    60  	res.Paths = append(res.Paths, &Plan_FieldTerminalPath{selector: Plan_FieldPathSelectorPlanLevel})
    61  	res.Paths = append(res.Paths, &Plan_FieldTerminalPath{selector: Plan_FieldPathSelectorBusinessTier})
    62  	res.Paths = append(res.Paths, &Plan_FieldTerminalPath{selector: Plan_FieldPathSelectorMetadata})
    63  	return res
    64  }
    65  
    66  func (fieldMask *Plan_FieldMask) String() string {
    67  	if fieldMask == nil {
    68  		return "<nil>"
    69  	}
    70  	pathsStr := make([]string, 0, len(fieldMask.Paths))
    71  	for _, path := range fieldMask.Paths {
    72  		pathsStr = append(pathsStr, path.String())
    73  	}
    74  	return strings.Join(pathsStr, ", ")
    75  }
    76  
    77  func (fieldMask *Plan_FieldMask) IsFull() bool {
    78  	if fieldMask == nil {
    79  		return false
    80  	}
    81  	presentSelectors := make([]bool, 7)
    82  	for _, path := range fieldMask.Paths {
    83  		if asFinal, ok := path.(*Plan_FieldTerminalPath); ok {
    84  			presentSelectors[int(asFinal.selector)] = true
    85  		}
    86  	}
    87  	for _, flag := range presentSelectors {
    88  		if !flag {
    89  			return false
    90  		}
    91  	}
    92  	return true
    93  }
    94  
    95  func (fieldMask *Plan_FieldMask) ProtoReflect() preflect.Message {
    96  	return gotenobject.MakeFieldMaskReflection(fieldMask, func(raw string) (gotenobject.FieldPath, error) {
    97  		return ParsePlan_FieldPath(raw)
    98  	})
    99  }
   100  
   101  func (fieldMask *Plan_FieldMask) ProtoMessage() {}
   102  
   103  func (fieldMask *Plan_FieldMask) Reset() {
   104  	if fieldMask != nil {
   105  		fieldMask.Paths = nil
   106  	}
   107  }
   108  
   109  func (fieldMask *Plan_FieldMask) Subtract(other *Plan_FieldMask) *Plan_FieldMask {
   110  	result := &Plan_FieldMask{}
   111  	removedSelectors := make([]bool, 7)
   112  	otherSubMasks := map[Plan_FieldPathSelector]gotenobject.FieldMask{
   113  		Plan_FieldPathSelectorResourceLimits: &common.Allowance_FieldMask{},
   114  		Plan_FieldPathSelectorMetadata:       &meta.Meta_FieldMask{},
   115  	}
   116  	mySubMasks := map[Plan_FieldPathSelector]gotenobject.FieldMask{
   117  		Plan_FieldPathSelectorResourceLimits: &common.Allowance_FieldMask{},
   118  		Plan_FieldPathSelectorMetadata:       &meta.Meta_FieldMask{},
   119  	}
   120  
   121  	for _, path := range other.GetPaths() {
   122  		switch tp := path.(type) {
   123  		case *Plan_FieldTerminalPath:
   124  			removedSelectors[int(tp.selector)] = true
   125  		case *Plan_FieldSubPath:
   126  			otherSubMasks[tp.selector].AppendRawPath(tp.subPath)
   127  		}
   128  	}
   129  	for _, path := range fieldMask.GetPaths() {
   130  		if !removedSelectors[int(path.Selector())] {
   131  			if otherSubMask := otherSubMasks[path.Selector()]; otherSubMask != nil && otherSubMask.PathsCount() > 0 {
   132  				if tp, ok := path.(*Plan_FieldTerminalPath); ok {
   133  					switch tp.selector {
   134  					case Plan_FieldPathSelectorResourceLimits:
   135  						mySubMasks[Plan_FieldPathSelectorResourceLimits] = common.FullAllowance_FieldMask()
   136  					case Plan_FieldPathSelectorMetadata:
   137  						mySubMasks[Plan_FieldPathSelectorMetadata] = meta.FullMeta_FieldMask()
   138  					}
   139  				} else if tp, ok := path.(*Plan_FieldSubPath); ok {
   140  					mySubMasks[tp.selector].AppendRawPath(tp.subPath)
   141  				}
   142  			} else {
   143  				result.Paths = append(result.Paths, path)
   144  			}
   145  		}
   146  	}
   147  	for selector, mySubMask := range mySubMasks {
   148  		if mySubMask.PathsCount() > 0 {
   149  			for _, allowedPath := range mySubMask.SubtractRaw(otherSubMasks[selector]).GetRawPaths() {
   150  				result.Paths = append(result.Paths, &Plan_FieldSubPath{selector: selector, subPath: allowedPath})
   151  			}
   152  		}
   153  	}
   154  
   155  	if len(result.Paths) == 0 {
   156  		return nil
   157  	}
   158  	return result
   159  }
   160  
   161  func (fieldMask *Plan_FieldMask) SubtractRaw(other gotenobject.FieldMask) gotenobject.FieldMask {
   162  	return fieldMask.Subtract(other.(*Plan_FieldMask))
   163  }
   164  
   165  // FilterInputFields generates copy of field paths with output_only field paths removed
   166  func (fieldMask *Plan_FieldMask) FilterInputFields() *Plan_FieldMask {
   167  	result := &Plan_FieldMask{}
   168  	for _, path := range fieldMask.Paths {
   169  		switch path.Selector() {
   170  		case Plan_FieldPathSelectorMetadata:
   171  			if _, ok := path.(*Plan_FieldTerminalPath); ok {
   172  				for _, subpath := range meta.FullMeta_FieldMask().FilterInputFields().Paths {
   173  					result.Paths = append(result.Paths, &Plan_FieldSubPath{selector: path.Selector(), subPath: subpath})
   174  				}
   175  			} else if sub, ok := path.(*Plan_FieldSubPath); ok {
   176  				selectedMask := &meta.Meta_FieldMask{
   177  					Paths: []meta.Meta_FieldPath{sub.subPath.(meta.Meta_FieldPath)},
   178  				}
   179  				for _, allowedPath := range selectedMask.FilterInputFields().Paths {
   180  					result.Paths = append(result.Paths, &Plan_FieldSubPath{selector: Plan_FieldPathSelectorMetadata, subPath: allowedPath})
   181  				}
   182  			}
   183  		default:
   184  			result.Paths = append(result.Paths, path)
   185  		}
   186  	}
   187  	return result
   188  }
   189  
   190  // ToFieldMask is used for proto conversions
   191  func (fieldMask *Plan_FieldMask) ToProtoFieldMask() *googlefieldmaskpb.FieldMask {
   192  	protoFieldMask := &googlefieldmaskpb.FieldMask{}
   193  	for _, path := range fieldMask.Paths {
   194  		protoFieldMask.Paths = append(protoFieldMask.Paths, path.String())
   195  	}
   196  	return protoFieldMask
   197  }
   198  
   199  func (fieldMask *Plan_FieldMask) FromProtoFieldMask(protoFieldMask *googlefieldmaskpb.FieldMask) error {
   200  	if fieldMask == nil {
   201  		return status.Error(codes.Internal, "target field mask is nil")
   202  	}
   203  	fieldMask.Paths = make([]Plan_FieldPath, 0, len(protoFieldMask.Paths))
   204  	for _, strPath := range protoFieldMask.Paths {
   205  		path, err := ParsePlan_FieldPath(strPath)
   206  		if err != nil {
   207  			return err
   208  		}
   209  		fieldMask.Paths = append(fieldMask.Paths, path)
   210  	}
   211  	return nil
   212  }
   213  
   214  // implement methods required by customType
   215  func (fieldMask Plan_FieldMask) Marshal() ([]byte, error) {
   216  	protoFieldMask := fieldMask.ToProtoFieldMask()
   217  	return proto.Marshal(protoFieldMask)
   218  }
   219  
   220  func (fieldMask *Plan_FieldMask) Unmarshal(data []byte) error {
   221  	protoFieldMask := &googlefieldmaskpb.FieldMask{}
   222  	if err := proto.Unmarshal(data, protoFieldMask); err != nil {
   223  		return err
   224  	}
   225  	if err := fieldMask.FromProtoFieldMask(protoFieldMask); err != nil {
   226  		return err
   227  	}
   228  	return nil
   229  }
   230  
   231  func (fieldMask *Plan_FieldMask) Size() int {
   232  	return proto.Size(fieldMask.ToProtoFieldMask())
   233  }
   234  
   235  func (fieldMask Plan_FieldMask) MarshalJSON() ([]byte, error) {
   236  	return json.Marshal(fieldMask.ToProtoFieldMask())
   237  }
   238  
   239  func (fieldMask *Plan_FieldMask) UnmarshalJSON(data []byte) error {
   240  	protoFieldMask := &googlefieldmaskpb.FieldMask{}
   241  	if err := json.Unmarshal(data, protoFieldMask); err != nil {
   242  		return err
   243  	}
   244  	if err := fieldMask.FromProtoFieldMask(protoFieldMask); err != nil {
   245  		return err
   246  	}
   247  	return nil
   248  }
   249  
   250  func (fieldMask *Plan_FieldMask) AppendPath(path Plan_FieldPath) {
   251  	fieldMask.Paths = append(fieldMask.Paths, path)
   252  }
   253  
   254  func (fieldMask *Plan_FieldMask) AppendRawPath(path gotenobject.FieldPath) {
   255  	fieldMask.Paths = append(fieldMask.Paths, path.(Plan_FieldPath))
   256  }
   257  
   258  func (fieldMask *Plan_FieldMask) GetPaths() []Plan_FieldPath {
   259  	if fieldMask == nil {
   260  		return nil
   261  	}
   262  	return fieldMask.Paths
   263  }
   264  
   265  func (fieldMask *Plan_FieldMask) GetRawPaths() []gotenobject.FieldPath {
   266  	if fieldMask == nil {
   267  		return nil
   268  	}
   269  	rawPaths := make([]gotenobject.FieldPath, 0, len(fieldMask.Paths))
   270  	for _, path := range fieldMask.Paths {
   271  		rawPaths = append(rawPaths, path)
   272  	}
   273  	return rawPaths
   274  }
   275  
   276  func (fieldMask *Plan_FieldMask) SetFromCliFlag(raw string) error {
   277  	path, err := ParsePlan_FieldPath(raw)
   278  	if err != nil {
   279  		return err
   280  	}
   281  	fieldMask.Paths = append(fieldMask.Paths, path)
   282  	return nil
   283  }
   284  
   285  func (fieldMask *Plan_FieldMask) Set(target, source *Plan) {
   286  	for _, path := range fieldMask.Paths {
   287  		val, _ := path.GetSingle(source)
   288  		// if val is nil, then field does not exist in source, skip
   289  		// otherwise, process (can still reflect.ValueOf(val).IsNil!)
   290  		if val != nil {
   291  			path.WithIValue(val).SetTo(&target)
   292  		}
   293  	}
   294  }
   295  
   296  func (fieldMask *Plan_FieldMask) SetRaw(target, source gotenobject.GotenObjectExt) {
   297  	fieldMask.Set(target.(*Plan), source.(*Plan))
   298  }
   299  
   300  func (fieldMask *Plan_FieldMask) Project(source *Plan) *Plan {
   301  	if source == nil {
   302  		return nil
   303  	}
   304  	if fieldMask == nil {
   305  		return source
   306  	}
   307  	result := &Plan{}
   308  	resourceLimitsMask := &common.Allowance_FieldMask{}
   309  	wholeResourceLimitsAccepted := false
   310  	metadataMask := &meta.Meta_FieldMask{}
   311  	wholeMetadataAccepted := false
   312  
   313  	for _, p := range fieldMask.Paths {
   314  		switch tp := p.(type) {
   315  		case *Plan_FieldTerminalPath:
   316  			switch tp.selector {
   317  			case Plan_FieldPathSelectorName:
   318  				result.Name = source.Name
   319  			case Plan_FieldPathSelectorDisplayName:
   320  				result.DisplayName = source.DisplayName
   321  			case Plan_FieldPathSelectorService:
   322  				result.Service = source.Service
   323  			case Plan_FieldPathSelectorResourceLimits:
   324  				result.ResourceLimits = source.ResourceLimits
   325  				wholeResourceLimitsAccepted = true
   326  			case Plan_FieldPathSelectorPlanLevel:
   327  				result.PlanLevel = source.PlanLevel
   328  			case Plan_FieldPathSelectorBusinessTier:
   329  				result.BusinessTier = source.BusinessTier
   330  			case Plan_FieldPathSelectorMetadata:
   331  				result.Metadata = source.Metadata
   332  				wholeMetadataAccepted = true
   333  			}
   334  		case *Plan_FieldSubPath:
   335  			switch tp.selector {
   336  			case Plan_FieldPathSelectorResourceLimits:
   337  				resourceLimitsMask.AppendPath(tp.subPath.(common.Allowance_FieldPath))
   338  			case Plan_FieldPathSelectorMetadata:
   339  				metadataMask.AppendPath(tp.subPath.(meta.Meta_FieldPath))
   340  			}
   341  		}
   342  	}
   343  	if wholeResourceLimitsAccepted == false && len(resourceLimitsMask.Paths) > 0 {
   344  		for _, sourceItem := range source.GetResourceLimits() {
   345  			result.ResourceLimits = append(result.ResourceLimits, resourceLimitsMask.Project(sourceItem))
   346  		}
   347  	}
   348  	if wholeMetadataAccepted == false && len(metadataMask.Paths) > 0 {
   349  		result.Metadata = metadataMask.Project(source.GetMetadata())
   350  	}
   351  	return result
   352  }
   353  
   354  func (fieldMask *Plan_FieldMask) ProjectRaw(source gotenobject.GotenObjectExt) gotenobject.GotenObjectExt {
   355  	return fieldMask.Project(source.(*Plan))
   356  }
   357  
   358  func (fieldMask *Plan_FieldMask) PathsCount() int {
   359  	if fieldMask == nil {
   360  		return 0
   361  	}
   362  	return len(fieldMask.Paths)
   363  }