Repository: mr-linch/go-tg (go)

Issue:
Sending files using urls always fails 
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x10481a3fc]

Pull request description:
fix(tg): send files by URL
close #113 

Patch:
diff --git a/request.go b/request.go
index 0d49c81..5b9ede9 100644
--- a/request.go
+++ b/request.go
@@ -76,11 +76,11 @@ func (r *Request) FileID(name string, v FileID) *Request {
 }
 
 func (r *Request) File(name string, arg FileArg) *Request {
-	if arg.FileID != "" {
-		return r.String(name, string(arg.FileID))
-	} else {
-		return r.InputFile(name, arg.Upload)
+	if arg.isRef() {
+		return r.String(name, arg.getRef())
 	}
+
+	return r.InputFile(name, arg.Upload)
 }
 
 func (r *Request) InputMediaSlice(name string, im []InputMedia) *Request {
@@ -99,7 +99,7 @@ func (r *Request) InputMedia(im InputMedia) *Request {
 	id := fmt.Sprintf("attachment_%d", r.attachmentIdx)
 	addr := fmt.Sprintf("attach://%s", id)
 
-	if media.getString() == "" {
+	if media.getRef() == "" {
 		r.InputFile(id, media.Upload)
 		media.addr = addr
 		r.attachmentIdx++
diff --git a/types_gen_ext.go b/types_gen_ext.go
index 57290f9..466cbb4 100644
--- a/types_gen_ext.go
+++ b/types_gen_ext.go
@@ -168,7 +168,7 @@ func NewFileArgID(id FileID) FileArg {
 }
 
 func (arg FileArg) MarshalJSON() ([]byte, error) {
-	str := arg.getString()
+	str := arg.getRef()
 	if str != "" {
 		return json.Marshal(str)
 	}
@@ -176,7 +176,13 @@ func (arg FileArg) MarshalJSON() ([]byte, error) {
 	return nil, fmt.Errorf("FileArg is not json serializable")
 }
 
-func (arg *FileArg) getString() string {
+// isRef returns true if FileArg is just reference
+func (arg *FileArg) isRef() bool {
+	return arg.FileID != "" || arg.URL != "" || arg.addr != ""
+}
+
+// getRef returns text representation of reference
+func (arg *FileArg) getRef() string {
 	if arg.FileID != "" {
 		return string(arg.FileID)
 	} else if arg.URL != "" {


Test patch:
diff --git a/request_test.go b/request_test.go
index 5bf4f09..763df6e 100644
--- a/request_test.go
+++ b/request_test.go
@@ -33,6 +33,9 @@ func TestRequest_Setters(t *testing.T) {
 	r.File("file_by_id", FileArg{
 		FileID: "file_id",
 	})
+	r.File("file_by_url", FileArg{
+		URL: "file_url",
+	})
 	r.File("file_input", FileArg{
 		Upload: NewInputFileBytes("file_name", []byte("file_content")),
 	})
@@ -61,6 +64,7 @@ func TestRequest_Setters(t *testing.T) {
 		"bool",
 		"chat",
 		"file_by_id",
+		"file_by_url",
 		"file_id",
 		"float64",
 		"foo",
diff --git a/types_gen_ext_test.go b/types_gen_ext_test.go
index 4f40198..0267bde 100644
--- a/types_gen_ext_test.go
+++ b/types_gen_ext_test.go
@@ -532,7 +532,7 @@ func TestFileArg_getString(t *testing.T) {
 			Want:    "addr",
 		},
 	} {
-		assert.Equal(t, test.Want, test.FileArg.getString())
+		assert.Equal(t, test.Want, test.FileArg.getRef())
 	}
 }