From 7fdc89ac7e97592b0449c87b8bff05b3f4d8683b Mon Sep 17 00:00:00 2001 From: Costin M <4030027+costinmrr@users.noreply.github.com> Date: Tue, 12 Nov 2024 12:25:51 +0100 Subject: [PATCH] chore: readme (#2) * chore: .idea folder gitignore * chore: write readme --- .gitignore | 3 ++ .idea/.gitignore | 8 ---- .idea/gontenttype.iml | 9 ---- .idea/modules.xml | 8 ---- .idea/vcs.xml | 6 --- README.md | 102 +++++++++++++++++++++++++++++++++++++++++- gontenttype.go | 2 +- gontenttype_test.go | 52 +++++++++++++++++++++ 8 files changed, 157 insertions(+), 33 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/gontenttype.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml create mode 100644 gontenttype_test.go diff --git a/.gitignore b/.gitignore index 6f72f89..88c4f4f 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ go.work.sum # env file .env + +# jetbrains +.idea/ \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/gontenttype.iml b/.idea/gontenttype.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/gontenttype.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 97d308e..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index e89895f..0f91218 100644 --- a/README.md +++ b/README.md @@ -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 = `bar` + 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 = `bar` + err = xml.IsXML(myStr) + fmt.Println(err) // nil + + myStr = `bar +unexpected end of JSON input + +XML syntax error on line 1: unexpected EOF + +record on line 2: wrong number of fields +``` diff --git a/gontenttype.go b/gontenttype.go index 406bed7..fe3301e 100644 --- a/gontenttype.go +++ b/gontenttype.go @@ -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 diff --git a/gontenttype_test.go b/gontenttype_test.go new file mode 100644 index 0000000..0f0928c --- /dev/null +++ b/gontenttype_test.go @@ -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: "bar"}, + 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) + } + }) + } +}