Sha256: fde4ed97777aa82e05cd898778ad82aaba2d154fb01b30ad0ff05ffe57a4f38c

Contents?: true

Size: 1.99 KB

Versions: 13

Compression:

Stored size: 1.99 KB

Contents

package internal

import (
	"encoding/json"
	"fmt"
  options "google.golang.org/genproto/googleapis/api/annotations"
  "google.golang.org/protobuf/proto"
  "google.golang.org/protobuf/types/descriptorpb"
	"regexp"
	"strings"
)

func MethodAndPath(pattern any) (string, string, error) {

	switch typedPattern := pattern.(type) {
	case *options.HttpRule_Get:
		return "GET", typedPattern.Get, nil
	case *options.HttpRule_Post:
		return "POST", typedPattern.Post, nil
	case *options.HttpRule_Put:
		return "PUT", typedPattern.Put, nil
	case *options.HttpRule_Delete:
		return "DELETE", typedPattern.Delete, nil
	case *options.HttpRule_Patch:
		return "PATCH", typedPattern.Patch, nil
	case *options.HttpRule_Custom:
		return typedPattern.Custom.Kind, typedPattern.Custom.Path, nil
	default:
		return "", "", fmt.Errorf("unknown pattern type %T", pattern)
	}
}

type PathInfo struct {
	Name string
	ValPattern string
	SplitName string
	HasValPattern bool
}

func ParsedPath(path string) ([]PathInfo, error) {
	var infos []PathInfo
	re := regexp.MustCompile("\\{(.*?)}")
	matches := re.FindAllString(path, -1)
	for _, match := range matches {
		name := match[1:len(match)-1]
		val := ""
		equal := strings.Index(match, "=")
		if equal != -1 {
			val = name[equal:]
			name = name[0:equal-1]
		}
		splitName := strings.Split(name, ".")
		jsonSplit, err := json.Marshal(splitName)
		if err != nil {
			return nil, fmt.Errorf("error marshalling splitName: %w", err)
		}
		infos = append(infos, PathInfo{
			Name: name,
			ValPattern: val,
			SplitName: string(jsonSplit),
			HasValPattern: val != "",
		})
	}
	return infos, nil
}

func ExtractAPIOptions(meth *descriptorpb.MethodDescriptorProto) (*options.HttpRule, error) {
	if meth.Options == nil {
		return nil, nil
	}
	if !proto.HasExtension(meth.Options, options.E_Http) {
		return nil, nil
	}
	ext := proto.GetExtension(meth.Options, options.E_Http)
	opts, ok := ext.(*options.HttpRule)
	if !ok {
		return nil, fmt.Errorf("extension is %T; want an HttpRule", ext)
	}
	return opts, nil
}


Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
grpc-rest-0.1.18 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.17 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.16 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.15 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.14 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.13 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.10 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.9 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.7 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.5 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.4 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.2 protoc-gen-rails/internal/parse.go
grpc-rest-0.1.1 protoc-gen-rails/internal/parse.go