Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tag literal to Go #18702

Closed
mei-rune opened this issue Jan 18, 2017 · 6 comments
Closed

Add more tag literal to Go #18702

mei-rune opened this issue Jan 18, 2017 · 6 comments

Comments

@mei-rune
Copy link

mei-rune commented Jan 18, 2017

I write a web application, hope write controller like spring mvc, as follows:

type Helloword struct {
    Controller

    Ctrl struct{}   `baseUrl:"/helloword"`
}

func (h *Helloword) Index(name `param:"name,maxLength=250,minLength=6"`  string) Result {
     `url:"index" method:“GET”`
    
      log(name, time.Now())
      return h.Render(R{"name": name})
}


func init() {
   RegistrController(&Helloword{})
}

In order to achieve this goal,I hope add Field with name is Tag and type is StructTag to reflect.Method,
add method with name is InTag to reflect.Type.

   ctrl := reflect.TypeOf(&Helloword())
   method, _ := ctrl.MethodByName("Index")
   fmt.Println(method.Tag.Get("url"))   // should print "index"
   fmt.Println(method.Tag.Get("method"))   // should print "GET"

   fmt.Println(method.Func.InTag(0).Get("param")) // shold print "name,maxLength=250,minLength=6"

Now, only add tag to field of struct. Can add more tag literal.

  1. add tag literal to func
func add(x, y int) int {
   `path:"/add";argument:"x" argument:"y"`
 
   xxxxxx
}

or

func add(x  `argument:"x"`, y  `argument:"y"`  int) int {
   `path:"/add"`
 
   xxxxxx
}
  1. -----dd tag literal to struct ---- (delete)
  struct {
   `name:"position_table" unique:"x+y"`

    X int
    Y int
  }
@minux
Copy link
Member

minux commented Jan 18, 2017 via email

@dsnet
Copy link
Member

dsnet commented Jan 18, 2017

Aside from a proposal write-up, what would be the use case for this?

@minux
Copy link
Member

minux commented Jan 18, 2017 via email

@myitcv
Copy link
Member

myitcv commented Jan 18, 2017

@minux I could only recall #15398 but there may be other related issues/proposals/go-nuts posts

What follows is not a reflection per se on the proposal but an example of how we're working around the lack of struct/function/method tags.

My use case for something like this is similar @minux... but is at go generate-time; so I can get away with "special comments" (I've made-up this term; special comments are single line comments with no space after the //) in the function/method/struct body:

func MyFunc() {
    //my:special:comment1
    //my:special:comment2

    // the rest of my function
}

func (s *S) method1() {
    //mapsTo=apple.Bite

    // the rest of my method
}

type S struct {
    //mapsTo=apple
}

We then have a simple check that ensures:

  • "special comments" are not part of the Doc *CommentGroup associated documentation of files/functions/imports etc
  • all "special comments" are in some way recognised (go generate is one case obviously)
  • special comments in function/method/struct bodies must appear before anything else, including other comments

Not ideal, but means, like your field-based solution, we can move on without a language change.

@mei-rune
Copy link
Author

mei-rune commented Jan 19, 2017

@myitcv How get "special comments" while program is running?

@bradfitz
Copy link
Contributor

Dup of declined proposal #15398. See discussion there.

@golang golang locked and limited conversation to collaborators Jan 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants