github.com/cloudwan/edgelq-sdk@v1.15.4/monitoring/resources/v4/metric_descriptor/metric_descriptor.pb.name.go (about)

     1  // Code generated by protoc-gen-goten-resource
     2  // Resource: MetricDescriptor
     3  // DO NOT EDIT!!!
     4  
     5  package metric_descriptor
     6  
     7  import (
     8  	"fmt"
     9  	"net/url"
    10  	"reflect"
    11  	"regexp"
    12  	"strings"
    13  
    14  	"google.golang.org/grpc/codes"
    15  	"google.golang.org/grpc/status"
    16  	"google.golang.org/protobuf/proto"
    17  
    18  	"github.com/cloudwan/goten-sdk/runtime/goten"
    19  	gotenresource "github.com/cloudwan/goten-sdk/runtime/resource"
    20  )
    21  
    22  // proto imports
    23  import (
    24  	api "github.com/cloudwan/edgelq-sdk/common/api"
    25  	common "github.com/cloudwan/edgelq-sdk/monitoring/resources/v4/common"
    26  	monitored_resource_descriptor "github.com/cloudwan/edgelq-sdk/monitoring/resources/v4/monitored_resource_descriptor"
    27  	project "github.com/cloudwan/edgelq-sdk/monitoring/resources/v4/project"
    28  	meta "github.com/cloudwan/goten-sdk/types/meta"
    29  	durationpb "google.golang.org/protobuf/types/known/durationpb"
    30  )
    31  
    32  // ensure the imports are used
    33  var (
    34  	_ = codes.NotFound
    35  	_ = new(fmt.Stringer)
    36  	_ = new(proto.Message)
    37  	_ = status.Status{}
    38  	_ = url.URL{}
    39  	_ = strings.Builder{}
    40  
    41  	_ = new(goten.GotenMessage)
    42  	_ = new(gotenresource.ListQuery)
    43  )
    44  
    45  // make sure we're using proto imports
    46  var (
    47  	_ = api.LaunchStage(0)
    48  	_ = &common.LabelDescriptor{}
    49  	_ = &monitored_resource_descriptor.MonitoredResourceDescriptor{}
    50  	_ = &project.Project{}
    51  	_ = &durationpb.Duration{}
    52  	_ = &meta.Meta{}
    53  )
    54  
    55  var metricDescriptor_RegexpId = regexp.MustCompile("^(?P<metric_descriptor_id>[\\w./-]{4,128})$")
    56  var regexPath_Project = regexp.MustCompile("^projects/(?P<project_id>-|[\\w][\\w.-]{0,127})/metricDescriptors/(?P<metric_descriptor_id>-|[\\w./-]{4,128})$")
    57  
    58  func (r *MetricDescriptor) MaybePopulateDefaults() error {
    59  	metricDescriptorInterface := interface{}(r)
    60  	if defaulter, ok := metricDescriptorInterface.(goten.Defaulter); ok {
    61  		return defaulter.PopulateDefaults()
    62  	}
    63  	return nil
    64  }
    65  
    66  type Name struct {
    67  	ParentName
    68  	MetricDescriptorId string `firestore:"metricDescriptorId"`
    69  }
    70  
    71  func ParseName(name string) (*Name, error) {
    72  	var matches []string
    73  	if matches = regexPath_Project.FindStringSubmatch(name); matches != nil {
    74  		return NewNameBuilder().
    75  			SetProjectId(matches[1]).
    76  			SetId(matches[2]).
    77  			Name(), nil
    78  	}
    79  
    80  	return nil, status.Errorf(codes.InvalidArgument, "unable to parse '%s' as MetricDescriptor name", name)
    81  }
    82  
    83  func MustParseName(name string) *Name {
    84  	result, err := ParseName(name)
    85  	if err != nil {
    86  		panic(err)
    87  	}
    88  	return result
    89  }
    90  
    91  func ParseNameOrId(nameOrId string) (*Name, error) {
    92  	name, err := ParseName(nameOrId)
    93  	if err == nil {
    94  		return name, err
    95  	}
    96  	if metricDescriptor_RegexpId.MatchString(nameOrId) {
    97  		return &Name{MetricDescriptorId: nameOrId}, nil
    98  	} else {
    99  		return nil, fmt.Errorf("unable to parse '%s' as MetricDescriptor name or id", nameOrId)
   100  	}
   101  }
   102  
   103  func (name *Name) SetFromSegments(segments gotenresource.NameSegments) error {
   104  	if len(segments) == 0 {
   105  		return status.Errorf(codes.InvalidArgument, "No segments given for MetricDescriptor name")
   106  	}
   107  	if err := name.ParentName.SetFromSegments(segments[:len(segments)-1]); err != nil {
   108  		return err
   109  	}
   110  	if segments[len(segments)-1].CollectionLowerJson != "metricDescriptors" {
   111  		return status.Errorf(codes.InvalidArgument, "unable to use segments %s to form MetricDescriptor name", segments)
   112  	}
   113  	name.MetricDescriptorId = segments[len(segments)-1].Id
   114  	return nil
   115  }
   116  
   117  func (name *Name) GetProjectName() *project.Name {
   118  	if name == nil {
   119  		return nil
   120  	}
   121  	return name.ParentName.GetProjectName()
   122  }
   123  
   124  func (name *Name) IsSpecified() bool {
   125  	if name == nil || name.Pattern == "" || name.MetricDescriptorId == "" {
   126  		return false
   127  	}
   128  	return name.ParentName.IsSpecified()
   129  }
   130  
   131  func (name *Name) IsFullyQualified() bool {
   132  	if name == nil {
   133  		return false
   134  	}
   135  	if name.ParentName.IsFullyQualified() == false {
   136  		return false
   137  	}
   138  	if name.MetricDescriptorId == "" || name.MetricDescriptorId == gotenresource.WildcardId {
   139  		return false
   140  	}
   141  	return true
   142  }
   143  
   144  func (name *Name) FullyQualifiedName() (string, error) {
   145  	if !name.IsFullyQualified() {
   146  		return "", status.Errorf(codes.InvalidArgument, "Name for MetricDescriptor is not fully qualified")
   147  	}
   148  	return fmt.Sprintf("//monitoring.edgelq.com/%s", name.String()), nil
   149  }
   150  
   151  func (name *Name) String() string {
   152  	if name == nil {
   153  		return "<nil>"
   154  	}
   155  	if valueStr, err := name.ProtoString(); err != nil {
   156  		panic(err)
   157  	} else {
   158  		return valueStr
   159  	}
   160  }
   161  
   162  func (name *Name) AsReference() *Reference {
   163  	return &Reference{Name: *name}
   164  }
   165  
   166  func (name *Name) AsRawReference() gotenresource.Reference {
   167  	return name.AsReference()
   168  }
   169  
   170  func (name *Name) GetResourceDescriptor() gotenresource.Descriptor {
   171  	return descriptor
   172  }
   173  
   174  func (name *Name) GetPattern() gotenresource.NamePattern {
   175  	if name == nil {
   176  		return ""
   177  	}
   178  	return name.Pattern
   179  }
   180  
   181  func (name *Name) GetIdParts() map[string]string {
   182  	if name != nil {
   183  		return map[string]string{
   184  			"projectId":          name.ProjectId,
   185  			"metricDescriptorId": name.MetricDescriptorId,
   186  		}
   187  	}
   188  	return map[string]string{
   189  		"projectId":          "",
   190  		"metricDescriptorId": "",
   191  	}
   192  }
   193  
   194  func (name *Name) GetSegments() gotenresource.NameSegments {
   195  	if name == nil || name.Pattern == "" {
   196  		return nil
   197  	}
   198  	segments := name.ParentName.GetSegments()
   199  	return append(segments, gotenresource.NameSegment{
   200  		CollectionLowerJson: "metricDescriptors",
   201  		Id:                  name.MetricDescriptorId,
   202  	})
   203  }
   204  
   205  func (name *Name) GetIParentName() gotenresource.Name {
   206  	if name == nil {
   207  		return (*ParentName)(nil)
   208  	}
   209  	return &name.ParentName
   210  }
   211  
   212  func (name *Name) GetIUnderlyingParentName() gotenresource.Name {
   213  	if parentName := name.GetProjectName(); parentName != nil {
   214  		return parentName
   215  	}
   216  	return nil
   217  }
   218  
   219  // implement methods required by protobuf-go library for string-struct conversion
   220  
   221  func (name *Name) ProtoString() (string, error) {
   222  	if name == nil {
   223  		return "", nil
   224  	}
   225  	result := ""
   226  	parentPrefix, err := name.ParentName.ProtoString()
   227  	if err != nil {
   228  		return "", err
   229  	}
   230  	if parentPrefix != "" {
   231  		result += parentPrefix + "/"
   232  	}
   233  	result += "metricDescriptors/" + name.MetricDescriptorId
   234  	return result, nil
   235  }
   236  
   237  func (name *Name) ParseProtoString(data string) error {
   238  	parsed, err := ParseName(data)
   239  	if err != nil {
   240  		return err
   241  	}
   242  	*name = *parsed
   243  	return nil
   244  }
   245  
   246  // GotenEqual returns true if other is of same type and paths are equal (implements goten.Equaler interface)
   247  func (name *Name) GotenEqual(other interface{}) bool {
   248  	if other == nil {
   249  		return name == nil
   250  	}
   251  	other1, ok := other.(*Name)
   252  	if !ok {
   253  		other2, ok := other.(Name)
   254  		if ok {
   255  			other1 = &other2
   256  		} else {
   257  			return false
   258  		}
   259  	}
   260  	if other1 == nil {
   261  		return name == nil
   262  	} else if name == nil {
   263  		return false
   264  	}
   265  	if name.ParentName.GotenEqual(other1.ParentName) == false {
   266  		return false
   267  	}
   268  	if name.MetricDescriptorId != other1.MetricDescriptorId {
   269  		return false
   270  	}
   271  
   272  	return true
   273  }
   274  
   275  // Matches is same as GotenEqual, but also will accept "other" if name is wildcard.
   276  func (name *Name) Matches(other interface{}) bool {
   277  	if other == nil {
   278  		return name == nil
   279  	}
   280  	other1, ok := other.(*Name)
   281  	if !ok {
   282  		other2, ok := other.(Name)
   283  		if ok {
   284  			other1 = &other2
   285  		} else {
   286  			return false
   287  		}
   288  	}
   289  	if other1 == nil {
   290  		return name == nil
   291  	} else if name == nil {
   292  		return false
   293  	}
   294  	if name.ParentName.Matches(other1.ParentName) == false {
   295  		return false
   296  	}
   297  	if name.MetricDescriptorId != other1.MetricDescriptorId {
   298  		return name.MetricDescriptorId == gotenresource.WildcardId
   299  	}
   300  
   301  	return true
   302  }
   303  
   304  // implement CustomTypeCliValue method
   305  func (name *Name) SetFromCliFlag(raw string) error {
   306  	parsedName, err := ParseName(raw)
   307  	if err != nil {
   308  		return err
   309  	}
   310  	*name = *parsedName
   311  	return nil
   312  }
   313  
   314  type Reference struct {
   315  	Name
   316  	metricDescriptor *MetricDescriptor
   317  }
   318  
   319  func MakeReference(name *Name, metricDescriptor *MetricDescriptor) (*Reference, error) {
   320  	return &Reference{
   321  		Name:             *name,
   322  		metricDescriptor: metricDescriptor,
   323  	}, nil
   324  }
   325  
   326  func ParseReference(name string) (*Reference, error) {
   327  	parsedName, err := ParseName(name)
   328  	if err != nil {
   329  		return nil, err
   330  	}
   331  	return MakeReference(parsedName, nil)
   332  }
   333  
   334  func MustParseReference(name string) *Reference {
   335  	result, err := ParseReference(name)
   336  	if err != nil {
   337  		panic(err)
   338  	}
   339  	return result
   340  }
   341  
   342  func (ref *Reference) Resolve(resolved *MetricDescriptor) {
   343  	ref.metricDescriptor = resolved
   344  }
   345  
   346  func (ref *Reference) ResolveRaw(res gotenresource.Resource) error {
   347  	if res == nil {
   348  		ref.Resolve(nil)
   349  		return nil
   350  	}
   351  	if typedRes, ok := res.(*MetricDescriptor); ok {
   352  		ref.Resolve(typedRes)
   353  		return nil
   354  	}
   355  	return status.Errorf(codes.Internal, "Invalid resource type for MetricDescriptor: %s", reflect.TypeOf(res).Elem().Name())
   356  }
   357  
   358  func (ref *Reference) Resolved() bool {
   359  	return ref != nil && ref.metricDescriptor != nil
   360  }
   361  
   362  func (ref *Reference) ClearCached() {
   363  	ref.metricDescriptor = nil
   364  }
   365  
   366  func (ref *Reference) GetMetricDescriptor() *MetricDescriptor {
   367  	if ref == nil {
   368  		return nil
   369  	}
   370  	return ref.metricDescriptor
   371  }
   372  
   373  func (ref *Reference) GetRawResource() gotenresource.Resource {
   374  	if ref == nil {
   375  		return (*MetricDescriptor)(nil)
   376  	}
   377  	return ref.metricDescriptor
   378  }
   379  
   380  func (ref *Reference) IsFullyQualified() bool {
   381  	if ref == nil {
   382  		return false
   383  	}
   384  	return ref.Name.IsFullyQualified()
   385  }
   386  
   387  func (ref *Reference) IsSpecified() bool {
   388  	if ref == nil {
   389  		return false
   390  	}
   391  	return ref.Name.IsSpecified()
   392  }
   393  
   394  func (ref *Reference) FullyQualifiedName() (string, error) {
   395  	if !ref.IsFullyQualified() {
   396  		return "", status.Errorf(codes.InvalidArgument, "Name for MetricDescriptor is not fully qualified")
   397  	}
   398  	return fmt.Sprintf("//monitoring.edgelq.com/%s", ref.String()), nil
   399  }
   400  
   401  func (ref *Reference) GetResourceDescriptor() gotenresource.Descriptor {
   402  	return descriptor
   403  }
   404  
   405  func (ref *Reference) GetPattern() gotenresource.NamePattern {
   406  	if ref == nil {
   407  		return ""
   408  	}
   409  	return ref.Pattern
   410  }
   411  
   412  func (ref *Reference) GetIdParts() map[string]string {
   413  	if ref != nil {
   414  		return ref.Name.GetIdParts()
   415  	}
   416  	return map[string]string{
   417  		"projectId":          "",
   418  		"metricDescriptorId": "",
   419  	}
   420  }
   421  
   422  func (ref *Reference) GetSegments() gotenresource.NameSegments {
   423  	if ref != nil {
   424  		return ref.Name.GetSegments()
   425  	}
   426  	return nil
   427  }
   428  
   429  func (ref *Reference) GetIParentName() gotenresource.Name {
   430  	if ref == nil {
   431  		return (*ParentName)(nil)
   432  	}
   433  	return ref.Name.GetIParentName()
   434  }
   435  
   436  func (ref *Reference) GetIUnderlyingParentName() gotenresource.Name {
   437  	if ref != nil {
   438  		return ref.Name.GetIUnderlyingParentName()
   439  	}
   440  	return nil
   441  }
   442  
   443  func (ref *Reference) String() string {
   444  	if ref == nil {
   445  		return "<nil>"
   446  	}
   447  	return ref.Name.String()
   448  }
   449  
   450  // implement methods required by protobuf-go library for string-struct conversion
   451  
   452  func (ref *Reference) ProtoString() (string, error) {
   453  	if ref == nil {
   454  		return "", nil
   455  	}
   456  	return ref.Name.ProtoString()
   457  }
   458  
   459  func (ref *Reference) ParseProtoString(data string) error {
   460  	parsed, err := ParseReference(data)
   461  	if err != nil {
   462  		return err
   463  	}
   464  	*ref = *parsed
   465  	return nil
   466  }
   467  
   468  // GotenEqual returns true if other is of same type and paths are equal (implements goten.Equaler interface)
   469  func (ref *Reference) GotenEqual(other interface{}) bool {
   470  	if other == nil {
   471  		return ref == nil
   472  	}
   473  	other1, ok := other.(*Reference)
   474  	if !ok {
   475  		other2, ok := other.(Reference)
   476  		if ok {
   477  			other1 = &other2
   478  		} else {
   479  			return false
   480  		}
   481  	}
   482  	if other1 == nil {
   483  		return ref == nil
   484  	} else if ref == nil {
   485  		return false
   486  	}
   487  
   488  	return ref.Name.GotenEqual(other1.Name)
   489  }
   490  
   491  // Matches is same as GotenEqual, but also will accept "other" if name is wildcard.
   492  func (name *Reference) Matches(other interface{}) bool {
   493  	if other == nil {
   494  		return name == nil
   495  	}
   496  	other1, ok := other.(*Reference)
   497  	if !ok {
   498  		other2, ok := other.(Reference)
   499  		if ok {
   500  			other1 = &other2
   501  		} else {
   502  			return false
   503  		}
   504  	}
   505  	if other1 == nil {
   506  		return name == nil
   507  	} else if name == nil {
   508  		return false
   509  	}
   510  	return name.Name.Matches(&other1.Name)
   511  }
   512  
   513  // implement CustomTypeCliValue method
   514  func (ref *Reference) SetFromCliFlag(raw string) error {
   515  	parsedRef, err := ParseReference(raw)
   516  	if err != nil {
   517  		return err
   518  	}
   519  	*ref = *parsedRef
   520  	return nil
   521  }