Skip to content

Commit

Permalink
chore: write readme
Browse files Browse the repository at this point in the history
  • Loading branch information
costinmrr committed Nov 12, 2024
1 parent 2417b9d commit 2cfdc76
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 2 deletions.
102 changes: 101 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,102 @@
# gontenttype
Detect the format type of a given string
1. **Detect the content type of a given string**
1. JSON (`application/json`)
2. XML (`application/xml`)
3. CSV (`text/csv`)
2. **Validate syntax for a given string and supported content types**


## Detect content type

### Example

#### Usage

```go
package main

import (
"fmt"
"github.com/costinmrr/gontenttype"
)

func main() {
// json
myStr := `{"foo":"bar"}`
contentType := gontenttype.Detect(myStr)
fmt.Println(contentType) // application/json

// xml
myStr = `<foo>bar</foo>`
contentType = gontenttype.Detect(myStr)
fmt.Println(contentType) // application/xml

// csv
myStr = `foo,bar`
contentType = gontenttype.Detect(myStr)
fmt.Println(contentType) // text/csv
}
```

#### Output

```shell
application/json
application/xml
text/csv
```

## Validate syntax

### Example

```go
package main

import (
"fmt"
"github.com/costinmrr/gontenttype/types/json"
"github.com/costinmrr/gontenttype/types/xml"
"github.com/costinmrr/gontenttype/types/csv"
)

func main() {
// json
myStr := `{"foo":"bar"}`
err := json.IsJSON(myStr)
fmt.Println(err) // nil

myStr = `{"foo":"bar"`
err = json.IsJSON(myStr)
fmt.Println(err) // unexpected end of JSON input

// xml
myStr = `<foo>bar</foo>`
err = xml.IsXML(myStr)
fmt.Println(err) // nil

myStr = `<foo>bar</foo`
err = xml.IsXML(myStr)
fmt.Println(err) // XML syntax error on line 1: unexpected EOF

// csv
myStr = `foo,bar`
err = csv.IsCSV(myStr)
fmt.Println(err) // nil

myStr = "col1,col2\nfoo,bar,baz"
err = csv.IsCSV(myStr)
fmt.Println(err) // record on line 2: wrong number of fields
}
```

#### Output

```shell
<nil>
unexpected end of JSON input
<nil>
XML syntax error on line 1: unexpected EOF
<nil>
record on line 2: wrong number of fields
```
2 changes: 1 addition & 1 deletion gontenttype.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/costinmrr/gontenttype/types/xml"
)

func GetContentType(content string) ContentType {
func Detect(content string) ContentType {
err := json.IsJSON(content)
if err == nil {
return JSON
Expand Down
52 changes: 52 additions & 0 deletions gontenttype_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package gontenttype

import "testing"

func TestDetect(t *testing.T) {
type args struct {
content string
}
tests := []struct {
name string
args args
want ContentType
}{
{
name: "empty",
args: args{content: ""},
want: Unsupported,
},
{
name: "random string defaults to csv",
args: args{content: "lorem ipsum dolor sit amet"},
want: CSV,
},
{
name: "random string with commas, semicolons, tabs, and pipes defaults to csv",
args: args{content: "foo,bar;baz\tqux|quux"},
want: CSV,
},
{
name: "csv comma separated",
args: args{content: "foo,bar\nbaz,qux"},
want: CSV,
},
{
name: "json",
args: args{content: "{\"foo\":\"bar\"}"},
want: JSON,
},
{
name: "xml",
args: args{content: "<foo>bar</foo>"},
want: XML,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Detect(tt.args.content); got != tt.want {
t.Errorf("Detect() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 2cfdc76

Please sign in to comment.