Skip to content

Commit da3d9ab

Browse files
committed
initial commit
0 parents  commit da3d9ab

File tree

5 files changed

+830
-0
lines changed

5 files changed

+830
-0
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: go
2+
go:
3+
- 1.7.x
4+
- tip

LICENSE

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Copyright (c) 2017, Fatih Arslan
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of structtag nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
This software includes some portions from Go. Go is used under the terms of the
30+
BSD like license.
31+
32+
Copyright (c) 2012 The Go Authors. All rights reserved.
33+
34+
Redistribution and use in source and binary forms, with or without
35+
modification, are permitted provided that the following conditions are
36+
met:
37+
38+
* Redistributions of source code must retain the above copyright
39+
notice, this list of conditions and the following disclaimer.
40+
* Redistributions in binary form must reproduce the above
41+
copyright notice, this list of conditions and the following disclaimer
42+
in the documentation and/or other materials provided with the
43+
distribution.
44+
* Neither the name of Google Inc. nor the names of its
45+
contributors may be used to endorse or promote products derived from
46+
this software without specific prior written permission.
47+
48+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
52+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59+
60+
The Go gopher was designed by Renee French. http://reneefrench.blogspot.com/ The design is licensed under the Creative Commons 3.0 Attributions license. Read this article for more details: https://blog.golang.org/gopher

README.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# structtag [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/fatih/structtag) [![Build Status](https://travis-ci.org/fatih/structtag.svg?branch=master)](https://travis-ci.org/fatih/structtag)
2+
3+
structtag provides an easy way of parsing and manipulating struct tag fields.
4+
Please vendor the library as it might change in future versions.
5+
6+
# Install
7+
8+
```bash
9+
go get github.com/fatih/structtag
10+
```
11+
12+
# Example
13+
14+
```go
15+
package main
16+
17+
import (
18+
"fmt"
19+
"reflect"
20+
"sort"
21+
22+
"github.com/fatih/structtag"
23+
)
24+
25+
func main() {
26+
type t struct {
27+
t string `json:"foo,omitempty,string" xml:"foo"`
28+
}
29+
30+
// get field tag
31+
tag := reflect.TypeOf(t{}).Field(0).Tag
32+
33+
// ... and start using structtag by parsing the tag
34+
tags, err := structtag.Parse(string(tag))
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
// iterate over all tags
40+
for _, t := range tags.Tags() {
41+
fmt.Printf("tag: %+v\n", t)
42+
}
43+
44+
// get a single tag
45+
jsonTag, err := tags.Get("json")
46+
if err != nil {
47+
panic(err)
48+
}
49+
fmt.Println(jsonTag) // Output: json:"foo,omitempty,string"
50+
fmt.Println(jsonTag.Key) // Output: json
51+
fmt.Println(jsonTag.Name) // Output: foo
52+
fmt.Println(jsonTag.Options) // Output: [omitempty string]
53+
54+
// change existing tag
55+
jsonTag.Name = "foo_bar"
56+
jsonTag.Options = nil
57+
tags.Set(jsonTag)
58+
59+
// add new tag
60+
tags.Set(&structtag.Tag{
61+
Key: "hcl",
62+
Name: "foo",
63+
Options: []string{"squash"},
64+
})
65+
66+
// print the tags
67+
fmt.Println(tags) // Output: json:"foo_bar" xml:"foo" hcl:"foo,squash"
68+
69+
// sort tags according to keys
70+
sort.Sort(tags)
71+
fmt.Println(tags) // Output: hcl:"foo,squash" json:"foo_bar" xml:"foo"
72+
}
73+
```

0 commit comments

Comments
 (0)