repo stringlengths 5 54 | path stringlengths 4 155 | func_name stringlengths 1 118 | original_string stringlengths 52 85.5k | language stringclasses 1 value | code stringlengths 52 85.5k | code_tokens list | docstring stringlengths 6 2.61k | docstring_tokens list | sha stringlengths 40 40 | url stringlengths 85 252 | partition stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|
mailgun/mailgun-go | mailgun.go | generateCredentialsUrl | func generateCredentialsUrl(m Mailgun, id string) string {
tail := ""
if id != "" {
tail = fmt.Sprintf("/%s", id)
}
return generateDomainApiUrl(m, fmt.Sprintf("credentials%s", tail))
// return fmt.Sprintf("%s/domains/%s/credentials%s", apiBase, m.Domain(), tail)
} | go | func generateCredentialsUrl(m Mailgun, id string) string {
tail := ""
if id != "" {
tail = fmt.Sprintf("/%s", id)
}
return generateDomainApiUrl(m, fmt.Sprintf("credentials%s", tail))
// return fmt.Sprintf("%s/domains/%s/credentials%s", apiBase, m.Domain(), tail)
} | [
"func",
"generateCredentialsUrl",
"(",
"m",
"Mailgun",
",",
"id",
"string",
")",
"string",
"{",
"tail",
":=",
"\"",
"\"",
"\n",
"if",
"id",
"!=",
"\"",
"\"",
"{",
"tail",
"=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"id",
")",
"\n",
"}",
"\n",
"return",
"generateDomainApiUrl",
"(",
"m",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"tail",
")",
")",
"\n",
"// return fmt.Sprintf(\"%s/domains/%s/credentials%s\", apiBase, m.Domain(), tail)",
"}"
] | // generateCredentialsUrl renders a URL as generateDomainApiUrl,
// but focuses on the SMTP credentials family of API functions. | [
"generateCredentialsUrl",
"renders",
"a",
"URL",
"as",
"generateDomainApiUrl",
"but",
"focuses",
"on",
"the",
"SMTP",
"credentials",
"family",
"of",
"API",
"functions",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/mailgun.go#L339-L346 | train |
mailgun/mailgun-go | mailgun.go | generateStoredMessageUrl | func generateStoredMessageUrl(m Mailgun, endpoint, id string) string {
return generateDomainApiUrl(m, fmt.Sprintf("%s/%s", endpoint, id))
// return fmt.Sprintf("%s/domains/%s/%s/%s", apiBase, m.Domain(), endpoint, id)
} | go | func generateStoredMessageUrl(m Mailgun, endpoint, id string) string {
return generateDomainApiUrl(m, fmt.Sprintf("%s/%s", endpoint, id))
// return fmt.Sprintf("%s/domains/%s/%s/%s", apiBase, m.Domain(), endpoint, id)
} | [
"func",
"generateStoredMessageUrl",
"(",
"m",
"Mailgun",
",",
"endpoint",
",",
"id",
"string",
")",
"string",
"{",
"return",
"generateDomainApiUrl",
"(",
"m",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"endpoint",
",",
"id",
")",
")",
"\n",
"// return fmt.Sprintf(\"%s/domains/%s/%s/%s\", apiBase, m.Domain(), endpoint, id)",
"}"
] | // generateStoredMessageUrl generates the URL needed to acquire a copy of a stored message. | [
"generateStoredMessageUrl",
"generates",
"the",
"URL",
"needed",
"to",
"acquire",
"a",
"copy",
"of",
"a",
"stored",
"message",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/mailgun.go#L349-L352 | train |
mailgun/mailgun-go | mailgun.go | generatePublicApiUrl | func generatePublicApiUrl(m Mailgun, endpoint string) string {
return fmt.Sprintf("%s/%s", m.APIBase(), endpoint)
} | go | func generatePublicApiUrl(m Mailgun, endpoint string) string {
return fmt.Sprintf("%s/%s", m.APIBase(), endpoint)
} | [
"func",
"generatePublicApiUrl",
"(",
"m",
"Mailgun",
",",
"endpoint",
"string",
")",
"string",
"{",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"m",
".",
"APIBase",
"(",
")",
",",
"endpoint",
")",
"\n",
"}"
] | // generatePublicApiUrl works as generateApiUrl, except that generatePublicApiUrl has no need for the domain. | [
"generatePublicApiUrl",
"works",
"as",
"generateApiUrl",
"except",
"that",
"generatePublicApiUrl",
"has",
"no",
"need",
"for",
"the",
"domain",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/mailgun.go#L355-L357 | train |
mailgun/mailgun-go | mailgun.go | generateParameterizedUrl | func generateParameterizedUrl(m Mailgun, endpoint string, payload payload) (string, error) {
paramBuffer, err := payload.getPayloadBuffer()
if err != nil {
return "", err
}
params := string(paramBuffer.Bytes())
return fmt.Sprintf("%s?%s", generateApiUrl(m, eventsEndpoint), params), nil
} | go | func generateParameterizedUrl(m Mailgun, endpoint string, payload payload) (string, error) {
paramBuffer, err := payload.getPayloadBuffer()
if err != nil {
return "", err
}
params := string(paramBuffer.Bytes())
return fmt.Sprintf("%s?%s", generateApiUrl(m, eventsEndpoint), params), nil
} | [
"func",
"generateParameterizedUrl",
"(",
"m",
"Mailgun",
",",
"endpoint",
"string",
",",
"payload",
"payload",
")",
"(",
"string",
",",
"error",
")",
"{",
"paramBuffer",
",",
"err",
":=",
"payload",
".",
"getPayloadBuffer",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"params",
":=",
"string",
"(",
"paramBuffer",
".",
"Bytes",
"(",
")",
")",
"\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"generateApiUrl",
"(",
"m",
",",
"eventsEndpoint",
")",
",",
"params",
")",
",",
"nil",
"\n",
"}"
] | // generateParameterizedUrl works as generateApiUrl, but supports query parameters. | [
"generateParameterizedUrl",
"works",
"as",
"generateApiUrl",
"but",
"supports",
"query",
"parameters",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/mailgun.go#L360-L367 | train |
mailgun/mailgun-go | mailgun.go | parseMailgunTime | func parseMailgunTime(ts string) (t time.Time, err error) {
t, err = time.Parse("Mon, 2 Jan 2006 15:04:05 MST", ts)
return
} | go | func parseMailgunTime(ts string) (t time.Time, err error) {
t, err = time.Parse("Mon, 2 Jan 2006 15:04:05 MST", ts)
return
} | [
"func",
"parseMailgunTime",
"(",
"ts",
"string",
")",
"(",
"t",
"time",
".",
"Time",
",",
"err",
"error",
")",
"{",
"t",
",",
"err",
"=",
"time",
".",
"Parse",
"(",
"\"",
"\"",
",",
"ts",
")",
"\n",
"return",
"\n",
"}"
] | // parseMailgunTime translates a timestamp as returned by Mailgun into a Go standard timestamp. | [
"parseMailgunTime",
"translates",
"a",
"timestamp",
"as",
"returned",
"by",
"Mailgun",
"into",
"a",
"Go",
"standard",
"timestamp",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/mailgun.go#L370-L373 | train |
mailgun/mailgun-go | parse.go | new_ | func new_(e interface{}) func() Event {
typ := reflect.TypeOf(e)
return func() Event {
return reflect.New(typ).Interface().(Event)
}
} | go | func new_(e interface{}) func() Event {
typ := reflect.TypeOf(e)
return func() Event {
return reflect.New(typ).Interface().(Event)
}
} | [
"func",
"new_",
"(",
"e",
"interface",
"{",
"}",
")",
"func",
"(",
")",
"Event",
"{",
"typ",
":=",
"reflect",
".",
"TypeOf",
"(",
"e",
")",
"\n",
"return",
"func",
"(",
")",
"Event",
"{",
"return",
"reflect",
".",
"New",
"(",
"typ",
")",
".",
"Interface",
"(",
")",
".",
"(",
"Event",
")",
"\n",
"}",
"\n",
"}"
] | // new_ is a universal event "constructor". | [
"new_",
"is",
"a",
"universal",
"event",
"constructor",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/parse.go#L41-L46 | train |
mailgun/mailgun-go | parse.go | ParseEvents | func ParseEvents(raw []events.RawJSON) ([]Event, error) {
var result []Event
for _, value := range raw {
event, err := ParseEvent(value)
if err != nil {
return nil, fmt.Errorf("while parsing event: %s", err)
}
result = append(result, event)
}
return result, nil
} | go | func ParseEvents(raw []events.RawJSON) ([]Event, error) {
var result []Event
for _, value := range raw {
event, err := ParseEvent(value)
if err != nil {
return nil, fmt.Errorf("while parsing event: %s", err)
}
result = append(result, event)
}
return result, nil
} | [
"func",
"ParseEvents",
"(",
"raw",
"[",
"]",
"events",
".",
"RawJSON",
")",
"(",
"[",
"]",
"Event",
",",
"error",
")",
"{",
"var",
"result",
"[",
"]",
"Event",
"\n",
"for",
"_",
",",
"value",
":=",
"range",
"raw",
"{",
"event",
",",
"err",
":=",
"ParseEvent",
"(",
"value",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"result",
"=",
"append",
"(",
"result",
",",
"event",
")",
"\n",
"}",
"\n",
"return",
"result",
",",
"nil",
"\n",
"}"
] | // Given a slice of events.RawJSON events return a slice of Event for each parsed event | [
"Given",
"a",
"slice",
"of",
"events",
".",
"RawJSON",
"events",
"return",
"a",
"slice",
"of",
"Event",
"for",
"each",
"parsed",
"event"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/parse.go#L66-L76 | train |
mailgun/mailgun-go | parse.go | ParseEvent | func ParseEvent(raw []byte) (Event, error) {
// Try to recognize the event first.
var e events.EventName
if err := easyjson.Unmarshal(raw, &e); err != nil {
return nil, fmt.Errorf("failed to recognize event: %v", err)
}
// Get the event "constructor" from the map.
newEvent, ok := EventNames[e.Name]
if !ok {
return nil, fmt.Errorf("unsupported event: '%s'", e.Name)
}
event := newEvent()
// Parse the known event.
if err := easyjson.Unmarshal(raw, event); err != nil {
return nil, fmt.Errorf("failed to parse event '%s': %v", e.Name, err)
}
return event, nil
} | go | func ParseEvent(raw []byte) (Event, error) {
// Try to recognize the event first.
var e events.EventName
if err := easyjson.Unmarshal(raw, &e); err != nil {
return nil, fmt.Errorf("failed to recognize event: %v", err)
}
// Get the event "constructor" from the map.
newEvent, ok := EventNames[e.Name]
if !ok {
return nil, fmt.Errorf("unsupported event: '%s'", e.Name)
}
event := newEvent()
// Parse the known event.
if err := easyjson.Unmarshal(raw, event); err != nil {
return nil, fmt.Errorf("failed to parse event '%s': %v", e.Name, err)
}
return event, nil
} | [
"func",
"ParseEvent",
"(",
"raw",
"[",
"]",
"byte",
")",
"(",
"Event",
",",
"error",
")",
"{",
"// Try to recognize the event first.",
"var",
"e",
"events",
".",
"EventName",
"\n",
"if",
"err",
":=",
"easyjson",
".",
"Unmarshal",
"(",
"raw",
",",
"&",
"e",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n\n",
"// Get the event \"constructor\" from the map.",
"newEvent",
",",
"ok",
":=",
"EventNames",
"[",
"e",
".",
"Name",
"]",
"\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"e",
".",
"Name",
")",
"\n",
"}",
"\n",
"event",
":=",
"newEvent",
"(",
")",
"\n\n",
"// Parse the known event.",
"if",
"err",
":=",
"easyjson",
".",
"Unmarshal",
"(",
"raw",
",",
"event",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"e",
".",
"Name",
",",
"err",
")",
"\n",
"}",
"\n\n",
"return",
"event",
",",
"nil",
"\n",
"}"
] | // Parse converts raw bytes data into an event struct. Can accept events.RawJSON as input | [
"Parse",
"converts",
"raw",
"bytes",
"data",
"into",
"an",
"event",
"struct",
".",
"Can",
"accept",
"events",
".",
"RawJSON",
"as",
"input"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/parse.go#L79-L99 | train |
mailgun/mailgun-go | unsubscribes.go | ListUnsubscribes | func (mg *MailgunImpl) ListUnsubscribes(opts *ListOptions) *UnsubscribesIterator {
r := newHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
if opts != nil {
if opts.Limit != 0 {
r.addParameter("limit", strconv.Itoa(opts.Limit))
}
}
url, err := r.generateUrlWithParameters()
return &UnsubscribesIterator{
mg: mg,
unsubscribesResponse: unsubscribesResponse{Paging: Paging{Next: url, First: url}},
err: err,
}
} | go | func (mg *MailgunImpl) ListUnsubscribes(opts *ListOptions) *UnsubscribesIterator {
r := newHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
if opts != nil {
if opts.Limit != 0 {
r.addParameter("limit", strconv.Itoa(opts.Limit))
}
}
url, err := r.generateUrlWithParameters()
return &UnsubscribesIterator{
mg: mg,
unsubscribesResponse: unsubscribesResponse{Paging: Paging{Next: url, First: url}},
err: err,
}
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"ListUnsubscribes",
"(",
"opts",
"*",
"ListOptions",
")",
"*",
"UnsubscribesIterator",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"unsubscribesEndpoint",
")",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"if",
"opts",
"!=",
"nil",
"{",
"if",
"opts",
".",
"Limit",
"!=",
"0",
"{",
"r",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"strconv",
".",
"Itoa",
"(",
"opts",
".",
"Limit",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"url",
",",
"err",
":=",
"r",
".",
"generateUrlWithParameters",
"(",
")",
"\n",
"return",
"&",
"UnsubscribesIterator",
"{",
"mg",
":",
"mg",
",",
"unsubscribesResponse",
":",
"unsubscribesResponse",
"{",
"Paging",
":",
"Paging",
"{",
"Next",
":",
"url",
",",
"First",
":",
"url",
"}",
"}",
",",
"err",
":",
"err",
",",
"}",
"\n",
"}"
] | // Fetches the list of unsubscribes | [
"Fetches",
"the",
"list",
"of",
"unsubscribes"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/unsubscribes.go#L21-L36 | train |
mailgun/mailgun-go | unsubscribes.go | GetUnsubscribe | func (mg *MailgunImpl) GetUnsubscribe(ctx context.Context, address string) (Unsubscribe, error) {
r := newHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, address))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
envelope := Unsubscribe{}
err := getResponseFromJSON(ctx, r, &envelope)
return envelope, err
} | go | func (mg *MailgunImpl) GetUnsubscribe(ctx context.Context, address string) (Unsubscribe, error) {
r := newHTTPRequest(generateApiUrlWithTarget(mg, unsubscribesEndpoint, address))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
envelope := Unsubscribe{}
err := getResponseFromJSON(ctx, r, &envelope)
return envelope, err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"GetUnsubscribe",
"(",
"ctx",
"context",
".",
"Context",
",",
"address",
"string",
")",
"(",
"Unsubscribe",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrlWithTarget",
"(",
"mg",
",",
"unsubscribesEndpoint",
",",
"address",
")",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n\n",
"envelope",
":=",
"Unsubscribe",
"{",
"}",
"\n",
"err",
":=",
"getResponseFromJSON",
"(",
"ctx",
",",
"r",
",",
"&",
"envelope",
")",
"\n\n",
"return",
"envelope",
",",
"err",
"\n",
"}"
] | // Retreives a single unsubscribe record. Can be used to check if a given address is present in the list of unsubscribed users. | [
"Retreives",
"a",
"single",
"unsubscribe",
"record",
".",
"Can",
"be",
"used",
"to",
"check",
"if",
"a",
"given",
"address",
"is",
"present",
"in",
"the",
"list",
"of",
"unsubscribed",
"users",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/unsubscribes.go#L136-L145 | train |
mailgun/mailgun-go | unsubscribes.go | CreateUnsubscribe | func (mg *MailgunImpl) CreateUnsubscribe(ctx context.Context, address, tag string) error {
r := newHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
p.addValue("address", address)
p.addValue("tag", tag)
_, err := makePostRequest(ctx, r, p)
return err
} | go | func (mg *MailgunImpl) CreateUnsubscribe(ctx context.Context, address, tag string) error {
r := newHTTPRequest(generateApiUrl(mg, unsubscribesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
p.addValue("address", address)
p.addValue("tag", tag)
_, err := makePostRequest(ctx, r, p)
return err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"CreateUnsubscribe",
"(",
"ctx",
"context",
".",
"Context",
",",
"address",
",",
"tag",
"string",
")",
"error",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"unsubscribesEndpoint",
")",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"p",
":=",
"newUrlEncodedPayload",
"(",
")",
"\n",
"p",
".",
"addValue",
"(",
"\"",
"\"",
",",
"address",
")",
"\n",
"p",
".",
"addValue",
"(",
"\"",
"\"",
",",
"tag",
")",
"\n",
"_",
",",
"err",
":=",
"makePostRequest",
"(",
"ctx",
",",
"r",
",",
"p",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // Unsubscribe adds an e-mail address to the domain's unsubscription table. | [
"Unsubscribe",
"adds",
"an",
"e",
"-",
"mail",
"address",
"to",
"the",
"domain",
"s",
"unsubscription",
"table",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/unsubscribes.go#L148-L157 | train |
mailgun/mailgun-go | tags.go | DeleteTag | func (mg *MailgunImpl) DeleteTag(ctx context.Context, tag string) error {
r := newHTTPRequest(generateApiUrl(mg, tagsEndpoint) + "/" + tag)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
} | go | func (mg *MailgunImpl) DeleteTag(ctx context.Context, tag string) error {
r := newHTTPRequest(generateApiUrl(mg, tagsEndpoint) + "/" + tag)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"DeleteTag",
"(",
"ctx",
"context",
".",
"Context",
",",
"tag",
"string",
")",
"error",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"tagsEndpoint",
")",
"+",
"\"",
"\"",
"+",
"tag",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"_",
",",
"err",
":=",
"makeDeleteRequest",
"(",
"ctx",
",",
"r",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // DeleteTag removes all counters for a particular tag, including the tag itself. | [
"DeleteTag",
"removes",
"all",
"counters",
"for",
"a",
"particular",
"tag",
"including",
"the",
"tag",
"itself",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/tags.go#L30-L36 | train |
mailgun/mailgun-go | tags.go | GetTag | func (mg *MailgunImpl) GetTag(ctx context.Context, tag string) (Tag, error) {
r := newHTTPRequest(generateApiUrl(mg, tagsEndpoint) + "/" + tag)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var tagItem Tag
return tagItem, getResponseFromJSON(ctx, r, &tagItem)
} | go | func (mg *MailgunImpl) GetTag(ctx context.Context, tag string) (Tag, error) {
r := newHTTPRequest(generateApiUrl(mg, tagsEndpoint) + "/" + tag)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var tagItem Tag
return tagItem, getResponseFromJSON(ctx, r, &tagItem)
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"GetTag",
"(",
"ctx",
"context",
".",
"Context",
",",
"tag",
"string",
")",
"(",
"Tag",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"tagsEndpoint",
")",
"+",
"\"",
"\"",
"+",
"tag",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"var",
"tagItem",
"Tag",
"\n",
"return",
"tagItem",
",",
"getResponseFromJSON",
"(",
"ctx",
",",
"r",
",",
"&",
"tagItem",
")",
"\n",
"}"
] | // GetTag retrieves metadata about the tag from the api | [
"GetTag",
"retrieves",
"metadata",
"about",
"the",
"tag",
"from",
"the",
"api"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/tags.go#L39-L45 | train |
mailgun/mailgun-go | tags.go | Next | func (ti *TagIterator) Next(ctx context.Context, items *[]Tag) bool {
if ti.err != nil {
return false
}
if !canFetchPage(ti.Paging.Next) {
return false
}
ti.err = ti.fetch(ctx, ti.Paging.Next)
if ti.err != nil {
return false
}
*items = ti.Items
if len(ti.Items) == 0 {
return false
}
return true
} | go | func (ti *TagIterator) Next(ctx context.Context, items *[]Tag) bool {
if ti.err != nil {
return false
}
if !canFetchPage(ti.Paging.Next) {
return false
}
ti.err = ti.fetch(ctx, ti.Paging.Next)
if ti.err != nil {
return false
}
*items = ti.Items
if len(ti.Items) == 0 {
return false
}
return true
} | [
"func",
"(",
"ti",
"*",
"TagIterator",
")",
"Next",
"(",
"ctx",
"context",
".",
"Context",
",",
"items",
"*",
"[",
"]",
"Tag",
")",
"bool",
"{",
"if",
"ti",
".",
"err",
"!=",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"if",
"!",
"canFetchPage",
"(",
"ti",
".",
"Paging",
".",
"Next",
")",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"ti",
".",
"err",
"=",
"ti",
".",
"fetch",
"(",
"ctx",
",",
"ti",
".",
"Paging",
".",
"Next",
")",
"\n",
"if",
"ti",
".",
"err",
"!=",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"*",
"items",
"=",
"ti",
".",
"Items",
"\n",
"if",
"len",
"(",
"ti",
".",
"Items",
")",
"==",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] | // Next returns the next page in the list of tags | [
"Next",
"returns",
"the",
"next",
"page",
"in",
"the",
"list",
"of",
"tags"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/tags.go#L84-L102 | train |
mailgun/mailgun-go | tags.go | First | func (ti *TagIterator) First(ctx context.Context, items *[]Tag) bool {
if ti.err != nil {
return false
}
ti.err = ti.fetch(ctx, ti.Paging.First)
if ti.err != nil {
return false
}
*items = ti.Items
return true
} | go | func (ti *TagIterator) First(ctx context.Context, items *[]Tag) bool {
if ti.err != nil {
return false
}
ti.err = ti.fetch(ctx, ti.Paging.First)
if ti.err != nil {
return false
}
*items = ti.Items
return true
} | [
"func",
"(",
"ti",
"*",
"TagIterator",
")",
"First",
"(",
"ctx",
"context",
".",
"Context",
",",
"items",
"*",
"[",
"]",
"Tag",
")",
"bool",
"{",
"if",
"ti",
".",
"err",
"!=",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"ti",
".",
"err",
"=",
"ti",
".",
"fetch",
"(",
"ctx",
",",
"ti",
".",
"Paging",
".",
"First",
")",
"\n",
"if",
"ti",
".",
"err",
"!=",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n",
"*",
"items",
"=",
"ti",
".",
"Items",
"\n",
"return",
"true",
"\n",
"}"
] | // First returns the first page in the list of tags | [
"First",
"returns",
"the",
"first",
"page",
"in",
"the",
"list",
"of",
"tags"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/tags.go#L130-L140 | train |
mailgun/mailgun-go | mailing_lists.go | ListMailingLists | func (mg *MailgunImpl) ListMailingLists(opts *ListOptions) *ListsIterator {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/pages")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
if opts != nil {
if opts.Limit != 0 {
r.addParameter("limit", strconv.Itoa(opts.Limit))
}
}
url, err := r.generateUrlWithParameters()
return &ListsIterator{
mg: mg,
listsResponse: listsResponse{Paging: Paging{Next: url, First: url}},
err: err,
}
} | go | func (mg *MailgunImpl) ListMailingLists(opts *ListOptions) *ListsIterator {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/pages")
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
if opts != nil {
if opts.Limit != 0 {
r.addParameter("limit", strconv.Itoa(opts.Limit))
}
}
url, err := r.generateUrlWithParameters()
return &ListsIterator{
mg: mg,
listsResponse: listsResponse{Paging: Paging{Next: url, First: url}},
err: err,
}
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"ListMailingLists",
"(",
"opts",
"*",
"ListOptions",
")",
"*",
"ListsIterator",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generatePublicApiUrl",
"(",
"mg",
",",
"listsEndpoint",
")",
"+",
"\"",
"\"",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"if",
"opts",
"!=",
"nil",
"{",
"if",
"opts",
".",
"Limit",
"!=",
"0",
"{",
"r",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"strconv",
".",
"Itoa",
"(",
"opts",
".",
"Limit",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"url",
",",
"err",
":=",
"r",
".",
"generateUrlWithParameters",
"(",
")",
"\n",
"return",
"&",
"ListsIterator",
"{",
"mg",
":",
"mg",
",",
"listsResponse",
":",
"listsResponse",
"{",
"Paging",
":",
"Paging",
"{",
"Next",
":",
"url",
",",
"First",
":",
"url",
"}",
"}",
",",
"err",
":",
"err",
",",
"}",
"\n",
"}"
] | // ListMailingLists returns the specified set of mailing lists administered by your account. | [
"ListMailingLists",
"returns",
"the",
"specified",
"set",
"of",
"mailing",
"lists",
"administered",
"by",
"your",
"account",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/mailing_lists.go#L53-L68 | train |
mailgun/mailgun-go | mailing_lists.go | DeleteMailingList | func (mg *MailgunImpl) DeleteMailingList(ctx context.Context, addr string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/" + addr)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
} | go | func (mg *MailgunImpl) DeleteMailingList(ctx context.Context, addr string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/" + addr)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"DeleteMailingList",
"(",
"ctx",
"context",
".",
"Context",
",",
"addr",
"string",
")",
"error",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generatePublicApiUrl",
"(",
"mg",
",",
"listsEndpoint",
")",
"+",
"\"",
"\"",
"+",
"addr",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"_",
",",
"err",
":=",
"makeDeleteRequest",
"(",
"ctx",
",",
"r",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // DeleteMailingList removes all current members of the list, then removes the list itself.
// Attempts to send e-mail to the list will fail subsequent to this call. | [
"DeleteMailingList",
"removes",
"all",
"current",
"members",
"of",
"the",
"list",
"then",
"removes",
"the",
"list",
"itself",
".",
"Attempts",
"to",
"send",
"e",
"-",
"mail",
"to",
"the",
"list",
"will",
"fail",
"subsequent",
"to",
"this",
"call",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/mailing_lists.go#L194-L200 | train |
mailgun/mailgun-go | mailing_lists.go | GetMailingList | func (mg *MailgunImpl) GetMailingList(ctx context.Context, addr string) (MailingList, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/" + addr)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
response, err := makeGetRequest(ctx, r)
if err != nil {
return MailingList{}, err
}
var resp mailingListResponse
err = response.parseFromJSON(&resp)
return resp.MailingList, err
} | go | func (mg *MailgunImpl) GetMailingList(ctx context.Context, addr string) (MailingList, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/" + addr)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
response, err := makeGetRequest(ctx, r)
if err != nil {
return MailingList{}, err
}
var resp mailingListResponse
err = response.parseFromJSON(&resp)
return resp.MailingList, err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"GetMailingList",
"(",
"ctx",
"context",
".",
"Context",
",",
"addr",
"string",
")",
"(",
"MailingList",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generatePublicApiUrl",
"(",
"mg",
",",
"listsEndpoint",
")",
"+",
"\"",
"\"",
"+",
"addr",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"response",
",",
"err",
":=",
"makeGetRequest",
"(",
"ctx",
",",
"r",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"MailingList",
"{",
"}",
",",
"err",
"\n",
"}",
"\n\n",
"var",
"resp",
"mailingListResponse",
"\n",
"err",
"=",
"response",
".",
"parseFromJSON",
"(",
"&",
"resp",
")",
"\n",
"return",
"resp",
".",
"MailingList",
",",
"err",
"\n",
"}"
] | // GetMailingList allows your application to recover the complete List structure
// representing a mailing list, so long as you have its e-mail address. | [
"GetMailingList",
"allows",
"your",
"application",
"to",
"recover",
"the",
"complete",
"List",
"structure",
"representing",
"a",
"mailing",
"list",
"so",
"long",
"as",
"you",
"have",
"its",
"e",
"-",
"mail",
"address",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/mailing_lists.go#L204-L216 | train |
mailgun/mailgun-go | mailing_lists.go | UpdateMailingList | func (mg *MailgunImpl) UpdateMailingList(ctx context.Context, addr string, prototype MailingList) (MailingList, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/" + addr)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
if prototype.Address != "" {
p.addValue("address", prototype.Address)
}
if prototype.Name != "" {
p.addValue("name", prototype.Name)
}
if prototype.Description != "" {
p.addValue("description", prototype.Description)
}
if prototype.AccessLevel != "" {
p.addValue("access_level", string(prototype.AccessLevel))
}
var l MailingList
response, err := makePutRequest(ctx, r, p)
if err != nil {
return l, err
}
err = response.parseFromJSON(&l)
return l, err
} | go | func (mg *MailgunImpl) UpdateMailingList(ctx context.Context, addr string, prototype MailingList) (MailingList, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, listsEndpoint) + "/" + addr)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
if prototype.Address != "" {
p.addValue("address", prototype.Address)
}
if prototype.Name != "" {
p.addValue("name", prototype.Name)
}
if prototype.Description != "" {
p.addValue("description", prototype.Description)
}
if prototype.AccessLevel != "" {
p.addValue("access_level", string(prototype.AccessLevel))
}
var l MailingList
response, err := makePutRequest(ctx, r, p)
if err != nil {
return l, err
}
err = response.parseFromJSON(&l)
return l, err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"UpdateMailingList",
"(",
"ctx",
"context",
".",
"Context",
",",
"addr",
"string",
",",
"prototype",
"MailingList",
")",
"(",
"MailingList",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generatePublicApiUrl",
"(",
"mg",
",",
"listsEndpoint",
")",
"+",
"\"",
"\"",
"+",
"addr",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"p",
":=",
"newUrlEncodedPayload",
"(",
")",
"\n",
"if",
"prototype",
".",
"Address",
"!=",
"\"",
"\"",
"{",
"p",
".",
"addValue",
"(",
"\"",
"\"",
",",
"prototype",
".",
"Address",
")",
"\n",
"}",
"\n",
"if",
"prototype",
".",
"Name",
"!=",
"\"",
"\"",
"{",
"p",
".",
"addValue",
"(",
"\"",
"\"",
",",
"prototype",
".",
"Name",
")",
"\n",
"}",
"\n",
"if",
"prototype",
".",
"Description",
"!=",
"\"",
"\"",
"{",
"p",
".",
"addValue",
"(",
"\"",
"\"",
",",
"prototype",
".",
"Description",
")",
"\n",
"}",
"\n",
"if",
"prototype",
".",
"AccessLevel",
"!=",
"\"",
"\"",
"{",
"p",
".",
"addValue",
"(",
"\"",
"\"",
",",
"string",
"(",
"prototype",
".",
"AccessLevel",
")",
")",
"\n",
"}",
"\n",
"var",
"l",
"MailingList",
"\n",
"response",
",",
"err",
":=",
"makePutRequest",
"(",
"ctx",
",",
"r",
",",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"l",
",",
"err",
"\n",
"}",
"\n",
"err",
"=",
"response",
".",
"parseFromJSON",
"(",
"&",
"l",
")",
"\n",
"return",
"l",
",",
"err",
"\n",
"}"
] | // UpdateMailingList allows you to change various attributes of a list.
// Address, Name, Description, and AccessLevel are all optional;
// only those fields which are set in the prototype will change.
//
// Be careful! If changing the address of a mailing list,
// e-mail sent to the old address will not succeed.
// Make sure you account for the change accordingly. | [
"UpdateMailingList",
"allows",
"you",
"to",
"change",
"various",
"attributes",
"of",
"a",
"list",
".",
"Address",
"Name",
"Description",
"and",
"AccessLevel",
"are",
"all",
"optional",
";",
"only",
"those",
"fields",
"which",
"are",
"set",
"in",
"the",
"prototype",
"will",
"change",
".",
"Be",
"careful!",
"If",
"changing",
"the",
"address",
"of",
"a",
"mailing",
"list",
"e",
"-",
"mail",
"sent",
"to",
"the",
"old",
"address",
"will",
"not",
"succeed",
".",
"Make",
"sure",
"you",
"account",
"for",
"the",
"change",
"accordingly",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/mailing_lists.go#L225-L249 | train |
mailgun/mailgun-go | stats.go | GetStats | func (mg *MailgunImpl) GetStats(ctx context.Context, events []string, opts *GetStatOptions) ([]Stats, error) {
r := newHTTPRequest(generateApiUrl(mg, statsTotalEndpoint))
if opts != nil {
if !opts.Start.IsZero() {
r.addParameter("start", opts.Start.Format(iso8601date))
}
if !opts.End.IsZero() {
r.addParameter("end", opts.End.Format(iso8601date))
}
if opts.Resolution != "" {
r.addParameter("resolution", string(opts.Resolution))
}
if opts.Duration != "" {
r.addParameter("duration", opts.Duration)
}
}
for _, e := range events {
r.addParameter("event", e)
}
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var res statsTotalResponse
err := getResponseFromJSON(ctx, r, &res)
if err != nil {
return nil, err
} else {
return res.Stats, nil
}
} | go | func (mg *MailgunImpl) GetStats(ctx context.Context, events []string, opts *GetStatOptions) ([]Stats, error) {
r := newHTTPRequest(generateApiUrl(mg, statsTotalEndpoint))
if opts != nil {
if !opts.Start.IsZero() {
r.addParameter("start", opts.Start.Format(iso8601date))
}
if !opts.End.IsZero() {
r.addParameter("end", opts.End.Format(iso8601date))
}
if opts.Resolution != "" {
r.addParameter("resolution", string(opts.Resolution))
}
if opts.Duration != "" {
r.addParameter("duration", opts.Duration)
}
}
for _, e := range events {
r.addParameter("event", e)
}
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var res statsTotalResponse
err := getResponseFromJSON(ctx, r, &res)
if err != nil {
return nil, err
} else {
return res.Stats, nil
}
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"GetStats",
"(",
"ctx",
"context",
".",
"Context",
",",
"events",
"[",
"]",
"string",
",",
"opts",
"*",
"GetStatOptions",
")",
"(",
"[",
"]",
"Stats",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"statsTotalEndpoint",
")",
")",
"\n\n",
"if",
"opts",
"!=",
"nil",
"{",
"if",
"!",
"opts",
".",
"Start",
".",
"IsZero",
"(",
")",
"{",
"r",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"opts",
".",
"Start",
".",
"Format",
"(",
"iso8601date",
")",
")",
"\n",
"}",
"\n",
"if",
"!",
"opts",
".",
"End",
".",
"IsZero",
"(",
")",
"{",
"r",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"opts",
".",
"End",
".",
"Format",
"(",
"iso8601date",
")",
")",
"\n",
"}",
"\n",
"if",
"opts",
".",
"Resolution",
"!=",
"\"",
"\"",
"{",
"r",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"string",
"(",
"opts",
".",
"Resolution",
")",
")",
"\n",
"}",
"\n",
"if",
"opts",
".",
"Duration",
"!=",
"\"",
"\"",
"{",
"r",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"opts",
".",
"Duration",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"e",
":=",
"range",
"events",
"{",
"r",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"e",
")",
"\n",
"}",
"\n\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n\n",
"var",
"res",
"statsTotalResponse",
"\n",
"err",
":=",
"getResponseFromJSON",
"(",
"ctx",
",",
"r",
",",
"&",
"res",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"else",
"{",
"return",
"res",
".",
"Stats",
",",
"nil",
"\n",
"}",
"\n",
"}"
] | // GetStats returns total stats for a given domain for the specified time period | [
"GetStats",
"returns",
"total",
"stats",
"for",
"a",
"given",
"domain",
"for",
"the",
"specified",
"time",
"period"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/stats.go#L89-L121 | train |
mailgun/mailgun-go | events.go | ListEvents | func (mg *MailgunImpl) ListEvents(opts *ListEventOptions) *EventIterator {
req := newHTTPRequest(generateApiUrl(mg, eventsEndpoint))
if opts != nil {
if opts.Limit > 0 {
req.addParameter("limit", fmt.Sprintf("%d", opts.Limit))
}
if opts.Compact {
req.addParameter("pretty", "no")
}
if opts.ForceAscending {
req.addParameter("ascending", "yes")
} else if opts.ForceDescending {
req.addParameter("ascending", "no")
}
if !opts.Begin.IsZero() {
req.addParameter("begin", formatMailgunTime(opts.Begin))
}
if !opts.End.IsZero() {
req.addParameter("end", formatMailgunTime(opts.End))
}
if opts.Filter != nil {
for k, v := range opts.Filter {
req.addParameter(k, v)
}
}
}
url, err := req.generateUrlWithParameters()
return &EventIterator{
mg: mg,
Response: events.Response{Paging: events.Paging{Next: url, First: url}},
err: err,
}
} | go | func (mg *MailgunImpl) ListEvents(opts *ListEventOptions) *EventIterator {
req := newHTTPRequest(generateApiUrl(mg, eventsEndpoint))
if opts != nil {
if opts.Limit > 0 {
req.addParameter("limit", fmt.Sprintf("%d", opts.Limit))
}
if opts.Compact {
req.addParameter("pretty", "no")
}
if opts.ForceAscending {
req.addParameter("ascending", "yes")
} else if opts.ForceDescending {
req.addParameter("ascending", "no")
}
if !opts.Begin.IsZero() {
req.addParameter("begin", formatMailgunTime(opts.Begin))
}
if !opts.End.IsZero() {
req.addParameter("end", formatMailgunTime(opts.End))
}
if opts.Filter != nil {
for k, v := range opts.Filter {
req.addParameter(k, v)
}
}
}
url, err := req.generateUrlWithParameters()
return &EventIterator{
mg: mg,
Response: events.Response{Paging: events.Paging{Next: url, First: url}},
err: err,
}
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"ListEvents",
"(",
"opts",
"*",
"ListEventOptions",
")",
"*",
"EventIterator",
"{",
"req",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"eventsEndpoint",
")",
")",
"\n",
"if",
"opts",
"!=",
"nil",
"{",
"if",
"opts",
".",
"Limit",
">",
"0",
"{",
"req",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"opts",
".",
"Limit",
")",
")",
"\n",
"}",
"\n",
"if",
"opts",
".",
"Compact",
"{",
"req",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"opts",
".",
"ForceAscending",
"{",
"req",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"}",
"else",
"if",
"opts",
".",
"ForceDescending",
"{",
"req",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"!",
"opts",
".",
"Begin",
".",
"IsZero",
"(",
")",
"{",
"req",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"formatMailgunTime",
"(",
"opts",
".",
"Begin",
")",
")",
"\n",
"}",
"\n",
"if",
"!",
"opts",
".",
"End",
".",
"IsZero",
"(",
")",
"{",
"req",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"formatMailgunTime",
"(",
"opts",
".",
"End",
")",
")",
"\n",
"}",
"\n",
"if",
"opts",
".",
"Filter",
"!=",
"nil",
"{",
"for",
"k",
",",
"v",
":=",
"range",
"opts",
".",
"Filter",
"{",
"req",
".",
"addParameter",
"(",
"k",
",",
"v",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"url",
",",
"err",
":=",
"req",
".",
"generateUrlWithParameters",
"(",
")",
"\n",
"return",
"&",
"EventIterator",
"{",
"mg",
":",
"mg",
",",
"Response",
":",
"events",
".",
"Response",
"{",
"Paging",
":",
"events",
".",
"Paging",
"{",
"Next",
":",
"url",
",",
"First",
":",
"url",
"}",
"}",
",",
"err",
":",
"err",
",",
"}",
"\n",
"}"
] | // Create an new iterator to fetch a page of events from the events api | [
"Create",
"an",
"new",
"iterator",
"to",
"fetch",
"a",
"page",
"of",
"events",
"from",
"the",
"events",
"api"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/events.go#L39-L71 | train |
mailgun/mailgun-go | recipients.go | UnmarshalText | func (r *Recipient) UnmarshalText(text []byte) error {
s := string(text)
if s[len(s)-1:] != ">" {
*r = Recipient{Email: s}
return nil
}
i := strings.Index(s, "<")
// at least 1 char followed by a space
if i < 2 {
return fmt.Errorf("malformed recipient string '%s'", s)
}
*r = Recipient{
Name: strings.TrimSpace(s[:i]),
Email: s[i+1 : len(s)-1],
}
return nil
} | go | func (r *Recipient) UnmarshalText(text []byte) error {
s := string(text)
if s[len(s)-1:] != ">" {
*r = Recipient{Email: s}
return nil
}
i := strings.Index(s, "<")
// at least 1 char followed by a space
if i < 2 {
return fmt.Errorf("malformed recipient string '%s'", s)
}
*r = Recipient{
Name: strings.TrimSpace(s[:i]),
Email: s[i+1 : len(s)-1],
}
return nil
} | [
"func",
"(",
"r",
"*",
"Recipient",
")",
"UnmarshalText",
"(",
"text",
"[",
"]",
"byte",
")",
"error",
"{",
"s",
":=",
"string",
"(",
"text",
")",
"\n",
"if",
"s",
"[",
"len",
"(",
"s",
")",
"-",
"1",
":",
"]",
"!=",
"\"",
"\"",
"{",
"*",
"r",
"=",
"Recipient",
"{",
"Email",
":",
"s",
"}",
"\n",
"return",
"nil",
"\n",
"}",
"\n\n",
"i",
":=",
"strings",
".",
"Index",
"(",
"s",
",",
"\"",
"\"",
")",
"\n",
"// at least 1 char followed by a space",
"if",
"i",
"<",
"2",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"s",
")",
"\n",
"}",
"\n",
"*",
"r",
"=",
"Recipient",
"{",
"Name",
":",
"strings",
".",
"TrimSpace",
"(",
"s",
"[",
":",
"i",
"]",
")",
",",
"Email",
":",
"s",
"[",
"i",
"+",
"1",
":",
"len",
"(",
"s",
")",
"-",
"1",
"]",
",",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // UnmarshalText satisfies TextUnmarshaler | [
"UnmarshalText",
"satisfies",
"TextUnmarshaler"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/recipients.go#L24-L42 | train |
mailgun/mailgun-go | bounces.go | GetBounce | func (mg *MailgunImpl) GetBounce(ctx context.Context, address string) (Bounce, error) {
r := newHTTPRequest(generateApiUrl(mg, bouncesEndpoint) + "/" + address)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var response Bounce
err := getResponseFromJSON(ctx, r, &response)
return response, err
} | go | func (mg *MailgunImpl) GetBounce(ctx context.Context, address string) (Bounce, error) {
r := newHTTPRequest(generateApiUrl(mg, bouncesEndpoint) + "/" + address)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var response Bounce
err := getResponseFromJSON(ctx, r, &response)
return response, err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"GetBounce",
"(",
"ctx",
"context",
".",
"Context",
",",
"address",
"string",
")",
"(",
"Bounce",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"bouncesEndpoint",
")",
"+",
"\"",
"\"",
"+",
"address",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n\n",
"var",
"response",
"Bounce",
"\n",
"err",
":=",
"getResponseFromJSON",
"(",
"ctx",
",",
"r",
",",
"&",
"response",
")",
"\n",
"return",
"response",
",",
"err",
"\n",
"}"
] | // GetBounce retrieves a single bounce record, if any exist, for the given recipient address. | [
"GetBounce",
"retrieves",
"a",
"single",
"bounce",
"record",
"if",
"any",
"exist",
"for",
"the",
"given",
"recipient",
"address",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/bounces.go#L152-L160 | train |
mailgun/mailgun-go | bounces.go | DeleteBounce | func (mg *MailgunImpl) DeleteBounce(ctx context.Context, address string) error {
r := newHTTPRequest(generateApiUrl(mg, bouncesEndpoint) + "/" + address)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
} | go | func (mg *MailgunImpl) DeleteBounce(ctx context.Context, address string) error {
r := newHTTPRequest(generateApiUrl(mg, bouncesEndpoint) + "/" + address)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"DeleteBounce",
"(",
"ctx",
"context",
".",
"Context",
",",
"address",
"string",
")",
"error",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"bouncesEndpoint",
")",
"+",
"\"",
"\"",
"+",
"address",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"_",
",",
"err",
":=",
"makeDeleteRequest",
"(",
"ctx",
",",
"r",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // DeleteBounce removes all bounces associted with the provided e-mail address. | [
"DeleteBounce",
"removes",
"all",
"bounces",
"associted",
"with",
"the",
"provided",
"e",
"-",
"mail",
"address",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/bounces.go#L196-L202 | train |
mailgun/mailgun-go | template.go | CreateTemplate | func (mg *MailgunImpl) CreateTemplate(ctx context.Context, template *Template) error {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
if template.Name != "" {
payload.addValue("name", template.Name)
}
if template.Description != "" {
payload.addValue("description", template.Description)
}
if template.Version.Engine != "" {
payload.addValue("engine", string(template.Version.Engine))
}
if template.Version.Template != "" {
payload.addValue("template", template.Version.Template)
}
if template.Version.Comment != "" {
payload.addValue("comment", template.Version.Comment)
}
var resp templateResp
if err := postResponseFromJSON(ctx, r, payload, &resp); err != nil {
return err
}
*template = resp.Item
return nil
} | go | func (mg *MailgunImpl) CreateTemplate(ctx context.Context, template *Template) error {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
if template.Name != "" {
payload.addValue("name", template.Name)
}
if template.Description != "" {
payload.addValue("description", template.Description)
}
if template.Version.Engine != "" {
payload.addValue("engine", string(template.Version.Engine))
}
if template.Version.Template != "" {
payload.addValue("template", template.Version.Template)
}
if template.Version.Comment != "" {
payload.addValue("comment", template.Version.Comment)
}
var resp templateResp
if err := postResponseFromJSON(ctx, r, payload, &resp); err != nil {
return err
}
*template = resp.Item
return nil
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"CreateTemplate",
"(",
"ctx",
"context",
".",
"Context",
",",
"template",
"*",
"Template",
")",
"error",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"templatesEndpoint",
")",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n\n",
"payload",
":=",
"newUrlEncodedPayload",
"(",
")",
"\n\n",
"if",
"template",
".",
"Name",
"!=",
"\"",
"\"",
"{",
"payload",
".",
"addValue",
"(",
"\"",
"\"",
",",
"template",
".",
"Name",
")",
"\n",
"}",
"\n",
"if",
"template",
".",
"Description",
"!=",
"\"",
"\"",
"{",
"payload",
".",
"addValue",
"(",
"\"",
"\"",
",",
"template",
".",
"Description",
")",
"\n",
"}",
"\n\n",
"if",
"template",
".",
"Version",
".",
"Engine",
"!=",
"\"",
"\"",
"{",
"payload",
".",
"addValue",
"(",
"\"",
"\"",
",",
"string",
"(",
"template",
".",
"Version",
".",
"Engine",
")",
")",
"\n",
"}",
"\n",
"if",
"template",
".",
"Version",
".",
"Template",
"!=",
"\"",
"\"",
"{",
"payload",
".",
"addValue",
"(",
"\"",
"\"",
",",
"template",
".",
"Version",
".",
"Template",
")",
"\n",
"}",
"\n",
"if",
"template",
".",
"Version",
".",
"Comment",
"!=",
"\"",
"\"",
"{",
"payload",
".",
"addValue",
"(",
"\"",
"\"",
",",
"template",
".",
"Version",
".",
"Comment",
")",
"\n",
"}",
"\n\n",
"var",
"resp",
"templateResp",
"\n",
"if",
"err",
":=",
"postResponseFromJSON",
"(",
"ctx",
",",
"r",
",",
"payload",
",",
"&",
"resp",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"*",
"template",
"=",
"resp",
".",
"Item",
"\n",
"return",
"nil",
"\n",
"}"
] | // Create a new template which can be used to attach template versions to | [
"Create",
"a",
"new",
"template",
"which",
"can",
"be",
"used",
"to",
"attach",
"template",
"versions",
"to"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/template.go#L36-L66 | train |
mailgun/mailgun-go | template.go | GetTemplate | func (mg *MailgunImpl) GetTemplate(ctx context.Context, name string) (Template, error) {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
r.addParameter("active", "yes")
var resp templateResp
err := getResponseFromJSON(ctx, r, &resp)
if err != nil {
return Template{}, err
}
return resp.Item, nil
} | go | func (mg *MailgunImpl) GetTemplate(ctx context.Context, name string) (Template, error) {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
r.addParameter("active", "yes")
var resp templateResp
err := getResponseFromJSON(ctx, r, &resp)
if err != nil {
return Template{}, err
}
return resp.Item, nil
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"GetTemplate",
"(",
"ctx",
"context",
".",
"Context",
",",
"name",
"string",
")",
"(",
"Template",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"templatesEndpoint",
")",
"+",
"\"",
"\"",
"+",
"name",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"r",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n\n",
"var",
"resp",
"templateResp",
"\n",
"err",
":=",
"getResponseFromJSON",
"(",
"ctx",
",",
"r",
",",
"&",
"resp",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"Template",
"{",
"}",
",",
"err",
"\n",
"}",
"\n",
"return",
"resp",
".",
"Item",
",",
"nil",
"\n",
"}"
] | // GetTemplate gets a template given the template name | [
"GetTemplate",
"gets",
"a",
"template",
"given",
"the",
"template",
"name"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/template.go#L69-L81 | train |
mailgun/mailgun-go | template.go | UpdateTemplate | func (mg *MailgunImpl) UpdateTemplate(ctx context.Context, template *Template) error {
if template.Name == "" {
return errors.New("UpdateTemplate() Template.Name cannot be empty")
}
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + template.Name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
if template.Name != "" {
p.addValue("name", template.Name)
}
if template.Description != "" {
p.addValue("description", template.Description)
}
var resp templateResp
err := putResponseFromJSON(ctx, r, p, &resp)
if err != nil {
return err
}
*template = resp.Item
return nil
} | go | func (mg *MailgunImpl) UpdateTemplate(ctx context.Context, template *Template) error {
if template.Name == "" {
return errors.New("UpdateTemplate() Template.Name cannot be empty")
}
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + template.Name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
p := newUrlEncodedPayload()
if template.Name != "" {
p.addValue("name", template.Name)
}
if template.Description != "" {
p.addValue("description", template.Description)
}
var resp templateResp
err := putResponseFromJSON(ctx, r, p, &resp)
if err != nil {
return err
}
*template = resp.Item
return nil
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"UpdateTemplate",
"(",
"ctx",
"context",
".",
"Context",
",",
"template",
"*",
"Template",
")",
"error",
"{",
"if",
"template",
".",
"Name",
"==",
"\"",
"\"",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"templatesEndpoint",
")",
"+",
"\"",
"\"",
"+",
"template",
".",
"Name",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"p",
":=",
"newUrlEncodedPayload",
"(",
")",
"\n\n",
"if",
"template",
".",
"Name",
"!=",
"\"",
"\"",
"{",
"p",
".",
"addValue",
"(",
"\"",
"\"",
",",
"template",
".",
"Name",
")",
"\n",
"}",
"\n",
"if",
"template",
".",
"Description",
"!=",
"\"",
"\"",
"{",
"p",
".",
"addValue",
"(",
"\"",
"\"",
",",
"template",
".",
"Description",
")",
"\n",
"}",
"\n\n",
"var",
"resp",
"templateResp",
"\n",
"err",
":=",
"putResponseFromJSON",
"(",
"ctx",
",",
"r",
",",
"p",
",",
"&",
"resp",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"*",
"template",
"=",
"resp",
".",
"Item",
"\n",
"return",
"nil",
"\n",
"}"
] | // Update the name and description of a template | [
"Update",
"the",
"name",
"and",
"description",
"of",
"a",
"template"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/template.go#L84-L108 | train |
mailgun/mailgun-go | template.go | DeleteTemplate | func (mg *MailgunImpl) DeleteTemplate(ctx context.Context, name string) error {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
} | go | func (mg *MailgunImpl) DeleteTemplate(ctx context.Context, name string) error {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint) + "/" + name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
_, err := makeDeleteRequest(ctx, r)
return err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"DeleteTemplate",
"(",
"ctx",
"context",
".",
"Context",
",",
"name",
"string",
")",
"error",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"templatesEndpoint",
")",
"+",
"\"",
"\"",
"+",
"name",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"_",
",",
"err",
":=",
"makeDeleteRequest",
"(",
"ctx",
",",
"r",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // Delete a template given a template name | [
"Delete",
"a",
"template",
"given",
"a",
"template",
"name"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/template.go#L111-L117 | train |
mailgun/mailgun-go | template.go | ListTemplates | func (mg *MailgunImpl) ListTemplates(opts *ListTemplateOptions) *TemplatesIterator {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
if opts != nil {
if opts.Limit != 0 {
r.addParameter("limit", strconv.Itoa(opts.Limit))
}
if opts.Active {
r.addParameter("active", "yes")
}
}
url, err := r.generateUrlWithParameters()
return &TemplatesIterator{
mg: mg,
templateListResp: templateListResp{Paging: Paging{Next: url, First: url}},
err: err,
}
} | go | func (mg *MailgunImpl) ListTemplates(opts *ListTemplateOptions) *TemplatesIterator {
r := newHTTPRequest(generateApiUrl(mg, templatesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
if opts != nil {
if opts.Limit != 0 {
r.addParameter("limit", strconv.Itoa(opts.Limit))
}
if opts.Active {
r.addParameter("active", "yes")
}
}
url, err := r.generateUrlWithParameters()
return &TemplatesIterator{
mg: mg,
templateListResp: templateListResp{Paging: Paging{Next: url, First: url}},
err: err,
}
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"ListTemplates",
"(",
"opts",
"*",
"ListTemplateOptions",
")",
"*",
"TemplatesIterator",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generateApiUrl",
"(",
"mg",
",",
"templatesEndpoint",
")",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"if",
"opts",
"!=",
"nil",
"{",
"if",
"opts",
".",
"Limit",
"!=",
"0",
"{",
"r",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"strconv",
".",
"Itoa",
"(",
"opts",
".",
"Limit",
")",
")",
"\n",
"}",
"\n",
"if",
"opts",
".",
"Active",
"{",
"r",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}",
"\n",
"url",
",",
"err",
":=",
"r",
".",
"generateUrlWithParameters",
"(",
")",
"\n",
"return",
"&",
"TemplatesIterator",
"{",
"mg",
":",
"mg",
",",
"templateListResp",
":",
"templateListResp",
"{",
"Paging",
":",
"Paging",
"{",
"Next",
":",
"url",
",",
"First",
":",
"url",
"}",
"}",
",",
"err",
":",
"err",
",",
"}",
"\n",
"}"
] | // List all available templates | [
"List",
"all",
"available",
"templates"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/template.go#L131-L149 | train |
mailgun/mailgun-go | exports.go | CreateExport | func (mg *MailgunImpl) CreateExport(ctx context.Context, url string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, exportsEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
payload.addValue("url", url)
_, err := makePostRequest(ctx, r, payload)
return err
} | go | func (mg *MailgunImpl) CreateExport(ctx context.Context, url string) error {
r := newHTTPRequest(generatePublicApiUrl(mg, exportsEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newUrlEncodedPayload()
payload.addValue("url", url)
_, err := makePostRequest(ctx, r, payload)
return err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"CreateExport",
"(",
"ctx",
"context",
".",
"Context",
",",
"url",
"string",
")",
"error",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generatePublicApiUrl",
"(",
"mg",
",",
"exportsEndpoint",
")",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n\n",
"payload",
":=",
"newUrlEncodedPayload",
"(",
")",
"\n",
"payload",
".",
"addValue",
"(",
"\"",
"\"",
",",
"url",
")",
"\n",
"_",
",",
"err",
":=",
"makePostRequest",
"(",
"ctx",
",",
"r",
",",
"payload",
")",
"\n",
"return",
"err",
"\n",
"}"
] | // Create an export based on the URL given | [
"Create",
"an",
"export",
"based",
"on",
"the",
"URL",
"given"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/exports.go#L21-L30 | train |
mailgun/mailgun-go | exports.go | ListExports | func (mg *MailgunImpl) ListExports(ctx context.Context, url string) ([]Export, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, exportsEndpoint))
r.setClient(mg.Client())
if url != "" {
r.addParameter("url", url)
}
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp ExportList
if err := getResponseFromJSON(ctx, r, &resp); err != nil {
return nil, err
}
var result []Export
for _, item := range resp.Items {
result = append(result, Export(item))
}
return result, nil
} | go | func (mg *MailgunImpl) ListExports(ctx context.Context, url string) ([]Export, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, exportsEndpoint))
r.setClient(mg.Client())
if url != "" {
r.addParameter("url", url)
}
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp ExportList
if err := getResponseFromJSON(ctx, r, &resp); err != nil {
return nil, err
}
var result []Export
for _, item := range resp.Items {
result = append(result, Export(item))
}
return result, nil
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"ListExports",
"(",
"ctx",
"context",
".",
"Context",
",",
"url",
"string",
")",
"(",
"[",
"]",
"Export",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generatePublicApiUrl",
"(",
"mg",
",",
"exportsEndpoint",
")",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"if",
"url",
"!=",
"\"",
"\"",
"{",
"r",
".",
"addParameter",
"(",
"\"",
"\"",
",",
"url",
")",
"\n",
"}",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n\n",
"var",
"resp",
"ExportList",
"\n",
"if",
"err",
":=",
"getResponseFromJSON",
"(",
"ctx",
",",
"r",
",",
"&",
"resp",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"var",
"result",
"[",
"]",
"Export",
"\n",
"for",
"_",
",",
"item",
":=",
"range",
"resp",
".",
"Items",
"{",
"result",
"=",
"append",
"(",
"result",
",",
"Export",
"(",
"item",
")",
")",
"\n",
"}",
"\n",
"return",
"result",
",",
"nil",
"\n",
"}"
] | // List all exports created within the past 24 hours | [
"List",
"all",
"exports",
"created",
"within",
"the",
"past",
"24",
"hours"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/exports.go#L33-L51 | train |
mailgun/mailgun-go | exports.go | GetExport | func (mg *MailgunImpl) GetExport(ctx context.Context, id string) (Export, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, exportsEndpoint) + "/" + id)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp Export
err := getResponseFromJSON(ctx, r, &resp)
return resp, err
} | go | func (mg *MailgunImpl) GetExport(ctx context.Context, id string) (Export, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, exportsEndpoint) + "/" + id)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
var resp Export
err := getResponseFromJSON(ctx, r, &resp)
return resp, err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"GetExport",
"(",
"ctx",
"context",
".",
"Context",
",",
"id",
"string",
")",
"(",
"Export",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generatePublicApiUrl",
"(",
"mg",
",",
"exportsEndpoint",
")",
"+",
"\"",
"\"",
"+",
"id",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"var",
"resp",
"Export",
"\n",
"err",
":=",
"getResponseFromJSON",
"(",
"ctx",
",",
"r",
",",
"&",
"resp",
")",
"\n",
"return",
"resp",
",",
"err",
"\n",
"}"
] | // GetExport gets an export by id | [
"GetExport",
"gets",
"an",
"export",
"by",
"id"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/exports.go#L54-L61 | train |
mailgun/mailgun-go | exports.go | GetExportLink | func (mg *MailgunImpl) GetExportLink(ctx context.Context, id string) (string, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, exportsEndpoint) + "/" + id + "/download_url")
c := mg.Client()
// Ensure the client doesn't attempt to retry
c.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return errors.New("redirect")
}
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
r.addHeader("User-Agent", MailgunGoUserAgent)
req, err := r.NewRequest(ctx, "GET", nil)
if err != nil {
return "", err
}
if Debug {
fmt.Println(r.curlString(req, nil))
}
resp, err := r.Client.Do(req)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusFound {
url, err := resp.Location()
if err != nil {
return "", fmt.Errorf("while parsing 302 redirect url: %s", err)
}
return url.String(), nil
}
return "", err
}
return "", fmt.Errorf("expected a 302 response, API returned a '%d' instead", resp.StatusCode)
} | go | func (mg *MailgunImpl) GetExportLink(ctx context.Context, id string) (string, error) {
r := newHTTPRequest(generatePublicApiUrl(mg, exportsEndpoint) + "/" + id + "/download_url")
c := mg.Client()
// Ensure the client doesn't attempt to retry
c.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return errors.New("redirect")
}
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
r.addHeader("User-Agent", MailgunGoUserAgent)
req, err := r.NewRequest(ctx, "GET", nil)
if err != nil {
return "", err
}
if Debug {
fmt.Println(r.curlString(req, nil))
}
resp, err := r.Client.Do(req)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusFound {
url, err := resp.Location()
if err != nil {
return "", fmt.Errorf("while parsing 302 redirect url: %s", err)
}
return url.String(), nil
}
return "", err
}
return "", fmt.Errorf("expected a 302 response, API returned a '%d' instead", resp.StatusCode)
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"GetExportLink",
"(",
"ctx",
"context",
".",
"Context",
",",
"id",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"generatePublicApiUrl",
"(",
"mg",
",",
"exportsEndpoint",
")",
"+",
"\"",
"\"",
"+",
"id",
"+",
"\"",
"\"",
")",
"\n",
"c",
":=",
"mg",
".",
"Client",
"(",
")",
"\n\n",
"// Ensure the client doesn't attempt to retry",
"c",
".",
"CheckRedirect",
"=",
"func",
"(",
"req",
"*",
"http",
".",
"Request",
",",
"via",
"[",
"]",
"*",
"http",
".",
"Request",
")",
"error",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n\n",
"r",
".",
"addHeader",
"(",
"\"",
"\"",
",",
"MailgunGoUserAgent",
")",
"\n\n",
"req",
",",
"err",
":=",
"r",
".",
"NewRequest",
"(",
"ctx",
",",
"\"",
"\"",
",",
"nil",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"if",
"Debug",
"{",
"fmt",
".",
"Println",
"(",
"r",
".",
"curlString",
"(",
"req",
",",
"nil",
")",
")",
"\n",
"}",
"\n\n",
"resp",
",",
"err",
":=",
"r",
".",
"Client",
".",
"Do",
"(",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"if",
"resp",
"!=",
"nil",
"&&",
"resp",
".",
"StatusCode",
"==",
"http",
".",
"StatusFound",
"{",
"url",
",",
"err",
":=",
"resp",
".",
"Location",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n",
"return",
"url",
".",
"String",
"(",
")",
",",
"nil",
"\n",
"}",
"\n",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"return",
"\"",
"\"",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"resp",
".",
"StatusCode",
")",
"\n",
"}"
] | // Download an export by ID. This will respond with a '302 Moved'
// with the Location header of temporary S3 URL if it is available. | [
"Download",
"an",
"export",
"by",
"ID",
".",
"This",
"will",
"respond",
"with",
"a",
"302",
"Moved",
"with",
"the",
"Location",
"header",
"of",
"temporary",
"S3",
"URL",
"if",
"it",
"is",
"available",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/exports.go#L65-L99 | train |
mailgun/mailgun-go | messages.go | AddReaderAttachment | func (m *Message) AddReaderAttachment(filename string, readCloser io.ReadCloser) {
ra := ReaderAttachment{Filename: filename, ReadCloser: readCloser}
m.readerAttachments = append(m.readerAttachments, ra)
} | go | func (m *Message) AddReaderAttachment(filename string, readCloser io.ReadCloser) {
ra := ReaderAttachment{Filename: filename, ReadCloser: readCloser}
m.readerAttachments = append(m.readerAttachments, ra)
} | [
"func",
"(",
"m",
"*",
"Message",
")",
"AddReaderAttachment",
"(",
"filename",
"string",
",",
"readCloser",
"io",
".",
"ReadCloser",
")",
"{",
"ra",
":=",
"ReaderAttachment",
"{",
"Filename",
":",
"filename",
",",
"ReadCloser",
":",
"readCloser",
"}",
"\n",
"m",
".",
"readerAttachments",
"=",
"append",
"(",
"m",
".",
"readerAttachments",
",",
"ra",
")",
"\n",
"}"
] | // AddReaderAttachment arranges to send a file along with the e-mail message.
// File contents are read from a io.ReadCloser.
// The filename parameter is the resulting filename of the attachment.
// The readCloser parameter is the io.ReadCloser which reads the actual bytes to be used
// as the contents of the attached file. | [
"AddReaderAttachment",
"arranges",
"to",
"send",
"a",
"file",
"along",
"with",
"the",
"e",
"-",
"mail",
"message",
".",
"File",
"contents",
"are",
"read",
"from",
"a",
"io",
".",
"ReadCloser",
".",
"The",
"filename",
"parameter",
"is",
"the",
"resulting",
"filename",
"of",
"the",
"attachment",
".",
"The",
"readCloser",
"parameter",
"is",
"the",
"io",
".",
"ReadCloser",
"which",
"reads",
"the",
"actual",
"bytes",
"to",
"be",
"used",
"as",
"the",
"contents",
"of",
"the",
"attached",
"file",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L208-L211 | train |
mailgun/mailgun-go | messages.go | AddAttachment | func (m *Message) AddAttachment(attachment string) {
m.attachments = append(m.attachments, attachment)
} | go | func (m *Message) AddAttachment(attachment string) {
m.attachments = append(m.attachments, attachment)
} | [
"func",
"(",
"m",
"*",
"Message",
")",
"AddAttachment",
"(",
"attachment",
"string",
")",
"{",
"m",
".",
"attachments",
"=",
"append",
"(",
"m",
".",
"attachments",
",",
"attachment",
")",
"\n",
"}"
] | // AddAttachment arranges to send a file from the filesystem along with the e-mail message.
// The attachment parameter is a filename, which must refer to a file which actually resides
// in the local filesystem. | [
"AddAttachment",
"arranges",
"to",
"send",
"a",
"file",
"from",
"the",
"filesystem",
"along",
"with",
"the",
"e",
"-",
"mail",
"message",
".",
"The",
"attachment",
"parameter",
"is",
"a",
"filename",
"which",
"must",
"refer",
"to",
"a",
"file",
"which",
"actually",
"resides",
"in",
"the",
"local",
"filesystem",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L226-L228 | train |
mailgun/mailgun-go | messages.go | AddReaderInline | func (m *Message) AddReaderInline(filename string, readCloser io.ReadCloser) {
ra := ReaderAttachment{Filename: filename, ReadCloser: readCloser}
m.readerInlines = append(m.readerInlines, ra)
} | go | func (m *Message) AddReaderInline(filename string, readCloser io.ReadCloser) {
ra := ReaderAttachment{Filename: filename, ReadCloser: readCloser}
m.readerInlines = append(m.readerInlines, ra)
} | [
"func",
"(",
"m",
"*",
"Message",
")",
"AddReaderInline",
"(",
"filename",
"string",
",",
"readCloser",
"io",
".",
"ReadCloser",
")",
"{",
"ra",
":=",
"ReaderAttachment",
"{",
"Filename",
":",
"filename",
",",
"ReadCloser",
":",
"readCloser",
"}",
"\n",
"m",
".",
"readerInlines",
"=",
"append",
"(",
"m",
".",
"readerInlines",
",",
"ra",
")",
"\n",
"}"
] | // AddReaderInline arranges to send a file along with the e-mail message.
// File contents are read from a io.ReadCloser.
// The filename parameter is the resulting filename of the attachment.
// The readCloser parameter is the io.ReadCloser which reads the actual bytes to be used
// as the contents of the attached file. | [
"AddReaderInline",
"arranges",
"to",
"send",
"a",
"file",
"along",
"with",
"the",
"e",
"-",
"mail",
"message",
".",
"File",
"contents",
"are",
"read",
"from",
"a",
"io",
".",
"ReadCloser",
".",
"The",
"filename",
"parameter",
"is",
"the",
"resulting",
"filename",
"of",
"the",
"attachment",
".",
"The",
"readCloser",
"parameter",
"is",
"the",
"io",
".",
"ReadCloser",
"which",
"reads",
"the",
"actual",
"bytes",
"to",
"be",
"used",
"as",
"the",
"contents",
"of",
"the",
"attached",
"file",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L235-L238 | train |
mailgun/mailgun-go | messages.go | AddInline | func (m *Message) AddInline(inline string) {
m.inlines = append(m.inlines, inline)
} | go | func (m *Message) AddInline(inline string) {
m.inlines = append(m.inlines, inline)
} | [
"func",
"(",
"m",
"*",
"Message",
")",
"AddInline",
"(",
"inline",
"string",
")",
"{",
"m",
".",
"inlines",
"=",
"append",
"(",
"m",
".",
"inlines",
",",
"inline",
")",
"\n",
"}"
] | // AddInline arranges to send a file along with the e-mail message, but does so
// in a way that its data remains "inline" with the rest of the message. This
// can be used to send image or font data along with an HTML-encoded message body.
// The attachment parameter is a filename, which must refer to a file which actually resides
// in the local filesystem. | [
"AddInline",
"arranges",
"to",
"send",
"a",
"file",
"along",
"with",
"the",
"e",
"-",
"mail",
"message",
"but",
"does",
"so",
"in",
"a",
"way",
"that",
"its",
"data",
"remains",
"inline",
"with",
"the",
"rest",
"of",
"the",
"message",
".",
"This",
"can",
"be",
"used",
"to",
"send",
"image",
"or",
"font",
"data",
"along",
"with",
"an",
"HTML",
"-",
"encoded",
"message",
"body",
".",
"The",
"attachment",
"parameter",
"is",
"a",
"filename",
"which",
"must",
"refer",
"to",
"a",
"file",
"which",
"actually",
"resides",
"in",
"the",
"local",
"filesystem",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L245-L247 | train |
mailgun/mailgun-go | messages.go | AddTag | func (m *Message) AddTag(tag ...string) error {
if len(m.tags) >= MaxNumberOfTags {
return fmt.Errorf("cannot add any new tags. Message tag limit (%b) reached", MaxNumberOfTags)
}
m.tags = append(m.tags, tag...)
return nil
} | go | func (m *Message) AddTag(tag ...string) error {
if len(m.tags) >= MaxNumberOfTags {
return fmt.Errorf("cannot add any new tags. Message tag limit (%b) reached", MaxNumberOfTags)
}
m.tags = append(m.tags, tag...)
return nil
} | [
"func",
"(",
"m",
"*",
"Message",
")",
"AddTag",
"(",
"tag",
"...",
"string",
")",
"error",
"{",
"if",
"len",
"(",
"m",
".",
"tags",
")",
">=",
"MaxNumberOfTags",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"MaxNumberOfTags",
")",
"\n",
"}",
"\n\n",
"m",
".",
"tags",
"=",
"append",
"(",
"m",
".",
"tags",
",",
"tag",
"...",
")",
"\n",
"return",
"nil",
"\n",
"}"
] | // AddTag attaches tags to the message. Tags are useful for metrics gathering and event tracking purposes.
// Refer to the Mailgun documentation for further details. | [
"AddTag",
"attaches",
"tags",
"to",
"the",
"message",
".",
"Tags",
"are",
"useful",
"for",
"metrics",
"gathering",
"and",
"event",
"tracking",
"purposes",
".",
"Refer",
"to",
"the",
"Mailgun",
"documentation",
"for",
"further",
"details",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L339-L346 | train |
mailgun/mailgun-go | messages.go | AddCampaign | func (m *Message) AddCampaign(campaign string) {
m.campaigns = append(m.campaigns, campaign)
} | go | func (m *Message) AddCampaign(campaign string) {
m.campaigns = append(m.campaigns, campaign)
} | [
"func",
"(",
"m",
"*",
"Message",
")",
"AddCampaign",
"(",
"campaign",
"string",
")",
"{",
"m",
".",
"campaigns",
"=",
"append",
"(",
"m",
".",
"campaigns",
",",
"campaign",
")",
"\n",
"}"
] | // This feature is deprecated for new software. | [
"This",
"feature",
"is",
"deprecated",
"for",
"new",
"software",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L361-L363 | train |
mailgun/mailgun-go | messages.go | AddHeader | func (m *Message) AddHeader(header, value string) {
if m.headers == nil {
m.headers = make(map[string]string)
}
m.headers[header] = value
} | go | func (m *Message) AddHeader(header, value string) {
if m.headers == nil {
m.headers = make(map[string]string)
}
m.headers[header] = value
} | [
"func",
"(",
"m",
"*",
"Message",
")",
"AddHeader",
"(",
"header",
",",
"value",
"string",
")",
"{",
"if",
"m",
".",
"headers",
"==",
"nil",
"{",
"m",
".",
"headers",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"string",
")",
"\n",
"}",
"\n",
"m",
".",
"headers",
"[",
"header",
"]",
"=",
"value",
"\n",
"}"
] | // AddHeader allows you to send custom MIME headers with the message. | [
"AddHeader",
"allows",
"you",
"to",
"send",
"custom",
"MIME",
"headers",
"with",
"the",
"message",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L426-L431 | train |
mailgun/mailgun-go | messages.go | AddVariable | func (m *Message) AddVariable(variable string, value interface{}) error {
if m.variables == nil {
m.variables = make(map[string]string)
}
j, err := json.Marshal(value)
if err != nil {
return err
}
encoded := string(j)
v, err := strconv.Unquote(encoded)
if err != nil {
v = encoded
}
m.variables[variable] = v
return nil
} | go | func (m *Message) AddVariable(variable string, value interface{}) error {
if m.variables == nil {
m.variables = make(map[string]string)
}
j, err := json.Marshal(value)
if err != nil {
return err
}
encoded := string(j)
v, err := strconv.Unquote(encoded)
if err != nil {
v = encoded
}
m.variables[variable] = v
return nil
} | [
"func",
"(",
"m",
"*",
"Message",
")",
"AddVariable",
"(",
"variable",
"string",
",",
"value",
"interface",
"{",
"}",
")",
"error",
"{",
"if",
"m",
".",
"variables",
"==",
"nil",
"{",
"m",
".",
"variables",
"=",
"make",
"(",
"map",
"[",
"string",
"]",
"string",
")",
"\n",
"}",
"\n\n",
"j",
",",
"err",
":=",
"json",
".",
"Marshal",
"(",
"value",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"encoded",
":=",
"string",
"(",
"j",
")",
"\n",
"v",
",",
"err",
":=",
"strconv",
".",
"Unquote",
"(",
"encoded",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"v",
"=",
"encoded",
"\n",
"}",
"\n\n",
"m",
".",
"variables",
"[",
"variable",
"]",
"=",
"v",
"\n",
"return",
"nil",
"\n",
"}"
] | // AddVariable lets you associate a set of variables with messages you send,
// which Mailgun can use to, in essence, complete form-mail.
// Refer to the Mailgun documentation for more information. | [
"AddVariable",
"lets",
"you",
"associate",
"a",
"set",
"of",
"variables",
"with",
"messages",
"you",
"send",
"which",
"Mailgun",
"can",
"use",
"to",
"in",
"essence",
"complete",
"form",
"-",
"mail",
".",
"Refer",
"to",
"the",
"Mailgun",
"documentation",
"for",
"more",
"information",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L436-L454 | train |
mailgun/mailgun-go | messages.go | isValid | func isValid(m *Message) bool {
if m == nil {
return false
}
if !m.specific.isValid() {
return false
}
if m.RecipientCount() == 0 {
return false
}
if !validateStringList(m.tags, false) {
return false
}
if !validateStringList(m.campaigns, false) || len(m.campaigns) > 3 {
return false
}
return true
} | go | func isValid(m *Message) bool {
if m == nil {
return false
}
if !m.specific.isValid() {
return false
}
if m.RecipientCount() == 0 {
return false
}
if !validateStringList(m.tags, false) {
return false
}
if !validateStringList(m.campaigns, false) || len(m.campaigns) > 3 {
return false
}
return true
} | [
"func",
"isValid",
"(",
"m",
"*",
"Message",
")",
"bool",
"{",
"if",
"m",
"==",
"nil",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"if",
"!",
"m",
".",
"specific",
".",
"isValid",
"(",
")",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"if",
"m",
".",
"RecipientCount",
"(",
")",
"==",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"if",
"!",
"validateStringList",
"(",
"m",
".",
"tags",
",",
"false",
")",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"if",
"!",
"validateStringList",
"(",
"m",
".",
"campaigns",
",",
"false",
")",
"||",
"len",
"(",
"m",
".",
"campaigns",
")",
">",
"3",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"return",
"true",
"\n",
"}"
] | // isValid returns true if, and only if,
// a Message instance is sufficiently initialized to send via the Mailgun interface. | [
"isValid",
"returns",
"true",
"if",
"and",
"only",
"if",
"a",
"Message",
"instance",
"is",
"sufficiently",
"initialized",
"to",
"send",
"via",
"the",
"Mailgun",
"interface",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L636-L658 | train |
mailgun/mailgun-go | messages.go | validateStringList | func validateStringList(list []string, requireOne bool) bool {
hasOne := false
if list == nil {
return !requireOne
} else {
for _, a := range list {
if a == "" {
return false
} else {
hasOne = hasOne || true
}
}
}
return hasOne
} | go | func validateStringList(list []string, requireOne bool) bool {
hasOne := false
if list == nil {
return !requireOne
} else {
for _, a := range list {
if a == "" {
return false
} else {
hasOne = hasOne || true
}
}
}
return hasOne
} | [
"func",
"validateStringList",
"(",
"list",
"[",
"]",
"string",
",",
"requireOne",
"bool",
")",
"bool",
"{",
"hasOne",
":=",
"false",
"\n\n",
"if",
"list",
"==",
"nil",
"{",
"return",
"!",
"requireOne",
"\n",
"}",
"else",
"{",
"for",
"_",
",",
"a",
":=",
"range",
"list",
"{",
"if",
"a",
"==",
"\"",
"\"",
"{",
"return",
"false",
"\n",
"}",
"else",
"{",
"hasOne",
"=",
"hasOne",
"||",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"hasOne",
"\n",
"}"
] | // validateStringList returns true if, and only if,
// a slice of strings exists AND all of its elements exist,
// OR if the slice doesn't exist AND it's not required to exist.
// The requireOne parameter indicates whether the list is required to exist. | [
"validateStringList",
"returns",
"true",
"if",
"and",
"only",
"if",
"a",
"slice",
"of",
"strings",
"exists",
"AND",
"all",
"of",
"its",
"elements",
"exist",
"OR",
"if",
"the",
"slice",
"doesn",
"t",
"exist",
"AND",
"it",
"s",
"not",
"required",
"to",
"exist",
".",
"The",
"requireOne",
"parameter",
"indicates",
"whether",
"the",
"list",
"is",
"required",
"to",
"exist",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L693-L709 | train |
mailgun/mailgun-go | messages.go | ReSend | func (mg *MailgunImpl) ReSend(ctx context.Context, url string, recipients ...string) (string, string, error) {
r := newHTTPRequest(url)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newFormDataPayload()
if len(recipients) == 0 {
return "", "", errors.New("must provide at least one recipient")
}
for _, to := range recipients {
payload.addValue("to", to)
}
var resp sendMessageResponse
err := postResponseFromJSON(ctx, r, payload, &resp)
if err != nil {
return "", "", err
}
return resp.Message, resp.Id, nil
} | go | func (mg *MailgunImpl) ReSend(ctx context.Context, url string, recipients ...string) (string, string, error) {
r := newHTTPRequest(url)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
payload := newFormDataPayload()
if len(recipients) == 0 {
return "", "", errors.New("must provide at least one recipient")
}
for _, to := range recipients {
payload.addValue("to", to)
}
var resp sendMessageResponse
err := postResponseFromJSON(ctx, r, payload, &resp)
if err != nil {
return "", "", err
}
return resp.Message, resp.Id, nil
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"ReSend",
"(",
"ctx",
"context",
".",
"Context",
",",
"url",
"string",
",",
"recipients",
"...",
"string",
")",
"(",
"string",
",",
"string",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"url",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n\n",
"payload",
":=",
"newFormDataPayload",
"(",
")",
"\n\n",
"if",
"len",
"(",
"recipients",
")",
"==",
"0",
"{",
"return",
"\"",
"\"",
",",
"\"",
"\"",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"to",
":=",
"range",
"recipients",
"{",
"payload",
".",
"addValue",
"(",
"\"",
"\"",
",",
"to",
")",
"\n",
"}",
"\n\n",
"var",
"resp",
"sendMessageResponse",
"\n",
"err",
":=",
"postResponseFromJSON",
"(",
"ctx",
",",
"r",
",",
"payload",
",",
"&",
"resp",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"return",
"resp",
".",
"Message",
",",
"resp",
".",
"Id",
",",
"nil",
"\n\n",
"}"
] | // Given a storage id resend the stored message to the specified recipients | [
"Given",
"a",
"storage",
"id",
"resend",
"the",
"stored",
"message",
"to",
"the",
"specified",
"recipients"
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L724-L746 | train |
mailgun/mailgun-go | messages.go | GetStoredMessageRaw | func (mg *MailgunImpl) GetStoredMessageRaw(ctx context.Context, url string) (StoredMessageRaw, error) {
r := newHTTPRequest(url)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
r.addHeader("Accept", "message/rfc2822")
var response StoredMessageRaw
err := getResponseFromJSON(ctx, r, &response)
return response, err
} | go | func (mg *MailgunImpl) GetStoredMessageRaw(ctx context.Context, url string) (StoredMessageRaw, error) {
r := newHTTPRequest(url)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
r.addHeader("Accept", "message/rfc2822")
var response StoredMessageRaw
err := getResponseFromJSON(ctx, r, &response)
return response, err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"GetStoredMessageRaw",
"(",
"ctx",
"context",
".",
"Context",
",",
"url",
"string",
")",
"(",
"StoredMessageRaw",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"url",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"r",
".",
"addHeader",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n\n",
"var",
"response",
"StoredMessageRaw",
"\n",
"err",
":=",
"getResponseFromJSON",
"(",
"ctx",
",",
"r",
",",
"&",
"response",
")",
"\n",
"return",
"response",
",",
"err",
"\n",
"}"
] | // GetStoredMessageRaw retrieves the raw MIME body of a received e-mail message.
// Compared to GetStoredMessage, it gives access to the unparsed MIME body, and
// thus delegates to the caller the required parsing. | [
"GetStoredMessageRaw",
"retrieves",
"the",
"raw",
"MIME",
"body",
"of",
"a",
"received",
"e",
"-",
"mail",
"message",
".",
"Compared",
"to",
"GetStoredMessage",
"it",
"gives",
"access",
"to",
"the",
"unparsed",
"MIME",
"body",
"and",
"thus",
"delegates",
"to",
"the",
"caller",
"the",
"required",
"parsing",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L751-L760 | train |
mailgun/mailgun-go | messages.go | GetStoredAttachment | func (mg *MailgunImpl) GetStoredAttachment(ctx context.Context, url string) ([]byte, error) {
r := newHTTPRequest(url)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
r.addHeader("Accept", "message/rfc2822")
response, err := makeGetRequest(ctx, r)
return response.Data, err
} | go | func (mg *MailgunImpl) GetStoredAttachment(ctx context.Context, url string) ([]byte, error) {
r := newHTTPRequest(url)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())
r.addHeader("Accept", "message/rfc2822")
response, err := makeGetRequest(ctx, r)
return response.Data, err
} | [
"func",
"(",
"mg",
"*",
"MailgunImpl",
")",
"GetStoredAttachment",
"(",
"ctx",
"context",
".",
"Context",
",",
"url",
"string",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"r",
":=",
"newHTTPRequest",
"(",
"url",
")",
"\n",
"r",
".",
"setClient",
"(",
"mg",
".",
"Client",
"(",
")",
")",
"\n",
"r",
".",
"setBasicAuth",
"(",
"basicAuthUser",
",",
"mg",
".",
"APIKey",
"(",
")",
")",
"\n",
"r",
".",
"addHeader",
"(",
"\"",
"\"",
",",
"\"",
"\"",
")",
"\n\n",
"response",
",",
"err",
":=",
"makeGetRequest",
"(",
"ctx",
",",
"r",
")",
"\n\n",
"return",
"response",
".",
"Data",
",",
"err",
"\n",
"}"
] | // GetStoredAttachment retrieves the raw MIME body of a received e-mail message attachment. | [
"GetStoredAttachment",
"retrieves",
"the",
"raw",
"MIME",
"body",
"of",
"a",
"received",
"e",
"-",
"mail",
"message",
"attachment",
"."
] | df421608b66e7dd4c03112d5bb01525c51f344a2 | https://github.com/mailgun/mailgun-go/blob/df421608b66e7dd4c03112d5bb01525c51f344a2/messages.go#L773-L782 | train |
xtaci/smux | stream.go | newStream | func newStream(id uint32, frameSize int, sess *Session) *Stream {
s := new(Stream)
s.id = id
s.chReadEvent = make(chan struct{}, 1)
s.frameSize = frameSize
s.sess = sess
s.die = make(chan struct{})
return s
} | go | func newStream(id uint32, frameSize int, sess *Session) *Stream {
s := new(Stream)
s.id = id
s.chReadEvent = make(chan struct{}, 1)
s.frameSize = frameSize
s.sess = sess
s.die = make(chan struct{})
return s
} | [
"func",
"newStream",
"(",
"id",
"uint32",
",",
"frameSize",
"int",
",",
"sess",
"*",
"Session",
")",
"*",
"Stream",
"{",
"s",
":=",
"new",
"(",
"Stream",
")",
"\n",
"s",
".",
"id",
"=",
"id",
"\n",
"s",
".",
"chReadEvent",
"=",
"make",
"(",
"chan",
"struct",
"{",
"}",
",",
"1",
")",
"\n",
"s",
".",
"frameSize",
"=",
"frameSize",
"\n",
"s",
".",
"sess",
"=",
"sess",
"\n",
"s",
".",
"die",
"=",
"make",
"(",
"chan",
"struct",
"{",
"}",
")",
"\n",
"return",
"s",
"\n",
"}"
] | // newStream initiates a Stream struct | [
"newStream",
"initiates",
"a",
"Stream",
"struct"
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/stream.go#L29-L37 | train |
xtaci/smux | stream.go | Write | func (s *Stream) Write(b []byte) (n int, err error) {
var deadline <-chan time.Time
if d, ok := s.writeDeadline.Load().(time.Time); ok && !d.IsZero() {
timer := time.NewTimer(time.Until(d))
defer timer.Stop()
deadline = timer.C
}
select {
case <-s.die:
return 0, errors.New(errBrokenPipe)
default:
}
// frame split and transmit
sent := 0
frame := newFrame(cmdPSH, s.id)
bts := b
for len(bts) > 0 {
sz := len(bts)
if sz > s.frameSize {
sz = s.frameSize
}
frame.data = bts[:sz]
bts = bts[sz:]
n, err := s.sess.writeFrameInternal(frame, deadline)
sent += n
if err != nil {
return sent, err
}
}
return sent, nil
} | go | func (s *Stream) Write(b []byte) (n int, err error) {
var deadline <-chan time.Time
if d, ok := s.writeDeadline.Load().(time.Time); ok && !d.IsZero() {
timer := time.NewTimer(time.Until(d))
defer timer.Stop()
deadline = timer.C
}
select {
case <-s.die:
return 0, errors.New(errBrokenPipe)
default:
}
// frame split and transmit
sent := 0
frame := newFrame(cmdPSH, s.id)
bts := b
for len(bts) > 0 {
sz := len(bts)
if sz > s.frameSize {
sz = s.frameSize
}
frame.data = bts[:sz]
bts = bts[sz:]
n, err := s.sess.writeFrameInternal(frame, deadline)
sent += n
if err != nil {
return sent, err
}
}
return sent, nil
} | [
"func",
"(",
"s",
"*",
"Stream",
")",
"Write",
"(",
"b",
"[",
"]",
"byte",
")",
"(",
"n",
"int",
",",
"err",
"error",
")",
"{",
"var",
"deadline",
"<-",
"chan",
"time",
".",
"Time",
"\n",
"if",
"d",
",",
"ok",
":=",
"s",
".",
"writeDeadline",
".",
"Load",
"(",
")",
".",
"(",
"time",
".",
"Time",
")",
";",
"ok",
"&&",
"!",
"d",
".",
"IsZero",
"(",
")",
"{",
"timer",
":=",
"time",
".",
"NewTimer",
"(",
"time",
".",
"Until",
"(",
"d",
")",
")",
"\n",
"defer",
"timer",
".",
"Stop",
"(",
")",
"\n",
"deadline",
"=",
"timer",
".",
"C",
"\n",
"}",
"\n\n",
"select",
"{",
"case",
"<-",
"s",
".",
"die",
":",
"return",
"0",
",",
"errors",
".",
"New",
"(",
"errBrokenPipe",
")",
"\n",
"default",
":",
"}",
"\n\n",
"// frame split and transmit",
"sent",
":=",
"0",
"\n",
"frame",
":=",
"newFrame",
"(",
"cmdPSH",
",",
"s",
".",
"id",
")",
"\n",
"bts",
":=",
"b",
"\n",
"for",
"len",
"(",
"bts",
")",
">",
"0",
"{",
"sz",
":=",
"len",
"(",
"bts",
")",
"\n",
"if",
"sz",
">",
"s",
".",
"frameSize",
"{",
"sz",
"=",
"s",
".",
"frameSize",
"\n",
"}",
"\n",
"frame",
".",
"data",
"=",
"bts",
"[",
":",
"sz",
"]",
"\n",
"bts",
"=",
"bts",
"[",
"sz",
":",
"]",
"\n",
"n",
",",
"err",
":=",
"s",
".",
"sess",
".",
"writeFrameInternal",
"(",
"frame",
",",
"deadline",
")",
"\n",
"sent",
"+=",
"n",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"sent",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"sent",
",",
"nil",
"\n",
"}"
] | // Write implements net.Conn | [
"Write",
"implements",
"net",
".",
"Conn"
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/stream.go#L93-L126 | train |
xtaci/smux | stream.go | Close | func (s *Stream) Close() error {
s.dieLock.Lock()
select {
case <-s.die:
s.dieLock.Unlock()
return errors.New(errBrokenPipe)
default:
close(s.die)
s.dieLock.Unlock()
s.sess.streamClosed(s.id)
_, err := s.sess.writeFrame(newFrame(cmdFIN, s.id))
return err
}
} | go | func (s *Stream) Close() error {
s.dieLock.Lock()
select {
case <-s.die:
s.dieLock.Unlock()
return errors.New(errBrokenPipe)
default:
close(s.die)
s.dieLock.Unlock()
s.sess.streamClosed(s.id)
_, err := s.sess.writeFrame(newFrame(cmdFIN, s.id))
return err
}
} | [
"func",
"(",
"s",
"*",
"Stream",
")",
"Close",
"(",
")",
"error",
"{",
"s",
".",
"dieLock",
".",
"Lock",
"(",
")",
"\n\n",
"select",
"{",
"case",
"<-",
"s",
".",
"die",
":",
"s",
".",
"dieLock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"errors",
".",
"New",
"(",
"errBrokenPipe",
")",
"\n",
"default",
":",
"close",
"(",
"s",
".",
"die",
")",
"\n",
"s",
".",
"dieLock",
".",
"Unlock",
"(",
")",
"\n",
"s",
".",
"sess",
".",
"streamClosed",
"(",
"s",
".",
"id",
")",
"\n",
"_",
",",
"err",
":=",
"s",
".",
"sess",
".",
"writeFrame",
"(",
"newFrame",
"(",
"cmdFIN",
",",
"s",
".",
"id",
")",
")",
"\n",
"return",
"err",
"\n",
"}",
"\n",
"}"
] | // Close implements net.Conn | [
"Close",
"implements",
"net",
".",
"Conn"
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/stream.go#L129-L143 | train |
xtaci/smux | stream.go | sessionClose | func (s *Stream) sessionClose() {
s.dieLock.Lock()
defer s.dieLock.Unlock()
select {
case <-s.die:
default:
close(s.die)
}
} | go | func (s *Stream) sessionClose() {
s.dieLock.Lock()
defer s.dieLock.Unlock()
select {
case <-s.die:
default:
close(s.die)
}
} | [
"func",
"(",
"s",
"*",
"Stream",
")",
"sessionClose",
"(",
")",
"{",
"s",
".",
"dieLock",
".",
"Lock",
"(",
")",
"\n",
"defer",
"s",
".",
"dieLock",
".",
"Unlock",
"(",
")",
"\n\n",
"select",
"{",
"case",
"<-",
"s",
".",
"die",
":",
"default",
":",
"close",
"(",
"s",
".",
"die",
")",
"\n",
"}",
"\n",
"}"
] | // session closes the stream | [
"session",
"closes",
"the",
"stream"
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/stream.go#L181-L190 | train |
xtaci/smux | stream.go | RemoteAddr | func (s *Stream) RemoteAddr() net.Addr {
if ts, ok := s.sess.conn.(interface {
RemoteAddr() net.Addr
}); ok {
return ts.RemoteAddr()
}
return nil
} | go | func (s *Stream) RemoteAddr() net.Addr {
if ts, ok := s.sess.conn.(interface {
RemoteAddr() net.Addr
}); ok {
return ts.RemoteAddr()
}
return nil
} | [
"func",
"(",
"s",
"*",
"Stream",
")",
"RemoteAddr",
"(",
")",
"net",
".",
"Addr",
"{",
"if",
"ts",
",",
"ok",
":=",
"s",
".",
"sess",
".",
"conn",
".",
"(",
"interface",
"{",
"RemoteAddr",
"(",
")",
"net",
".",
"Addr",
"\n",
"}",
")",
";",
"ok",
"{",
"return",
"ts",
".",
"RemoteAddr",
"(",
")",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // RemoteAddr satisfies net.Conn interface | [
"RemoteAddr",
"satisfies",
"net",
".",
"Conn",
"interface"
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/stream.go#L203-L210 | train |
xtaci/smux | stream.go | pushBytes | func (s *Stream) pushBytes(buf []byte) (written int, err error) {
s.bufferLock.Lock()
s.buffers = append(s.buffers, buf)
s.bufferLock.Unlock()
return
} | go | func (s *Stream) pushBytes(buf []byte) (written int, err error) {
s.bufferLock.Lock()
s.buffers = append(s.buffers, buf)
s.bufferLock.Unlock()
return
} | [
"func",
"(",
"s",
"*",
"Stream",
")",
"pushBytes",
"(",
"buf",
"[",
"]",
"byte",
")",
"(",
"written",
"int",
",",
"err",
"error",
")",
"{",
"s",
".",
"bufferLock",
".",
"Lock",
"(",
")",
"\n",
"s",
".",
"buffers",
"=",
"append",
"(",
"s",
".",
"buffers",
",",
"buf",
")",
"\n",
"s",
".",
"bufferLock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"\n",
"}"
] | // pushBytes append buf to buffers | [
"pushBytes",
"append",
"buf",
"to",
"buffers"
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/stream.go#L213-L218 | train |
xtaci/smux | session.go | Close | func (s *Session) Close() (err error) {
s.dieLock.Lock()
select {
case <-s.die:
s.dieLock.Unlock()
return errors.New(errBrokenPipe)
default:
close(s.die)
s.dieLock.Unlock()
err = s.conn.Close()
s.streamLock.Lock()
for k := range s.streams {
s.streams[k].sessionClose()
}
s.streamLock.Unlock()
return
}
} | go | func (s *Session) Close() (err error) {
s.dieLock.Lock()
select {
case <-s.die:
s.dieLock.Unlock()
return errors.New(errBrokenPipe)
default:
close(s.die)
s.dieLock.Unlock()
err = s.conn.Close()
s.streamLock.Lock()
for k := range s.streams {
s.streams[k].sessionClose()
}
s.streamLock.Unlock()
return
}
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"Close",
"(",
")",
"(",
"err",
"error",
")",
"{",
"s",
".",
"dieLock",
".",
"Lock",
"(",
")",
"\n\n",
"select",
"{",
"case",
"<-",
"s",
".",
"die",
":",
"s",
".",
"dieLock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"errors",
".",
"New",
"(",
"errBrokenPipe",
")",
"\n",
"default",
":",
"close",
"(",
"s",
".",
"die",
")",
"\n",
"s",
".",
"dieLock",
".",
"Unlock",
"(",
")",
"\n",
"err",
"=",
"s",
".",
"conn",
".",
"Close",
"(",
")",
"\n",
"s",
".",
"streamLock",
".",
"Lock",
"(",
")",
"\n",
"for",
"k",
":=",
"range",
"s",
".",
"streams",
"{",
"s",
".",
"streams",
"[",
"k",
"]",
".",
"sessionClose",
"(",
")",
"\n",
"}",
"\n",
"s",
".",
"streamLock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}"
] | // Close is used to close the session and all streams. | [
"Close",
"is",
"used",
"to",
"close",
"the",
"session",
"and",
"all",
"streams",
"."
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/session.go#L145-L163 | train |
xtaci/smux | session.go | streamClosed | func (s *Session) streamClosed(sid uint32) {
s.streamLock.Lock()
if n := s.streams[sid].recycleTokens(); n > 0 { // return remaining tokens to the bucket
if atomic.AddInt32(&s.bucket, int32(n)) > 0 {
s.notifyBucket()
}
}
delete(s.streams, sid)
s.streamLock.Unlock()
} | go | func (s *Session) streamClosed(sid uint32) {
s.streamLock.Lock()
if n := s.streams[sid].recycleTokens(); n > 0 { // return remaining tokens to the bucket
if atomic.AddInt32(&s.bucket, int32(n)) > 0 {
s.notifyBucket()
}
}
delete(s.streams, sid)
s.streamLock.Unlock()
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"streamClosed",
"(",
"sid",
"uint32",
")",
"{",
"s",
".",
"streamLock",
".",
"Lock",
"(",
")",
"\n",
"if",
"n",
":=",
"s",
".",
"streams",
"[",
"sid",
"]",
".",
"recycleTokens",
"(",
")",
";",
"n",
">",
"0",
"{",
"// return remaining tokens to the bucket",
"if",
"atomic",
".",
"AddInt32",
"(",
"&",
"s",
".",
"bucket",
",",
"int32",
"(",
"n",
")",
")",
">",
"0",
"{",
"s",
".",
"notifyBucket",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n",
"delete",
"(",
"s",
".",
"streams",
",",
"sid",
")",
"\n",
"s",
".",
"streamLock",
".",
"Unlock",
"(",
")",
"\n",
"}"
] | // notify the session that a stream has closed | [
"notify",
"the",
"session",
"that",
"a",
"stream",
"has",
"closed"
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/session.go#L201-L210 | train |
xtaci/smux | session.go | returnTokens | func (s *Session) returnTokens(n int) {
if atomic.AddInt32(&s.bucket, int32(n)) > 0 {
s.notifyBucket()
}
} | go | func (s *Session) returnTokens(n int) {
if atomic.AddInt32(&s.bucket, int32(n)) > 0 {
s.notifyBucket()
}
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"returnTokens",
"(",
"n",
"int",
")",
"{",
"if",
"atomic",
".",
"AddInt32",
"(",
"&",
"s",
".",
"bucket",
",",
"int32",
"(",
"n",
")",
")",
">",
"0",
"{",
"s",
".",
"notifyBucket",
"(",
")",
"\n",
"}",
"\n",
"}"
] | // returnTokens is called by stream to return token after read | [
"returnTokens",
"is",
"called",
"by",
"stream",
"to",
"return",
"token",
"after",
"read"
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/session.go#L213-L217 | train |
xtaci/smux | session.go | recvLoop | func (s *Session) recvLoop() {
var hdr rawHeader
for {
for atomic.LoadInt32(&s.bucket) <= 0 && !s.IsClosed() {
select {
case <-s.bucketNotify:
case <-s.die:
return
}
}
// read header first
if _, err := io.ReadFull(s.conn, hdr[:]); err == nil {
atomic.StoreInt32(&s.dataReady, 1)
if hdr.Version() != version {
s.Close()
return
}
sid := hdr.StreamID()
switch hdr.Cmd() {
case cmdNOP:
case cmdSYN:
s.streamLock.Lock()
if _, ok := s.streams[sid]; !ok {
stream := newStream(sid, s.config.MaxFrameSize, s)
s.streams[sid] = stream
select {
case s.chAccepts <- stream:
case <-s.die:
}
}
s.streamLock.Unlock()
case cmdFIN:
s.streamLock.Lock()
if stream, ok := s.streams[sid]; ok {
stream.markRST()
stream.notifyReadEvent()
}
s.streamLock.Unlock()
case cmdPSH:
if hdr.Length() > 0 {
newbuf := make([]byte, hdr.Length())
if written, err := io.ReadFull(s.conn, newbuf); err == nil {
s.streamLock.Lock()
if stream, ok := s.streams[sid]; ok {
stream.pushBytes(newbuf)
atomic.AddInt32(&s.bucket, -int32(written))
stream.notifyReadEvent()
}
s.streamLock.Unlock()
} else {
s.Close()
return
}
}
default:
s.Close()
return
}
} else {
s.Close()
return
}
}
} | go | func (s *Session) recvLoop() {
var hdr rawHeader
for {
for atomic.LoadInt32(&s.bucket) <= 0 && !s.IsClosed() {
select {
case <-s.bucketNotify:
case <-s.die:
return
}
}
// read header first
if _, err := io.ReadFull(s.conn, hdr[:]); err == nil {
atomic.StoreInt32(&s.dataReady, 1)
if hdr.Version() != version {
s.Close()
return
}
sid := hdr.StreamID()
switch hdr.Cmd() {
case cmdNOP:
case cmdSYN:
s.streamLock.Lock()
if _, ok := s.streams[sid]; !ok {
stream := newStream(sid, s.config.MaxFrameSize, s)
s.streams[sid] = stream
select {
case s.chAccepts <- stream:
case <-s.die:
}
}
s.streamLock.Unlock()
case cmdFIN:
s.streamLock.Lock()
if stream, ok := s.streams[sid]; ok {
stream.markRST()
stream.notifyReadEvent()
}
s.streamLock.Unlock()
case cmdPSH:
if hdr.Length() > 0 {
newbuf := make([]byte, hdr.Length())
if written, err := io.ReadFull(s.conn, newbuf); err == nil {
s.streamLock.Lock()
if stream, ok := s.streams[sid]; ok {
stream.pushBytes(newbuf)
atomic.AddInt32(&s.bucket, -int32(written))
stream.notifyReadEvent()
}
s.streamLock.Unlock()
} else {
s.Close()
return
}
}
default:
s.Close()
return
}
} else {
s.Close()
return
}
}
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"recvLoop",
"(",
")",
"{",
"var",
"hdr",
"rawHeader",
"\n\n",
"for",
"{",
"for",
"atomic",
".",
"LoadInt32",
"(",
"&",
"s",
".",
"bucket",
")",
"<=",
"0",
"&&",
"!",
"s",
".",
"IsClosed",
"(",
")",
"{",
"select",
"{",
"case",
"<-",
"s",
".",
"bucketNotify",
":",
"case",
"<-",
"s",
".",
"die",
":",
"return",
"\n",
"}",
"\n",
"}",
"\n\n",
"// read header first",
"if",
"_",
",",
"err",
":=",
"io",
".",
"ReadFull",
"(",
"s",
".",
"conn",
",",
"hdr",
"[",
":",
"]",
")",
";",
"err",
"==",
"nil",
"{",
"atomic",
".",
"StoreInt32",
"(",
"&",
"s",
".",
"dataReady",
",",
"1",
")",
"\n",
"if",
"hdr",
".",
"Version",
"(",
")",
"!=",
"version",
"{",
"s",
".",
"Close",
"(",
")",
"\n",
"return",
"\n",
"}",
"\n",
"sid",
":=",
"hdr",
".",
"StreamID",
"(",
")",
"\n",
"switch",
"hdr",
".",
"Cmd",
"(",
")",
"{",
"case",
"cmdNOP",
":",
"case",
"cmdSYN",
":",
"s",
".",
"streamLock",
".",
"Lock",
"(",
")",
"\n",
"if",
"_",
",",
"ok",
":=",
"s",
".",
"streams",
"[",
"sid",
"]",
";",
"!",
"ok",
"{",
"stream",
":=",
"newStream",
"(",
"sid",
",",
"s",
".",
"config",
".",
"MaxFrameSize",
",",
"s",
")",
"\n",
"s",
".",
"streams",
"[",
"sid",
"]",
"=",
"stream",
"\n",
"select",
"{",
"case",
"s",
".",
"chAccepts",
"<-",
"stream",
":",
"case",
"<-",
"s",
".",
"die",
":",
"}",
"\n",
"}",
"\n",
"s",
".",
"streamLock",
".",
"Unlock",
"(",
")",
"\n",
"case",
"cmdFIN",
":",
"s",
".",
"streamLock",
".",
"Lock",
"(",
")",
"\n",
"if",
"stream",
",",
"ok",
":=",
"s",
".",
"streams",
"[",
"sid",
"]",
";",
"ok",
"{",
"stream",
".",
"markRST",
"(",
")",
"\n",
"stream",
".",
"notifyReadEvent",
"(",
")",
"\n",
"}",
"\n",
"s",
".",
"streamLock",
".",
"Unlock",
"(",
")",
"\n",
"case",
"cmdPSH",
":",
"if",
"hdr",
".",
"Length",
"(",
")",
">",
"0",
"{",
"newbuf",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"hdr",
".",
"Length",
"(",
")",
")",
"\n",
"if",
"written",
",",
"err",
":=",
"io",
".",
"ReadFull",
"(",
"s",
".",
"conn",
",",
"newbuf",
")",
";",
"err",
"==",
"nil",
"{",
"s",
".",
"streamLock",
".",
"Lock",
"(",
")",
"\n",
"if",
"stream",
",",
"ok",
":=",
"s",
".",
"streams",
"[",
"sid",
"]",
";",
"ok",
"{",
"stream",
".",
"pushBytes",
"(",
"newbuf",
")",
"\n",
"atomic",
".",
"AddInt32",
"(",
"&",
"s",
".",
"bucket",
",",
"-",
"int32",
"(",
"written",
")",
")",
"\n",
"stream",
".",
"notifyReadEvent",
"(",
")",
"\n",
"}",
"\n",
"s",
".",
"streamLock",
".",
"Unlock",
"(",
")",
"\n",
"}",
"else",
"{",
"s",
".",
"Close",
"(",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"default",
":",
"s",
".",
"Close",
"(",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}",
"else",
"{",
"s",
".",
"Close",
"(",
")",
"\n",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // recvLoop keeps on reading from underlying connection if tokens are available | [
"recvLoop",
"keeps",
"on",
"reading",
"from",
"underlying",
"connection",
"if",
"tokens",
"are",
"available"
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/session.go#L220-L285 | train |
xtaci/smux | session.go | writeFrame | func (s *Session) writeFrame(f Frame) (n int, err error) {
return s.writeFrameInternal(f, nil)
} | go | func (s *Session) writeFrame(f Frame) (n int, err error) {
return s.writeFrameInternal(f, nil)
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"writeFrame",
"(",
"f",
"Frame",
")",
"(",
"n",
"int",
",",
"err",
"error",
")",
"{",
"return",
"s",
".",
"writeFrameInternal",
"(",
"f",
",",
"nil",
")",
"\n",
"}"
] | // writeFrame writes the frame to the underlying connection
// and returns the number of bytes written if successful | [
"writeFrame",
"writes",
"the",
"frame",
"to",
"the",
"underlying",
"connection",
"and",
"returns",
"the",
"number",
"of",
"bytes",
"written",
"if",
"successful"
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/session.go#L359-L361 | train |
xtaci/smux | session.go | writeFrameInternal | func (s *Session) writeFrameInternal(f Frame, deadline <-chan time.Time) (int, error) {
req := writeRequest{
frame: f,
result: make(chan writeResult, 1),
}
select {
case <-s.die:
return 0, errors.New(errBrokenPipe)
case s.writes <- req:
case <-deadline:
return 0, errTimeout
}
select {
case result := <-req.result:
return result.n, result.err
case <-deadline:
return 0, errTimeout
case <-s.die:
return 0, errors.New(errBrokenPipe)
}
} | go | func (s *Session) writeFrameInternal(f Frame, deadline <-chan time.Time) (int, error) {
req := writeRequest{
frame: f,
result: make(chan writeResult, 1),
}
select {
case <-s.die:
return 0, errors.New(errBrokenPipe)
case s.writes <- req:
case <-deadline:
return 0, errTimeout
}
select {
case result := <-req.result:
return result.n, result.err
case <-deadline:
return 0, errTimeout
case <-s.die:
return 0, errors.New(errBrokenPipe)
}
} | [
"func",
"(",
"s",
"*",
"Session",
")",
"writeFrameInternal",
"(",
"f",
"Frame",
",",
"deadline",
"<-",
"chan",
"time",
".",
"Time",
")",
"(",
"int",
",",
"error",
")",
"{",
"req",
":=",
"writeRequest",
"{",
"frame",
":",
"f",
",",
"result",
":",
"make",
"(",
"chan",
"writeResult",
",",
"1",
")",
",",
"}",
"\n",
"select",
"{",
"case",
"<-",
"s",
".",
"die",
":",
"return",
"0",
",",
"errors",
".",
"New",
"(",
"errBrokenPipe",
")",
"\n",
"case",
"s",
".",
"writes",
"<-",
"req",
":",
"case",
"<-",
"deadline",
":",
"return",
"0",
",",
"errTimeout",
"\n",
"}",
"\n\n",
"select",
"{",
"case",
"result",
":=",
"<-",
"req",
".",
"result",
":",
"return",
"result",
".",
"n",
",",
"result",
".",
"err",
"\n",
"case",
"<-",
"deadline",
":",
"return",
"0",
",",
"errTimeout",
"\n",
"case",
"<-",
"s",
".",
"die",
":",
"return",
"0",
",",
"errors",
".",
"New",
"(",
"errBrokenPipe",
")",
"\n",
"}",
"\n",
"}"
] | // internal writeFrame version to support deadline used in keepalive | [
"internal",
"writeFrame",
"version",
"to",
"support",
"deadline",
"used",
"in",
"keepalive"
] | 401b0da0596a2d1eb2d94aae5fa16973d6f50962 | https://github.com/xtaci/smux/blob/401b0da0596a2d1eb2d94aae5fa16973d6f50962/session.go#L364-L385 | train |
dop251/goja | date_parser.go | skip | func skip(value, prefix string) (string, error) {
for len(prefix) > 0 {
if prefix[0] == ' ' {
if len(value) > 0 && value[0] != ' ' {
return value, errBad
}
prefix = cutspace(prefix)
value = cutspace(value)
continue
}
if len(value) == 0 || value[0] != prefix[0] {
return value, errBad
}
prefix = prefix[1:]
value = value[1:]
}
return value, nil
} | go | func skip(value, prefix string) (string, error) {
for len(prefix) > 0 {
if prefix[0] == ' ' {
if len(value) > 0 && value[0] != ' ' {
return value, errBad
}
prefix = cutspace(prefix)
value = cutspace(value)
continue
}
if len(value) == 0 || value[0] != prefix[0] {
return value, errBad
}
prefix = prefix[1:]
value = value[1:]
}
return value, nil
} | [
"func",
"skip",
"(",
"value",
",",
"prefix",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"for",
"len",
"(",
"prefix",
")",
">",
"0",
"{",
"if",
"prefix",
"[",
"0",
"]",
"==",
"' '",
"{",
"if",
"len",
"(",
"value",
")",
">",
"0",
"&&",
"value",
"[",
"0",
"]",
"!=",
"' '",
"{",
"return",
"value",
",",
"errBad",
"\n",
"}",
"\n",
"prefix",
"=",
"cutspace",
"(",
"prefix",
")",
"\n",
"value",
"=",
"cutspace",
"(",
"value",
")",
"\n",
"continue",
"\n",
"}",
"\n",
"if",
"len",
"(",
"value",
")",
"==",
"0",
"||",
"value",
"[",
"0",
"]",
"!=",
"prefix",
"[",
"0",
"]",
"{",
"return",
"value",
",",
"errBad",
"\n",
"}",
"\n",
"prefix",
"=",
"prefix",
"[",
"1",
":",
"]",
"\n",
"value",
"=",
"value",
"[",
"1",
":",
"]",
"\n",
"}",
"\n",
"return",
"value",
",",
"nil",
"\n",
"}"
] | // skip removes the given prefix from value,
// treating runs of space characters as equivalent. | [
"skip",
"removes",
"the",
"given",
"prefix",
"from",
"value",
"treating",
"runs",
"of",
"space",
"characters",
"as",
"equivalent",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/date_parser.go#L649-L666 | train |
dop251/goja | date_parser.go | atoi | func atoi(s string) (x int, err error) {
q, rem, err := signedLeadingInt(s)
x = int(q)
if err != nil || rem != "" {
return 0, atoiError
}
return x, nil
} | go | func atoi(s string) (x int, err error) {
q, rem, err := signedLeadingInt(s)
x = int(q)
if err != nil || rem != "" {
return 0, atoiError
}
return x, nil
} | [
"func",
"atoi",
"(",
"s",
"string",
")",
"(",
"x",
"int",
",",
"err",
"error",
")",
"{",
"q",
",",
"rem",
",",
"err",
":=",
"signedLeadingInt",
"(",
"s",
")",
"\n",
"x",
"=",
"int",
"(",
"q",
")",
"\n",
"if",
"err",
"!=",
"nil",
"||",
"rem",
"!=",
"\"",
"\"",
"{",
"return",
"0",
",",
"atoiError",
"\n",
"}",
"\n",
"return",
"x",
",",
"nil",
"\n",
"}"
] | // Duplicates functionality in strconv, but avoids dependency. | [
"Duplicates",
"functionality",
"in",
"strconv",
"but",
"avoids",
"dependency",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/date_parser.go#L672-L679 | train |
dop251/goja | date_parser.go | match | func match(s1, s2 string) bool {
for i := 0; i < len(s1); i++ {
c1 := s1[i]
c2 := s2[i]
if c1 != c2 {
// Switch to lower-case; 'a'-'A' is known to be a single bit.
c1 |= 'a' - 'A'
c2 |= 'a' - 'A'
if c1 != c2 || c1 < 'a' || c1 > 'z' {
return false
}
}
}
return true
} | go | func match(s1, s2 string) bool {
for i := 0; i < len(s1); i++ {
c1 := s1[i]
c2 := s2[i]
if c1 != c2 {
// Switch to lower-case; 'a'-'A' is known to be a single bit.
c1 |= 'a' - 'A'
c2 |= 'a' - 'A'
if c1 != c2 || c1 < 'a' || c1 > 'z' {
return false
}
}
}
return true
} | [
"func",
"match",
"(",
"s1",
",",
"s2",
"string",
")",
"bool",
"{",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"s1",
")",
";",
"i",
"++",
"{",
"c1",
":=",
"s1",
"[",
"i",
"]",
"\n",
"c2",
":=",
"s2",
"[",
"i",
"]",
"\n",
"if",
"c1",
"!=",
"c2",
"{",
"// Switch to lower-case; 'a'-'A' is known to be a single bit.",
"c1",
"|=",
"'a'",
"-",
"'A'",
"\n",
"c2",
"|=",
"'a'",
"-",
"'A'",
"\n",
"if",
"c1",
"!=",
"c2",
"||",
"c1",
"<",
"'a'",
"||",
"c1",
">",
"'z'",
"{",
"return",
"false",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"true",
"\n",
"}"
] | // match reports whether s1 and s2 match ignoring case.
// It is assumed s1 and s2 are the same length. | [
"match",
"reports",
"whether",
"s1",
"and",
"s2",
"match",
"ignoring",
"case",
".",
"It",
"is",
"assumed",
"s1",
"and",
"s2",
"are",
"the",
"same",
"length",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/date_parser.go#L683-L697 | train |
dop251/goja | date_parser.go | parseTimeZone | func parseTimeZone(value string) (length int, ok bool) {
if len(value) < 3 {
return 0, false
}
// Special case 1: ChST and MeST are the only zones with a lower-case letter.
if len(value) >= 4 && (value[:4] == "ChST" || value[:4] == "MeST") {
return 4, true
}
// Special case 2: GMT may have an hour offset; treat it specially.
if value[:3] == "GMT" {
length = parseGMT(value)
return length, true
}
// Special Case 3: Some time zones are not named, but have +/-00 format
if value[0] == '+' || value[0] == '-' {
length = parseSignedOffset(value)
return length, true
}
// How many upper-case letters are there? Need at least three, at most five.
var nUpper int
for nUpper = 0; nUpper < 6; nUpper++ {
if nUpper >= len(value) {
break
}
if c := value[nUpper]; c < 'A' || 'Z' < c {
break
}
}
switch nUpper {
case 0, 1, 2, 6:
return 0, false
case 5: // Must end in T to match.
if value[4] == 'T' {
return 5, true
}
case 4:
// Must end in T, except one special case.
if value[3] == 'T' || value[:4] == "WITA" {
return 4, true
}
case 3:
return 3, true
}
return 0, false
} | go | func parseTimeZone(value string) (length int, ok bool) {
if len(value) < 3 {
return 0, false
}
// Special case 1: ChST and MeST are the only zones with a lower-case letter.
if len(value) >= 4 && (value[:4] == "ChST" || value[:4] == "MeST") {
return 4, true
}
// Special case 2: GMT may have an hour offset; treat it specially.
if value[:3] == "GMT" {
length = parseGMT(value)
return length, true
}
// Special Case 3: Some time zones are not named, but have +/-00 format
if value[0] == '+' || value[0] == '-' {
length = parseSignedOffset(value)
return length, true
}
// How many upper-case letters are there? Need at least three, at most five.
var nUpper int
for nUpper = 0; nUpper < 6; nUpper++ {
if nUpper >= len(value) {
break
}
if c := value[nUpper]; c < 'A' || 'Z' < c {
break
}
}
switch nUpper {
case 0, 1, 2, 6:
return 0, false
case 5: // Must end in T to match.
if value[4] == 'T' {
return 5, true
}
case 4:
// Must end in T, except one special case.
if value[3] == 'T' || value[:4] == "WITA" {
return 4, true
}
case 3:
return 3, true
}
return 0, false
} | [
"func",
"parseTimeZone",
"(",
"value",
"string",
")",
"(",
"length",
"int",
",",
"ok",
"bool",
")",
"{",
"if",
"len",
"(",
"value",
")",
"<",
"3",
"{",
"return",
"0",
",",
"false",
"\n",
"}",
"\n",
"// Special case 1: ChST and MeST are the only zones with a lower-case letter.",
"if",
"len",
"(",
"value",
")",
">=",
"4",
"&&",
"(",
"value",
"[",
":",
"4",
"]",
"==",
"\"",
"\"",
"||",
"value",
"[",
":",
"4",
"]",
"==",
"\"",
"\"",
")",
"{",
"return",
"4",
",",
"true",
"\n",
"}",
"\n",
"// Special case 2: GMT may have an hour offset; treat it specially.",
"if",
"value",
"[",
":",
"3",
"]",
"==",
"\"",
"\"",
"{",
"length",
"=",
"parseGMT",
"(",
"value",
")",
"\n",
"return",
"length",
",",
"true",
"\n",
"}",
"\n",
"// Special Case 3: Some time zones are not named, but have +/-00 format",
"if",
"value",
"[",
"0",
"]",
"==",
"'+'",
"||",
"value",
"[",
"0",
"]",
"==",
"'-'",
"{",
"length",
"=",
"parseSignedOffset",
"(",
"value",
")",
"\n",
"return",
"length",
",",
"true",
"\n",
"}",
"\n",
"// How many upper-case letters are there? Need at least three, at most five.",
"var",
"nUpper",
"int",
"\n",
"for",
"nUpper",
"=",
"0",
";",
"nUpper",
"<",
"6",
";",
"nUpper",
"++",
"{",
"if",
"nUpper",
">=",
"len",
"(",
"value",
")",
"{",
"break",
"\n",
"}",
"\n",
"if",
"c",
":=",
"value",
"[",
"nUpper",
"]",
";",
"c",
"<",
"'A'",
"||",
"'Z'",
"<",
"c",
"{",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"switch",
"nUpper",
"{",
"case",
"0",
",",
"1",
",",
"2",
",",
"6",
":",
"return",
"0",
",",
"false",
"\n",
"case",
"5",
":",
"// Must end in T to match.",
"if",
"value",
"[",
"4",
"]",
"==",
"'T'",
"{",
"return",
"5",
",",
"true",
"\n",
"}",
"\n",
"case",
"4",
":",
"// Must end in T, except one special case.",
"if",
"value",
"[",
"3",
"]",
"==",
"'T'",
"||",
"value",
"[",
":",
"4",
"]",
"==",
"\"",
"\"",
"{",
"return",
"4",
",",
"true",
"\n",
"}",
"\n",
"case",
"3",
":",
"return",
"3",
",",
"true",
"\n",
"}",
"\n",
"return",
"0",
",",
"false",
"\n",
"}"
] | // parseTimeZone parses a time zone string and returns its length. Time zones
// are human-generated and unpredictable. We can't do precise error checking.
// On the other hand, for a correct parse there must be a time zone at the
// beginning of the string, so it's almost always true that there's one
// there. We look at the beginning of the string for a run of upper-case letters.
// If there are more than 5, it's an error.
// If there are 4 or 5 and the last is a T, it's a time zone.
// If there are 3, it's a time zone.
// Otherwise, other than special cases, it's not a time zone.
// GMT is special because it can have an hour offset. | [
"parseTimeZone",
"parses",
"a",
"time",
"zone",
"string",
"and",
"returns",
"its",
"length",
".",
"Time",
"zones",
"are",
"human",
"-",
"generated",
"and",
"unpredictable",
".",
"We",
"can",
"t",
"do",
"precise",
"error",
"checking",
".",
"On",
"the",
"other",
"hand",
"for",
"a",
"correct",
"parse",
"there",
"must",
"be",
"a",
"time",
"zone",
"at",
"the",
"beginning",
"of",
"the",
"string",
"so",
"it",
"s",
"almost",
"always",
"true",
"that",
"there",
"s",
"one",
"there",
".",
"We",
"look",
"at",
"the",
"beginning",
"of",
"the",
"string",
"for",
"a",
"run",
"of",
"upper",
"-",
"case",
"letters",
".",
"If",
"there",
"are",
"more",
"than",
"5",
"it",
"s",
"an",
"error",
".",
"If",
"there",
"are",
"4",
"or",
"5",
"and",
"the",
"last",
"is",
"a",
"T",
"it",
"s",
"a",
"time",
"zone",
".",
"If",
"there",
"are",
"3",
"it",
"s",
"a",
"time",
"zone",
".",
"Otherwise",
"other",
"than",
"special",
"cases",
"it",
"s",
"not",
"a",
"time",
"zone",
".",
"GMT",
"is",
"special",
"because",
"it",
"can",
"have",
"an",
"hour",
"offset",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/date_parser.go#L748-L792 | train |
dop251/goja | date_parser.go | parseGMT | func parseGMT(value string) int {
value = value[3:]
if len(value) == 0 {
return 3
}
return 3 + parseSignedOffset(value)
} | go | func parseGMT(value string) int {
value = value[3:]
if len(value) == 0 {
return 3
}
return 3 + parseSignedOffset(value)
} | [
"func",
"parseGMT",
"(",
"value",
"string",
")",
"int",
"{",
"value",
"=",
"value",
"[",
"3",
":",
"]",
"\n",
"if",
"len",
"(",
"value",
")",
"==",
"0",
"{",
"return",
"3",
"\n",
"}",
"\n\n",
"return",
"3",
"+",
"parseSignedOffset",
"(",
"value",
")",
"\n",
"}"
] | // parseGMT parses a GMT time zone. The input string is known to start "GMT".
// The function checks whether that is followed by a sign and a number in the
// range -14 through 12 excluding zero. | [
"parseGMT",
"parses",
"a",
"GMT",
"time",
"zone",
".",
"The",
"input",
"string",
"is",
"known",
"to",
"start",
"GMT",
".",
"The",
"function",
"checks",
"whether",
"that",
"is",
"followed",
"by",
"a",
"sign",
"and",
"a",
"number",
"in",
"the",
"range",
"-",
"14",
"through",
"12",
"excluding",
"zero",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/date_parser.go#L797-L804 | train |
dop251/goja | date_parser.go | startsWithLowerCase | func startsWithLowerCase(str string) bool {
if len(str) == 0 {
return false
}
c := str[0]
return 'a' <= c && c <= 'z'
} | go | func startsWithLowerCase(str string) bool {
if len(str) == 0 {
return false
}
c := str[0]
return 'a' <= c && c <= 'z'
} | [
"func",
"startsWithLowerCase",
"(",
"str",
"string",
")",
"bool",
"{",
"if",
"len",
"(",
"str",
")",
"==",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n",
"c",
":=",
"str",
"[",
"0",
"]",
"\n",
"return",
"'a'",
"<=",
"c",
"&&",
"c",
"<=",
"'z'",
"\n",
"}"
] | // startsWithLowerCase reports whether the string has a lower-case letter at the beginning.
// Its purpose is to prevent matching strings like "Month" when looking for "Mon". | [
"startsWithLowerCase",
"reports",
"whether",
"the",
"string",
"has",
"a",
"lower",
"-",
"case",
"letter",
"at",
"the",
"beginning",
".",
"Its",
"purpose",
"is",
"to",
"prevent",
"matching",
"strings",
"like",
"Month",
"when",
"looking",
"for",
"Mon",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/date_parser.go#L854-L860 | train |
dop251/goja | object_goreflect.go | SetFieldNameMapper | func (r *Runtime) SetFieldNameMapper(mapper FieldNameMapper) {
r.fieldNameMapper = mapper
r.typeInfoCache = nil
} | go | func (r *Runtime) SetFieldNameMapper(mapper FieldNameMapper) {
r.fieldNameMapper = mapper
r.typeInfoCache = nil
} | [
"func",
"(",
"r",
"*",
"Runtime",
")",
"SetFieldNameMapper",
"(",
"mapper",
"FieldNameMapper",
")",
"{",
"r",
".",
"fieldNameMapper",
"=",
"mapper",
"\n",
"r",
".",
"typeInfoCache",
"=",
"nil",
"\n",
"}"
] | // Sets a custom field name mapper for Go types. It can be called at any time, however
// the mapping for any given value is fixed at the point of creation.
// Setting this to nil restores the default behaviour which is all exported fields and methods are mapped to their
// original unchanged names. | [
"Sets",
"a",
"custom",
"field",
"name",
"mapper",
"for",
"Go",
"types",
".",
"It",
"can",
"be",
"called",
"at",
"any",
"time",
"however",
"the",
"mapping",
"for",
"any",
"given",
"value",
"is",
"fixed",
"at",
"the",
"point",
"of",
"creation",
".",
"Setting",
"this",
"to",
"nil",
"restores",
"the",
"default",
"behaviour",
"which",
"is",
"all",
"exported",
"fields",
"and",
"methods",
"are",
"mapped",
"to",
"their",
"original",
"unchanged",
"names",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/object_goreflect.go#L515-L518 | train |
dop251/goja | builtin_global.go | parseInt | func parseInt(s string, base int) (Value, error) {
var n int64
var err error
var cutoff, maxVal int64
var sign bool
i := 0
if len(s) < 1 {
err = strconv.ErrSyntax
goto Error
}
switch s[0] {
case '-':
sign = true
s = s[1:]
case '+':
s = s[1:]
}
if len(s) < 1 {
err = strconv.ErrSyntax
goto Error
}
// Look for hex prefix.
if s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X') {
if base == 0 || base == 16 {
base = 16
s = s[2:]
}
}
switch {
case len(s) < 1:
err = strconv.ErrSyntax
goto Error
case 2 <= base && base <= 36:
// valid base; nothing to do
case base == 0:
// Look for hex prefix.
switch {
case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'):
if len(s) < 3 {
err = strconv.ErrSyntax
goto Error
}
base = 16
s = s[2:]
default:
base = 10
}
default:
err = errors.New("invalid base " + strconv.Itoa(base))
goto Error
}
// Cutoff is the smallest number such that cutoff*base > maxInt64.
// Use compile-time constants for common cases.
switch base {
case 10:
cutoff = math.MaxInt64/10 + 1
case 16:
cutoff = math.MaxInt64/16 + 1
default:
cutoff = math.MaxInt64/int64(base) + 1
}
maxVal = math.MaxInt64
for ; i < len(s); i++ {
if n >= cutoff {
// n*base overflows
return parseLargeInt(float64(n), s[i:], base, sign)
}
v := digitVal(s[i])
if v >= base {
break
}
n *= int64(base)
n1 := n + int64(v)
if n1 < n || n1 > maxVal {
// n+v overflows
return parseLargeInt(float64(n)+float64(v), s[i+1:], base, sign)
}
n = n1
}
if i == 0 {
err = strconv.ErrSyntax
goto Error
}
if sign {
n = -n
}
return intToValue(n), nil
Error:
return _NaN, err
} | go | func parseInt(s string, base int) (Value, error) {
var n int64
var err error
var cutoff, maxVal int64
var sign bool
i := 0
if len(s) < 1 {
err = strconv.ErrSyntax
goto Error
}
switch s[0] {
case '-':
sign = true
s = s[1:]
case '+':
s = s[1:]
}
if len(s) < 1 {
err = strconv.ErrSyntax
goto Error
}
// Look for hex prefix.
if s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X') {
if base == 0 || base == 16 {
base = 16
s = s[2:]
}
}
switch {
case len(s) < 1:
err = strconv.ErrSyntax
goto Error
case 2 <= base && base <= 36:
// valid base; nothing to do
case base == 0:
// Look for hex prefix.
switch {
case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'):
if len(s) < 3 {
err = strconv.ErrSyntax
goto Error
}
base = 16
s = s[2:]
default:
base = 10
}
default:
err = errors.New("invalid base " + strconv.Itoa(base))
goto Error
}
// Cutoff is the smallest number such that cutoff*base > maxInt64.
// Use compile-time constants for common cases.
switch base {
case 10:
cutoff = math.MaxInt64/10 + 1
case 16:
cutoff = math.MaxInt64/16 + 1
default:
cutoff = math.MaxInt64/int64(base) + 1
}
maxVal = math.MaxInt64
for ; i < len(s); i++ {
if n >= cutoff {
// n*base overflows
return parseLargeInt(float64(n), s[i:], base, sign)
}
v := digitVal(s[i])
if v >= base {
break
}
n *= int64(base)
n1 := n + int64(v)
if n1 < n || n1 > maxVal {
// n+v overflows
return parseLargeInt(float64(n)+float64(v), s[i+1:], base, sign)
}
n = n1
}
if i == 0 {
err = strconv.ErrSyntax
goto Error
}
if sign {
n = -n
}
return intToValue(n), nil
Error:
return _NaN, err
} | [
"func",
"parseInt",
"(",
"s",
"string",
",",
"base",
"int",
")",
"(",
"Value",
",",
"error",
")",
"{",
"var",
"n",
"int64",
"\n",
"var",
"err",
"error",
"\n",
"var",
"cutoff",
",",
"maxVal",
"int64",
"\n",
"var",
"sign",
"bool",
"\n",
"i",
":=",
"0",
"\n\n",
"if",
"len",
"(",
"s",
")",
"<",
"1",
"{",
"err",
"=",
"strconv",
".",
"ErrSyntax",
"\n",
"goto",
"Error",
"\n",
"}",
"\n\n",
"switch",
"s",
"[",
"0",
"]",
"{",
"case",
"'-'",
":",
"sign",
"=",
"true",
"\n",
"s",
"=",
"s",
"[",
"1",
":",
"]",
"\n",
"case",
"'+'",
":",
"s",
"=",
"s",
"[",
"1",
":",
"]",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"s",
")",
"<",
"1",
"{",
"err",
"=",
"strconv",
".",
"ErrSyntax",
"\n",
"goto",
"Error",
"\n",
"}",
"\n\n",
"// Look for hex prefix.",
"if",
"s",
"[",
"0",
"]",
"==",
"'0'",
"&&",
"len",
"(",
"s",
")",
">",
"1",
"&&",
"(",
"s",
"[",
"1",
"]",
"==",
"'x'",
"||",
"s",
"[",
"1",
"]",
"==",
"'X'",
")",
"{",
"if",
"base",
"==",
"0",
"||",
"base",
"==",
"16",
"{",
"base",
"=",
"16",
"\n",
"s",
"=",
"s",
"[",
"2",
":",
"]",
"\n",
"}",
"\n",
"}",
"\n\n",
"switch",
"{",
"case",
"len",
"(",
"s",
")",
"<",
"1",
":",
"err",
"=",
"strconv",
".",
"ErrSyntax",
"\n",
"goto",
"Error",
"\n\n",
"case",
"2",
"<=",
"base",
"&&",
"base",
"<=",
"36",
":",
"// valid base; nothing to do",
"case",
"base",
"==",
"0",
":",
"// Look for hex prefix.",
"switch",
"{",
"case",
"s",
"[",
"0",
"]",
"==",
"'0'",
"&&",
"len",
"(",
"s",
")",
">",
"1",
"&&",
"(",
"s",
"[",
"1",
"]",
"==",
"'x'",
"||",
"s",
"[",
"1",
"]",
"==",
"'X'",
")",
":",
"if",
"len",
"(",
"s",
")",
"<",
"3",
"{",
"err",
"=",
"strconv",
".",
"ErrSyntax",
"\n",
"goto",
"Error",
"\n",
"}",
"\n",
"base",
"=",
"16",
"\n",
"s",
"=",
"s",
"[",
"2",
":",
"]",
"\n",
"default",
":",
"base",
"=",
"10",
"\n",
"}",
"\n\n",
"default",
":",
"err",
"=",
"errors",
".",
"New",
"(",
"\"",
"\"",
"+",
"strconv",
".",
"Itoa",
"(",
"base",
")",
")",
"\n",
"goto",
"Error",
"\n",
"}",
"\n\n",
"// Cutoff is the smallest number such that cutoff*base > maxInt64.",
"// Use compile-time constants for common cases.",
"switch",
"base",
"{",
"case",
"10",
":",
"cutoff",
"=",
"math",
".",
"MaxInt64",
"/",
"10",
"+",
"1",
"\n",
"case",
"16",
":",
"cutoff",
"=",
"math",
".",
"MaxInt64",
"/",
"16",
"+",
"1",
"\n",
"default",
":",
"cutoff",
"=",
"math",
".",
"MaxInt64",
"/",
"int64",
"(",
"base",
")",
"+",
"1",
"\n",
"}",
"\n\n",
"maxVal",
"=",
"math",
".",
"MaxInt64",
"\n",
"for",
";",
"i",
"<",
"len",
"(",
"s",
")",
";",
"i",
"++",
"{",
"if",
"n",
">=",
"cutoff",
"{",
"// n*base overflows",
"return",
"parseLargeInt",
"(",
"float64",
"(",
"n",
")",
",",
"s",
"[",
"i",
":",
"]",
",",
"base",
",",
"sign",
")",
"\n",
"}",
"\n",
"v",
":=",
"digitVal",
"(",
"s",
"[",
"i",
"]",
")",
"\n",
"if",
"v",
">=",
"base",
"{",
"break",
"\n",
"}",
"\n",
"n",
"*=",
"int64",
"(",
"base",
")",
"\n\n",
"n1",
":=",
"n",
"+",
"int64",
"(",
"v",
")",
"\n",
"if",
"n1",
"<",
"n",
"||",
"n1",
">",
"maxVal",
"{",
"// n+v overflows",
"return",
"parseLargeInt",
"(",
"float64",
"(",
"n",
")",
"+",
"float64",
"(",
"v",
")",
",",
"s",
"[",
"i",
"+",
"1",
":",
"]",
",",
"base",
",",
"sign",
")",
"\n",
"}",
"\n",
"n",
"=",
"n1",
"\n",
"}",
"\n\n",
"if",
"i",
"==",
"0",
"{",
"err",
"=",
"strconv",
".",
"ErrSyntax",
"\n",
"goto",
"Error",
"\n",
"}",
"\n\n",
"if",
"sign",
"{",
"n",
"=",
"-",
"n",
"\n",
"}",
"\n",
"return",
"intToValue",
"(",
"n",
")",
",",
"nil",
"\n\n",
"Error",
":",
"return",
"_NaN",
",",
"err",
"\n",
"}"
] | // ECMAScript compatible version of strconv.ParseInt | [
"ECMAScript",
"compatible",
"version",
"of",
"strconv",
".",
"ParseInt"
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/builtin_global.go#L275-L378 | train |
dop251/goja | string_ascii.go | strToInt | func strToInt(ss string) (int64, error) {
if ss == "" {
return 0, nil
}
if ss == "-0" {
return 0, strconv.ErrSyntax
}
if len(ss) > 2 {
switch ss[:2] {
case "0x", "0X":
i, _ := strconv.ParseInt(ss[2:], 16, 64)
return i, nil
case "0b", "0B":
i, _ := strconv.ParseInt(ss[2:], 2, 64)
return i, nil
case "0o", "0O":
i, _ := strconv.ParseInt(ss[2:], 8, 64)
return i, nil
}
}
return strconv.ParseInt(ss, 10, 64)
} | go | func strToInt(ss string) (int64, error) {
if ss == "" {
return 0, nil
}
if ss == "-0" {
return 0, strconv.ErrSyntax
}
if len(ss) > 2 {
switch ss[:2] {
case "0x", "0X":
i, _ := strconv.ParseInt(ss[2:], 16, 64)
return i, nil
case "0b", "0B":
i, _ := strconv.ParseInt(ss[2:], 2, 64)
return i, nil
case "0o", "0O":
i, _ := strconv.ParseInt(ss[2:], 8, 64)
return i, nil
}
}
return strconv.ParseInt(ss, 10, 64)
} | [
"func",
"strToInt",
"(",
"ss",
"string",
")",
"(",
"int64",
",",
"error",
")",
"{",
"if",
"ss",
"==",
"\"",
"\"",
"{",
"return",
"0",
",",
"nil",
"\n",
"}",
"\n",
"if",
"ss",
"==",
"\"",
"\"",
"{",
"return",
"0",
",",
"strconv",
".",
"ErrSyntax",
"\n",
"}",
"\n",
"if",
"len",
"(",
"ss",
")",
">",
"2",
"{",
"switch",
"ss",
"[",
":",
"2",
"]",
"{",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"i",
",",
"_",
":=",
"strconv",
".",
"ParseInt",
"(",
"ss",
"[",
"2",
":",
"]",
",",
"16",
",",
"64",
")",
"\n",
"return",
"i",
",",
"nil",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"i",
",",
"_",
":=",
"strconv",
".",
"ParseInt",
"(",
"ss",
"[",
"2",
":",
"]",
",",
"2",
",",
"64",
")",
"\n",
"return",
"i",
",",
"nil",
"\n",
"case",
"\"",
"\"",
",",
"\"",
"\"",
":",
"i",
",",
"_",
":=",
"strconv",
".",
"ParseInt",
"(",
"ss",
"[",
"2",
":",
"]",
",",
"8",
",",
"64",
")",
"\n",
"return",
"i",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"strconv",
".",
"ParseInt",
"(",
"ss",
",",
"10",
",",
"64",
")",
"\n",
"}"
] | // ss must be trimmed | [
"ss",
"must",
"be",
"trimmed"
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/string_ascii.go#L37-L58 | train |
dop251/goja | runtime.go | MustCompile | func MustCompile(name, src string, strict bool) *Program {
prg, err := Compile(name, src, strict)
if err != nil {
panic(err)
}
return prg
} | go | func MustCompile(name, src string, strict bool) *Program {
prg, err := Compile(name, src, strict)
if err != nil {
panic(err)
}
return prg
} | [
"func",
"MustCompile",
"(",
"name",
",",
"src",
"string",
",",
"strict",
"bool",
")",
"*",
"Program",
"{",
"prg",
",",
"err",
":=",
"Compile",
"(",
"name",
",",
"src",
",",
"strict",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"err",
")",
"\n",
"}",
"\n\n",
"return",
"prg",
"\n",
"}"
] | // MustCompile is like Compile but panics if the code cannot be compiled.
// It simplifies safe initialization of global variables holding compiled JavaScript code. | [
"MustCompile",
"is",
"like",
"Compile",
"but",
"panics",
"if",
"the",
"code",
"cannot",
"be",
"compiled",
".",
"It",
"simplifies",
"safe",
"initialization",
"of",
"global",
"variables",
"holding",
"compiled",
"JavaScript",
"code",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/runtime.go#L781-L788 | train |
dop251/goja | runtime.go | RunString | func (r *Runtime) RunString(str string) (Value, error) {
return r.RunScript("", str)
} | go | func (r *Runtime) RunString(str string) (Value, error) {
return r.RunScript("", str)
} | [
"func",
"(",
"r",
"*",
"Runtime",
")",
"RunString",
"(",
"str",
"string",
")",
"(",
"Value",
",",
"error",
")",
"{",
"return",
"r",
".",
"RunScript",
"(",
"\"",
"\"",
",",
"str",
")",
"\n",
"}"
] | // RunString executes the given string in the global context. | [
"RunString",
"executes",
"the",
"given",
"string",
"in",
"the",
"global",
"context",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/runtime.go#L858-L860 | train |
dop251/goja | runtime.go | RunScript | func (r *Runtime) RunScript(name, src string) (Value, error) {
p, err := Compile(name, src, false)
if err != nil {
return nil, err
}
return r.RunProgram(p)
} | go | func (r *Runtime) RunScript(name, src string) (Value, error) {
p, err := Compile(name, src, false)
if err != nil {
return nil, err
}
return r.RunProgram(p)
} | [
"func",
"(",
"r",
"*",
"Runtime",
")",
"RunScript",
"(",
"name",
",",
"src",
"string",
")",
"(",
"Value",
",",
"error",
")",
"{",
"p",
",",
"err",
":=",
"Compile",
"(",
"name",
",",
"src",
",",
"false",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"r",
".",
"RunProgram",
"(",
"p",
")",
"\n",
"}"
] | // RunScript executes the given string in the global context. | [
"RunScript",
"executes",
"the",
"given",
"string",
"in",
"the",
"global",
"context",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/runtime.go#L863-L871 | train |
dop251/goja | runtime.go | ExportTo | func (r *Runtime) ExportTo(v Value, target interface{}) error {
tval := reflect.ValueOf(target)
if tval.Kind() != reflect.Ptr || tval.IsNil() {
return errors.New("target must be a non-nil pointer")
}
vv, err := r.toReflectValue(v, tval.Elem().Type())
if err != nil {
return err
}
tval.Elem().Set(vv)
return nil
} | go | func (r *Runtime) ExportTo(v Value, target interface{}) error {
tval := reflect.ValueOf(target)
if tval.Kind() != reflect.Ptr || tval.IsNil() {
return errors.New("target must be a non-nil pointer")
}
vv, err := r.toReflectValue(v, tval.Elem().Type())
if err != nil {
return err
}
tval.Elem().Set(vv)
return nil
} | [
"func",
"(",
"r",
"*",
"Runtime",
")",
"ExportTo",
"(",
"v",
"Value",
",",
"target",
"interface",
"{",
"}",
")",
"error",
"{",
"tval",
":=",
"reflect",
".",
"ValueOf",
"(",
"target",
")",
"\n",
"if",
"tval",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Ptr",
"||",
"tval",
".",
"IsNil",
"(",
")",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"vv",
",",
"err",
":=",
"r",
".",
"toReflectValue",
"(",
"v",
",",
"tval",
".",
"Elem",
"(",
")",
".",
"Type",
"(",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"tval",
".",
"Elem",
"(",
")",
".",
"Set",
"(",
"vv",
")",
"\n",
"return",
"nil",
"\n",
"}"
] | // ExportTo converts a JavaScript value into the specified Go value. The second parameter must be a non-nil pointer.
// Returns error if conversion is not possible. | [
"ExportTo",
"converts",
"a",
"JavaScript",
"value",
"into",
"the",
"specified",
"Go",
"value",
".",
"The",
"second",
"parameter",
"must",
"be",
"a",
"non",
"-",
"nil",
"pointer",
".",
"Returns",
"error",
"if",
"conversion",
"is",
"not",
"possible",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/runtime.go#L1360-L1371 | train |
dop251/goja | runtime.go | Get | func (r *Runtime) Get(name string) Value {
return r.globalObject.self.getStr(name)
} | go | func (r *Runtime) Get(name string) Value {
return r.globalObject.self.getStr(name)
} | [
"func",
"(",
"r",
"*",
"Runtime",
")",
"Get",
"(",
"name",
"string",
")",
"Value",
"{",
"return",
"r",
".",
"globalObject",
".",
"self",
".",
"getStr",
"(",
"name",
")",
"\n",
"}"
] | // Get the specified property of the global object. | [
"Get",
"the",
"specified",
"property",
"of",
"the",
"global",
"object",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/runtime.go#L1385-L1387 | train |
dop251/goja | runtime.go | AssertFunction | func AssertFunction(v Value) (Callable, bool) {
if obj, ok := v.(*Object); ok {
if f, ok := obj.self.assertCallable(); ok {
return func(this Value, args ...Value) (ret Value, err error) {
defer func() {
if x := recover(); x != nil {
if ex, ok := x.(*InterruptedError); ok {
err = ex
} else {
panic(x)
}
}
}()
ex := obj.runtime.vm.try(func() {
ret = f(FunctionCall{
This: this,
Arguments: args,
})
})
if ex != nil {
err = ex
}
obj.runtime.vm.clearStack()
return
}, true
}
}
return nil, false
} | go | func AssertFunction(v Value) (Callable, bool) {
if obj, ok := v.(*Object); ok {
if f, ok := obj.self.assertCallable(); ok {
return func(this Value, args ...Value) (ret Value, err error) {
defer func() {
if x := recover(); x != nil {
if ex, ok := x.(*InterruptedError); ok {
err = ex
} else {
panic(x)
}
}
}()
ex := obj.runtime.vm.try(func() {
ret = f(FunctionCall{
This: this,
Arguments: args,
})
})
if ex != nil {
err = ex
}
obj.runtime.vm.clearStack()
return
}, true
}
}
return nil, false
} | [
"func",
"AssertFunction",
"(",
"v",
"Value",
")",
"(",
"Callable",
",",
"bool",
")",
"{",
"if",
"obj",
",",
"ok",
":=",
"v",
".",
"(",
"*",
"Object",
")",
";",
"ok",
"{",
"if",
"f",
",",
"ok",
":=",
"obj",
".",
"self",
".",
"assertCallable",
"(",
")",
";",
"ok",
"{",
"return",
"func",
"(",
"this",
"Value",
",",
"args",
"...",
"Value",
")",
"(",
"ret",
"Value",
",",
"err",
"error",
")",
"{",
"defer",
"func",
"(",
")",
"{",
"if",
"x",
":=",
"recover",
"(",
")",
";",
"x",
"!=",
"nil",
"{",
"if",
"ex",
",",
"ok",
":=",
"x",
".",
"(",
"*",
"InterruptedError",
")",
";",
"ok",
"{",
"err",
"=",
"ex",
"\n",
"}",
"else",
"{",
"panic",
"(",
"x",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"(",
")",
"\n",
"ex",
":=",
"obj",
".",
"runtime",
".",
"vm",
".",
"try",
"(",
"func",
"(",
")",
"{",
"ret",
"=",
"f",
"(",
"FunctionCall",
"{",
"This",
":",
"this",
",",
"Arguments",
":",
"args",
",",
"}",
")",
"\n",
"}",
")",
"\n",
"if",
"ex",
"!=",
"nil",
"{",
"err",
"=",
"ex",
"\n",
"}",
"\n",
"obj",
".",
"runtime",
".",
"vm",
".",
"clearStack",
"(",
")",
"\n",
"return",
"\n",
"}",
",",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"nil",
",",
"false",
"\n",
"}"
] | // AssertFunction checks if the Value is a function and returns a Callable. | [
"AssertFunction",
"checks",
"if",
"the",
"Value",
"is",
"a",
"function",
"and",
"returns",
"a",
"Callable",
"."
] | 8d6ee3d1661108ff8433016620abb64dfa6d9937 | https://github.com/dop251/goja/blob/8d6ee3d1661108ff8433016620abb64dfa6d9937/runtime.go#L1398-L1426 | train |
pelletier/go-toml | marshal.go | isPrimitive | func isPrimitive(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Ptr:
return isPrimitive(mtype.Elem())
case reflect.Bool:
return true
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
case reflect.Float32, reflect.Float64:
return true
case reflect.String:
return true
case reflect.Struct:
return mtype == timeType || isCustomMarshaler(mtype)
default:
return false
}
} | go | func isPrimitive(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Ptr:
return isPrimitive(mtype.Elem())
case reflect.Bool:
return true
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
case reflect.Float32, reflect.Float64:
return true
case reflect.String:
return true
case reflect.Struct:
return mtype == timeType || isCustomMarshaler(mtype)
default:
return false
}
} | [
"func",
"isPrimitive",
"(",
"mtype",
"reflect",
".",
"Type",
")",
"bool",
"{",
"switch",
"mtype",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Ptr",
":",
"return",
"isPrimitive",
"(",
"mtype",
".",
"Elem",
"(",
")",
")",
"\n",
"case",
"reflect",
".",
"Bool",
":",
"return",
"true",
"\n",
"case",
"reflect",
".",
"Int",
",",
"reflect",
".",
"Int8",
",",
"reflect",
".",
"Int16",
",",
"reflect",
".",
"Int32",
",",
"reflect",
".",
"Int64",
":",
"return",
"true",
"\n",
"case",
"reflect",
".",
"Uint",
",",
"reflect",
".",
"Uint8",
",",
"reflect",
".",
"Uint16",
",",
"reflect",
".",
"Uint32",
",",
"reflect",
".",
"Uint64",
":",
"return",
"true",
"\n",
"case",
"reflect",
".",
"Float32",
",",
"reflect",
".",
"Float64",
":",
"return",
"true",
"\n",
"case",
"reflect",
".",
"String",
":",
"return",
"true",
"\n",
"case",
"reflect",
".",
"Struct",
":",
"return",
"mtype",
"==",
"timeType",
"||",
"isCustomMarshaler",
"(",
"mtype",
")",
"\n",
"default",
":",
"return",
"false",
"\n",
"}",
"\n",
"}"
] | // Check if the given marshal type maps to a Tree primitive | [
"Check",
"if",
"the",
"given",
"marshal",
"type",
"maps",
"to",
"a",
"Tree",
"primitive"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L73-L92 | train |
pelletier/go-toml | marshal.go | isTreeSlice | func isTreeSlice(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Slice:
return !isOtherSlice(mtype)
default:
return false
}
} | go | func isTreeSlice(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Slice:
return !isOtherSlice(mtype)
default:
return false
}
} | [
"func",
"isTreeSlice",
"(",
"mtype",
"reflect",
".",
"Type",
")",
"bool",
"{",
"switch",
"mtype",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Slice",
":",
"return",
"!",
"isOtherSlice",
"(",
"mtype",
")",
"\n",
"default",
":",
"return",
"false",
"\n",
"}",
"\n",
"}"
] | // Check if the given marshal type maps to a Tree slice | [
"Check",
"if",
"the",
"given",
"marshal",
"type",
"maps",
"to",
"a",
"Tree",
"slice"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L95-L102 | train |
pelletier/go-toml | marshal.go | isOtherSlice | func isOtherSlice(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Ptr:
return isOtherSlice(mtype.Elem())
case reflect.Slice:
return isPrimitive(mtype.Elem()) || isOtherSlice(mtype.Elem())
default:
return false
}
} | go | func isOtherSlice(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Ptr:
return isOtherSlice(mtype.Elem())
case reflect.Slice:
return isPrimitive(mtype.Elem()) || isOtherSlice(mtype.Elem())
default:
return false
}
} | [
"func",
"isOtherSlice",
"(",
"mtype",
"reflect",
".",
"Type",
")",
"bool",
"{",
"switch",
"mtype",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Ptr",
":",
"return",
"isOtherSlice",
"(",
"mtype",
".",
"Elem",
"(",
")",
")",
"\n",
"case",
"reflect",
".",
"Slice",
":",
"return",
"isPrimitive",
"(",
"mtype",
".",
"Elem",
"(",
")",
")",
"||",
"isOtherSlice",
"(",
"mtype",
".",
"Elem",
"(",
")",
")",
"\n",
"default",
":",
"return",
"false",
"\n",
"}",
"\n",
"}"
] | // Check if the given marshal type maps to a non-Tree slice | [
"Check",
"if",
"the",
"given",
"marshal",
"type",
"maps",
"to",
"a",
"non",
"-",
"Tree",
"slice"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L105-L114 | train |
pelletier/go-toml | marshal.go | isTree | func isTree(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Map:
return true
case reflect.Struct:
return !isPrimitive(mtype)
default:
return false
}
} | go | func isTree(mtype reflect.Type) bool {
switch mtype.Kind() {
case reflect.Map:
return true
case reflect.Struct:
return !isPrimitive(mtype)
default:
return false
}
} | [
"func",
"isTree",
"(",
"mtype",
"reflect",
".",
"Type",
")",
"bool",
"{",
"switch",
"mtype",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Map",
":",
"return",
"true",
"\n",
"case",
"reflect",
".",
"Struct",
":",
"return",
"!",
"isPrimitive",
"(",
"mtype",
")",
"\n",
"default",
":",
"return",
"false",
"\n",
"}",
"\n",
"}"
] | // Check if the given marshal type maps to a Tree | [
"Check",
"if",
"the",
"given",
"marshal",
"type",
"maps",
"to",
"a",
"Tree"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L117-L126 | train |
pelletier/go-toml | marshal.go | Encode | func (e *Encoder) Encode(v interface{}) error {
b, err := e.marshal(v)
if err != nil {
return err
}
if _, err := e.w.Write(b); err != nil {
return err
}
return nil
} | go | func (e *Encoder) Encode(v interface{}) error {
b, err := e.marshal(v)
if err != nil {
return err
}
if _, err := e.w.Write(b); err != nil {
return err
}
return nil
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"Encode",
"(",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"b",
",",
"err",
":=",
"e",
".",
"marshal",
"(",
"v",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"if",
"_",
",",
"err",
":=",
"e",
".",
"w",
".",
"Write",
"(",
"b",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // Encode writes the TOML encoding of v to the stream.
//
// See the documentation for Marshal for details. | [
"Encode",
"writes",
"the",
"TOML",
"encoding",
"of",
"v",
"to",
"the",
"stream",
".",
"See",
"the",
"documentation",
"for",
"Marshal",
"for",
"details",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L206-L215 | train |
pelletier/go-toml | marshal.go | QuoteMapKeys | func (e *Encoder) QuoteMapKeys(v bool) *Encoder {
e.quoteMapKeys = v
return e
} | go | func (e *Encoder) QuoteMapKeys(v bool) *Encoder {
e.quoteMapKeys = v
return e
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"QuoteMapKeys",
"(",
"v",
"bool",
")",
"*",
"Encoder",
"{",
"e",
".",
"quoteMapKeys",
"=",
"v",
"\n",
"return",
"e",
"\n",
"}"
] | // QuoteMapKeys sets up the encoder to encode
// maps with string type keys with quoted TOML keys.
//
// This relieves the character limitations on map keys. | [
"QuoteMapKeys",
"sets",
"up",
"the",
"encoder",
"to",
"encode",
"maps",
"with",
"string",
"type",
"keys",
"with",
"quoted",
"TOML",
"keys",
".",
"This",
"relieves",
"the",
"character",
"limitations",
"on",
"map",
"keys",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L221-L224 | train |
pelletier/go-toml | marshal.go | Order | func (e *Encoder) Order(ord marshalOrder) *Encoder {
e.order = ord
return e
} | go | func (e *Encoder) Order(ord marshalOrder) *Encoder {
e.order = ord
return e
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"Order",
"(",
"ord",
"marshalOrder",
")",
"*",
"Encoder",
"{",
"e",
".",
"order",
"=",
"ord",
"\n",
"return",
"e",
"\n",
"}"
] | // Order allows to change in which order fields will be written to the output stream. | [
"Order",
"allows",
"to",
"change",
"in",
"which",
"order",
"fields",
"will",
"be",
"written",
"to",
"the",
"output",
"stream",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L246-L249 | train |
pelletier/go-toml | marshal.go | SetTagComment | func (e *Encoder) SetTagComment(v string) *Encoder {
e.comment = v
return e
} | go | func (e *Encoder) SetTagComment(v string) *Encoder {
e.comment = v
return e
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"SetTagComment",
"(",
"v",
"string",
")",
"*",
"Encoder",
"{",
"e",
".",
"comment",
"=",
"v",
"\n",
"return",
"e",
"\n",
"}"
] | // SetTagComment allows changing default tag "comment" | [
"SetTagComment",
"allows",
"changing",
"default",
"tag",
"comment"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L258-L261 | train |
pelletier/go-toml | marshal.go | SetTagCommented | func (e *Encoder) SetTagCommented(v string) *Encoder {
e.commented = v
return e
} | go | func (e *Encoder) SetTagCommented(v string) *Encoder {
e.commented = v
return e
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"SetTagCommented",
"(",
"v",
"string",
")",
"*",
"Encoder",
"{",
"e",
".",
"commented",
"=",
"v",
"\n",
"return",
"e",
"\n",
"}"
] | // SetTagCommented allows changing default tag "commented" | [
"SetTagCommented",
"allows",
"changing",
"default",
"tag",
"commented"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L264-L267 | train |
pelletier/go-toml | marshal.go | SetTagMultiline | func (e *Encoder) SetTagMultiline(v string) *Encoder {
e.multiline = v
return e
} | go | func (e *Encoder) SetTagMultiline(v string) *Encoder {
e.multiline = v
return e
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"SetTagMultiline",
"(",
"v",
"string",
")",
"*",
"Encoder",
"{",
"e",
".",
"multiline",
"=",
"v",
"\n",
"return",
"e",
"\n",
"}"
] | // SetTagMultiline allows changing default tag "multiline" | [
"SetTagMultiline",
"allows",
"changing",
"default",
"tag",
"multiline"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L270-L273 | train |
pelletier/go-toml | marshal.go | nextTree | func (e *Encoder) nextTree() *Tree {
return newTreeWithPosition(Position{Line: e.line, Col: 1})
} | go | func (e *Encoder) nextTree() *Tree {
return newTreeWithPosition(Position{Line: e.line, Col: 1})
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"nextTree",
"(",
")",
"*",
"Tree",
"{",
"return",
"newTreeWithPosition",
"(",
"Position",
"{",
"Line",
":",
"e",
".",
"line",
",",
"Col",
":",
"1",
"}",
")",
"\n",
"}"
] | // Create next tree with a position based on Encoder.line | [
"Create",
"next",
"tree",
"with",
"a",
"position",
"based",
"on",
"Encoder",
".",
"line"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L304-L306 | train |
pelletier/go-toml | marshal.go | valueToTree | func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, error) {
if mtype.Kind() == reflect.Ptr {
return e.valueToTree(mtype.Elem(), mval.Elem())
}
tval := e.nextTree()
switch mtype.Kind() {
case reflect.Struct:
for i := 0; i < mtype.NumField(); i++ {
mtypef, mvalf := mtype.Field(i), mval.Field(i)
opts := tomlOptions(mtypef, e.annotation)
if opts.include && (!opts.omitempty || !isZero(mvalf)) {
val, err := e.valueToToml(mtypef.Type, mvalf)
if err != nil {
return nil, err
}
tval.SetWithOptions(opts.name, SetOptions{
Comment: opts.comment,
Commented: opts.commented,
Multiline: opts.multiline,
}, val)
}
}
case reflect.Map:
keys := mval.MapKeys()
if e.order == OrderPreserve && len(keys) > 0 {
// Sorting []reflect.Value is not straight forward.
//
// OrderPreserve will support deterministic results when string is used
// as the key to maps.
typ := keys[0].Type()
kind := keys[0].Kind()
if kind == reflect.String {
ikeys := make([]string, len(keys))
for i := range keys {
ikeys[i] = keys[i].Interface().(string)
}
sort.Strings(ikeys)
for i := range ikeys {
keys[i] = reflect.ValueOf(ikeys[i]).Convert(typ)
}
}
}
for _, key := range keys {
mvalf := mval.MapIndex(key)
val, err := e.valueToToml(mtype.Elem(), mvalf)
if err != nil {
return nil, err
}
if e.quoteMapKeys {
keyStr, err := tomlValueStringRepresentation(key.String(), "", e.arraysOneElementPerLine)
if err != nil {
return nil, err
}
tval.SetPath([]string{keyStr}, val)
} else {
tval.Set(key.String(), val)
}
}
}
return tval, nil
} | go | func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, error) {
if mtype.Kind() == reflect.Ptr {
return e.valueToTree(mtype.Elem(), mval.Elem())
}
tval := e.nextTree()
switch mtype.Kind() {
case reflect.Struct:
for i := 0; i < mtype.NumField(); i++ {
mtypef, mvalf := mtype.Field(i), mval.Field(i)
opts := tomlOptions(mtypef, e.annotation)
if opts.include && (!opts.omitempty || !isZero(mvalf)) {
val, err := e.valueToToml(mtypef.Type, mvalf)
if err != nil {
return nil, err
}
tval.SetWithOptions(opts.name, SetOptions{
Comment: opts.comment,
Commented: opts.commented,
Multiline: opts.multiline,
}, val)
}
}
case reflect.Map:
keys := mval.MapKeys()
if e.order == OrderPreserve && len(keys) > 0 {
// Sorting []reflect.Value is not straight forward.
//
// OrderPreserve will support deterministic results when string is used
// as the key to maps.
typ := keys[0].Type()
kind := keys[0].Kind()
if kind == reflect.String {
ikeys := make([]string, len(keys))
for i := range keys {
ikeys[i] = keys[i].Interface().(string)
}
sort.Strings(ikeys)
for i := range ikeys {
keys[i] = reflect.ValueOf(ikeys[i]).Convert(typ)
}
}
}
for _, key := range keys {
mvalf := mval.MapIndex(key)
val, err := e.valueToToml(mtype.Elem(), mvalf)
if err != nil {
return nil, err
}
if e.quoteMapKeys {
keyStr, err := tomlValueStringRepresentation(key.String(), "", e.arraysOneElementPerLine)
if err != nil {
return nil, err
}
tval.SetPath([]string{keyStr}, val)
} else {
tval.Set(key.String(), val)
}
}
}
return tval, nil
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"valueToTree",
"(",
"mtype",
"reflect",
".",
"Type",
",",
"mval",
"reflect",
".",
"Value",
")",
"(",
"*",
"Tree",
",",
"error",
")",
"{",
"if",
"mtype",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
"{",
"return",
"e",
".",
"valueToTree",
"(",
"mtype",
".",
"Elem",
"(",
")",
",",
"mval",
".",
"Elem",
"(",
")",
")",
"\n",
"}",
"\n",
"tval",
":=",
"e",
".",
"nextTree",
"(",
")",
"\n",
"switch",
"mtype",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Struct",
":",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"mtype",
".",
"NumField",
"(",
")",
";",
"i",
"++",
"{",
"mtypef",
",",
"mvalf",
":=",
"mtype",
".",
"Field",
"(",
"i",
")",
",",
"mval",
".",
"Field",
"(",
"i",
")",
"\n",
"opts",
":=",
"tomlOptions",
"(",
"mtypef",
",",
"e",
".",
"annotation",
")",
"\n",
"if",
"opts",
".",
"include",
"&&",
"(",
"!",
"opts",
".",
"omitempty",
"||",
"!",
"isZero",
"(",
"mvalf",
")",
")",
"{",
"val",
",",
"err",
":=",
"e",
".",
"valueToToml",
"(",
"mtypef",
".",
"Type",
",",
"mvalf",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"tval",
".",
"SetWithOptions",
"(",
"opts",
".",
"name",
",",
"SetOptions",
"{",
"Comment",
":",
"opts",
".",
"comment",
",",
"Commented",
":",
"opts",
".",
"commented",
",",
"Multiline",
":",
"opts",
".",
"multiline",
",",
"}",
",",
"val",
")",
"\n",
"}",
"\n",
"}",
"\n",
"case",
"reflect",
".",
"Map",
":",
"keys",
":=",
"mval",
".",
"MapKeys",
"(",
")",
"\n",
"if",
"e",
".",
"order",
"==",
"OrderPreserve",
"&&",
"len",
"(",
"keys",
")",
">",
"0",
"{",
"// Sorting []reflect.Value is not straight forward.",
"//",
"// OrderPreserve will support deterministic results when string is used",
"// as the key to maps.",
"typ",
":=",
"keys",
"[",
"0",
"]",
".",
"Type",
"(",
")",
"\n",
"kind",
":=",
"keys",
"[",
"0",
"]",
".",
"Kind",
"(",
")",
"\n",
"if",
"kind",
"==",
"reflect",
".",
"String",
"{",
"ikeys",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"keys",
")",
")",
"\n",
"for",
"i",
":=",
"range",
"keys",
"{",
"ikeys",
"[",
"i",
"]",
"=",
"keys",
"[",
"i",
"]",
".",
"Interface",
"(",
")",
".",
"(",
"string",
")",
"\n",
"}",
"\n",
"sort",
".",
"Strings",
"(",
"ikeys",
")",
"\n",
"for",
"i",
":=",
"range",
"ikeys",
"{",
"keys",
"[",
"i",
"]",
"=",
"reflect",
".",
"ValueOf",
"(",
"ikeys",
"[",
"i",
"]",
")",
".",
"Convert",
"(",
"typ",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"for",
"_",
",",
"key",
":=",
"range",
"keys",
"{",
"mvalf",
":=",
"mval",
".",
"MapIndex",
"(",
"key",
")",
"\n",
"val",
",",
"err",
":=",
"e",
".",
"valueToToml",
"(",
"mtype",
".",
"Elem",
"(",
")",
",",
"mvalf",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"e",
".",
"quoteMapKeys",
"{",
"keyStr",
",",
"err",
":=",
"tomlValueStringRepresentation",
"(",
"key",
".",
"String",
"(",
")",
",",
"\"",
"\"",
",",
"e",
".",
"arraysOneElementPerLine",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"tval",
".",
"SetPath",
"(",
"[",
"]",
"string",
"{",
"keyStr",
"}",
",",
"val",
")",
"\n",
"}",
"else",
"{",
"tval",
".",
"Set",
"(",
"key",
".",
"String",
"(",
")",
",",
"val",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"tval",
",",
"nil",
"\n",
"}"
] | // Convert given marshal struct or map value to toml tree | [
"Convert",
"given",
"marshal",
"struct",
"or",
"map",
"value",
"to",
"toml",
"tree"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L309-L370 | train |
pelletier/go-toml | marshal.go | valueToTreeSlice | func (e *Encoder) valueToTreeSlice(mtype reflect.Type, mval reflect.Value) ([]*Tree, error) {
tval := make([]*Tree, mval.Len(), mval.Len())
for i := 0; i < mval.Len(); i++ {
val, err := e.valueToTree(mtype.Elem(), mval.Index(i))
if err != nil {
return nil, err
}
tval[i] = val
}
return tval, nil
} | go | func (e *Encoder) valueToTreeSlice(mtype reflect.Type, mval reflect.Value) ([]*Tree, error) {
tval := make([]*Tree, mval.Len(), mval.Len())
for i := 0; i < mval.Len(); i++ {
val, err := e.valueToTree(mtype.Elem(), mval.Index(i))
if err != nil {
return nil, err
}
tval[i] = val
}
return tval, nil
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"valueToTreeSlice",
"(",
"mtype",
"reflect",
".",
"Type",
",",
"mval",
"reflect",
".",
"Value",
")",
"(",
"[",
"]",
"*",
"Tree",
",",
"error",
")",
"{",
"tval",
":=",
"make",
"(",
"[",
"]",
"*",
"Tree",
",",
"mval",
".",
"Len",
"(",
")",
",",
"mval",
".",
"Len",
"(",
")",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"mval",
".",
"Len",
"(",
")",
";",
"i",
"++",
"{",
"val",
",",
"err",
":=",
"e",
".",
"valueToTree",
"(",
"mtype",
".",
"Elem",
"(",
")",
",",
"mval",
".",
"Index",
"(",
"i",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"tval",
"[",
"i",
"]",
"=",
"val",
"\n",
"}",
"\n",
"return",
"tval",
",",
"nil",
"\n",
"}"
] | // Convert given marshal slice to slice of Toml trees | [
"Convert",
"given",
"marshal",
"slice",
"to",
"slice",
"of",
"Toml",
"trees"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L373-L383 | train |
pelletier/go-toml | marshal.go | valueToOtherSlice | func (e *Encoder) valueToOtherSlice(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
tval := make([]interface{}, mval.Len(), mval.Len())
for i := 0; i < mval.Len(); i++ {
val, err := e.valueToToml(mtype.Elem(), mval.Index(i))
if err != nil {
return nil, err
}
tval[i] = val
}
return tval, nil
} | go | func (e *Encoder) valueToOtherSlice(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
tval := make([]interface{}, mval.Len(), mval.Len())
for i := 0; i < mval.Len(); i++ {
val, err := e.valueToToml(mtype.Elem(), mval.Index(i))
if err != nil {
return nil, err
}
tval[i] = val
}
return tval, nil
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"valueToOtherSlice",
"(",
"mtype",
"reflect",
".",
"Type",
",",
"mval",
"reflect",
".",
"Value",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"tval",
":=",
"make",
"(",
"[",
"]",
"interface",
"{",
"}",
",",
"mval",
".",
"Len",
"(",
")",
",",
"mval",
".",
"Len",
"(",
")",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"mval",
".",
"Len",
"(",
")",
";",
"i",
"++",
"{",
"val",
",",
"err",
":=",
"e",
".",
"valueToToml",
"(",
"mtype",
".",
"Elem",
"(",
")",
",",
"mval",
".",
"Index",
"(",
"i",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"tval",
"[",
"i",
"]",
"=",
"val",
"\n",
"}",
"\n",
"return",
"tval",
",",
"nil",
"\n",
"}"
] | // Convert given marshal slice to slice of toml values | [
"Convert",
"given",
"marshal",
"slice",
"to",
"slice",
"of",
"toml",
"values"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L386-L396 | train |
pelletier/go-toml | marshal.go | valueToToml | func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
e.line++
if mtype.Kind() == reflect.Ptr {
return e.valueToToml(mtype.Elem(), mval.Elem())
}
switch {
case isCustomMarshaler(mtype):
return callCustomMarshaler(mval)
case isTree(mtype):
return e.valueToTree(mtype, mval)
case isTreeSlice(mtype):
return e.valueToTreeSlice(mtype, mval)
case isOtherSlice(mtype):
return e.valueToOtherSlice(mtype, mval)
default:
switch mtype.Kind() {
case reflect.Bool:
return mval.Bool(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if mtype.Kind() == reflect.Int64 && mtype == reflect.TypeOf(time.Duration(1)) {
return fmt.Sprint(mval), nil
}
return mval.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return mval.Uint(), nil
case reflect.Float32, reflect.Float64:
return mval.Float(), nil
case reflect.String:
return mval.String(), nil
case reflect.Struct:
return mval.Interface().(time.Time), nil
default:
return nil, fmt.Errorf("Marshal can't handle %v(%v)", mtype, mtype.Kind())
}
}
} | go | func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
e.line++
if mtype.Kind() == reflect.Ptr {
return e.valueToToml(mtype.Elem(), mval.Elem())
}
switch {
case isCustomMarshaler(mtype):
return callCustomMarshaler(mval)
case isTree(mtype):
return e.valueToTree(mtype, mval)
case isTreeSlice(mtype):
return e.valueToTreeSlice(mtype, mval)
case isOtherSlice(mtype):
return e.valueToOtherSlice(mtype, mval)
default:
switch mtype.Kind() {
case reflect.Bool:
return mval.Bool(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if mtype.Kind() == reflect.Int64 && mtype == reflect.TypeOf(time.Duration(1)) {
return fmt.Sprint(mval), nil
}
return mval.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return mval.Uint(), nil
case reflect.Float32, reflect.Float64:
return mval.Float(), nil
case reflect.String:
return mval.String(), nil
case reflect.Struct:
return mval.Interface().(time.Time), nil
default:
return nil, fmt.Errorf("Marshal can't handle %v(%v)", mtype, mtype.Kind())
}
}
} | [
"func",
"(",
"e",
"*",
"Encoder",
")",
"valueToToml",
"(",
"mtype",
"reflect",
".",
"Type",
",",
"mval",
"reflect",
".",
"Value",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"e",
".",
"line",
"++",
"\n",
"if",
"mtype",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
"{",
"return",
"e",
".",
"valueToToml",
"(",
"mtype",
".",
"Elem",
"(",
")",
",",
"mval",
".",
"Elem",
"(",
")",
")",
"\n",
"}",
"\n",
"switch",
"{",
"case",
"isCustomMarshaler",
"(",
"mtype",
")",
":",
"return",
"callCustomMarshaler",
"(",
"mval",
")",
"\n",
"case",
"isTree",
"(",
"mtype",
")",
":",
"return",
"e",
".",
"valueToTree",
"(",
"mtype",
",",
"mval",
")",
"\n",
"case",
"isTreeSlice",
"(",
"mtype",
")",
":",
"return",
"e",
".",
"valueToTreeSlice",
"(",
"mtype",
",",
"mval",
")",
"\n",
"case",
"isOtherSlice",
"(",
"mtype",
")",
":",
"return",
"e",
".",
"valueToOtherSlice",
"(",
"mtype",
",",
"mval",
")",
"\n",
"default",
":",
"switch",
"mtype",
".",
"Kind",
"(",
")",
"{",
"case",
"reflect",
".",
"Bool",
":",
"return",
"mval",
".",
"Bool",
"(",
")",
",",
"nil",
"\n",
"case",
"reflect",
".",
"Int",
",",
"reflect",
".",
"Int8",
",",
"reflect",
".",
"Int16",
",",
"reflect",
".",
"Int32",
",",
"reflect",
".",
"Int64",
":",
"if",
"mtype",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Int64",
"&&",
"mtype",
"==",
"reflect",
".",
"TypeOf",
"(",
"time",
".",
"Duration",
"(",
"1",
")",
")",
"{",
"return",
"fmt",
".",
"Sprint",
"(",
"mval",
")",
",",
"nil",
"\n",
"}",
"\n",
"return",
"mval",
".",
"Int",
"(",
")",
",",
"nil",
"\n",
"case",
"reflect",
".",
"Uint",
",",
"reflect",
".",
"Uint8",
",",
"reflect",
".",
"Uint16",
",",
"reflect",
".",
"Uint32",
",",
"reflect",
".",
"Uint64",
":",
"return",
"mval",
".",
"Uint",
"(",
")",
",",
"nil",
"\n",
"case",
"reflect",
".",
"Float32",
",",
"reflect",
".",
"Float64",
":",
"return",
"mval",
".",
"Float",
"(",
")",
",",
"nil",
"\n",
"case",
"reflect",
".",
"String",
":",
"return",
"mval",
".",
"String",
"(",
")",
",",
"nil",
"\n",
"case",
"reflect",
".",
"Struct",
":",
"return",
"mval",
".",
"Interface",
"(",
")",
".",
"(",
"time",
".",
"Time",
")",
",",
"nil",
"\n",
"default",
":",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"mtype",
",",
"mtype",
".",
"Kind",
"(",
")",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // Convert given marshal value to toml value | [
"Convert",
"given",
"marshal",
"value",
"to",
"toml",
"value"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L399-L434 | train |
pelletier/go-toml | marshal.go | Unmarshal | func (t *Tree) Unmarshal(v interface{}) error {
d := Decoder{tval: t, tagName: tagFieldName}
return d.unmarshal(v)
} | go | func (t *Tree) Unmarshal(v interface{}) error {
d := Decoder{tval: t, tagName: tagFieldName}
return d.unmarshal(v)
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"Unmarshal",
"(",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"d",
":=",
"Decoder",
"{",
"tval",
":",
"t",
",",
"tagName",
":",
"tagFieldName",
"}",
"\n",
"return",
"d",
".",
"unmarshal",
"(",
"v",
")",
"\n",
"}"
] | // Unmarshal attempts to unmarshal the Tree into a Go struct pointed by v.
// Neither Unmarshaler interfaces nor UnmarshalTOML functions are supported for
// sub-structs, and only definite types can be unmarshaled. | [
"Unmarshal",
"attempts",
"to",
"unmarshal",
"the",
"Tree",
"into",
"a",
"Go",
"struct",
"pointed",
"by",
"v",
".",
"Neither",
"Unmarshaler",
"interfaces",
"nor",
"UnmarshalTOML",
"functions",
"are",
"supported",
"for",
"sub",
"-",
"structs",
"and",
"only",
"definite",
"types",
"can",
"be",
"unmarshaled",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L439-L442 | train |
pelletier/go-toml | marshal.go | Decode | func (d *Decoder) Decode(v interface{}) error {
var err error
d.tval, err = LoadReader(d.r)
if err != nil {
return err
}
return d.unmarshal(v)
} | go | func (d *Decoder) Decode(v interface{}) error {
var err error
d.tval, err = LoadReader(d.r)
if err != nil {
return err
}
return d.unmarshal(v)
} | [
"func",
"(",
"d",
"*",
"Decoder",
")",
"Decode",
"(",
"v",
"interface",
"{",
"}",
")",
"error",
"{",
"var",
"err",
"error",
"\n",
"d",
".",
"tval",
",",
"err",
"=",
"LoadReader",
"(",
"d",
".",
"r",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"return",
"d",
".",
"unmarshal",
"(",
"v",
")",
"\n",
"}"
] | // Decode reads a TOML-encoded value from it's input
// and unmarshals it in the value pointed at by v.
//
// See the documentation for Marshal for details. | [
"Decode",
"reads",
"a",
"TOML",
"-",
"encoded",
"value",
"from",
"it",
"s",
"input",
"and",
"unmarshals",
"it",
"in",
"the",
"value",
"pointed",
"at",
"by",
"v",
".",
"See",
"the",
"documentation",
"for",
"Marshal",
"for",
"details",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L500-L507 | train |
pelletier/go-toml | marshal.go | valueFromOtherSlice | func (d *Decoder) valueFromOtherSlice(mtype reflect.Type, tval []interface{}) (reflect.Value, error) {
mval := reflect.MakeSlice(mtype, len(tval), len(tval))
for i := 0; i < len(tval); i++ {
val, err := d.valueFromToml(mtype.Elem(), tval[i])
if err != nil {
return mval, err
}
mval.Index(i).Set(val)
}
return mval, nil
} | go | func (d *Decoder) valueFromOtherSlice(mtype reflect.Type, tval []interface{}) (reflect.Value, error) {
mval := reflect.MakeSlice(mtype, len(tval), len(tval))
for i := 0; i < len(tval); i++ {
val, err := d.valueFromToml(mtype.Elem(), tval[i])
if err != nil {
return mval, err
}
mval.Index(i).Set(val)
}
return mval, nil
} | [
"func",
"(",
"d",
"*",
"Decoder",
")",
"valueFromOtherSlice",
"(",
"mtype",
"reflect",
".",
"Type",
",",
"tval",
"[",
"]",
"interface",
"{",
"}",
")",
"(",
"reflect",
".",
"Value",
",",
"error",
")",
"{",
"mval",
":=",
"reflect",
".",
"MakeSlice",
"(",
"mtype",
",",
"len",
"(",
"tval",
")",
",",
"len",
"(",
"tval",
")",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"len",
"(",
"tval",
")",
";",
"i",
"++",
"{",
"val",
",",
"err",
":=",
"d",
".",
"valueFromToml",
"(",
"mtype",
".",
"Elem",
"(",
")",
",",
"tval",
"[",
"i",
"]",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"mval",
",",
"err",
"\n",
"}",
"\n",
"mval",
".",
"Index",
"(",
"i",
")",
".",
"Set",
"(",
"val",
")",
"\n",
"}",
"\n",
"return",
"mval",
",",
"nil",
"\n",
"}"
] | // Convert toml value to marshal primitive slice, using marshal type | [
"Convert",
"toml",
"value",
"to",
"marshal",
"primitive",
"slice",
"using",
"marshal",
"type"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/marshal.go#L638-L648 | train |
pelletier/go-toml | lexer.go | read | func (l *tomlLexer) read() rune {
r := l.peek()
if r == '\n' {
l.endbufferLine++
l.endbufferCol = 1
} else {
l.endbufferCol++
}
l.inputIdx++
return r
} | go | func (l *tomlLexer) read() rune {
r := l.peek()
if r == '\n' {
l.endbufferLine++
l.endbufferCol = 1
} else {
l.endbufferCol++
}
l.inputIdx++
return r
} | [
"func",
"(",
"l",
"*",
"tomlLexer",
")",
"read",
"(",
")",
"rune",
"{",
"r",
":=",
"l",
".",
"peek",
"(",
")",
"\n",
"if",
"r",
"==",
"'\\n'",
"{",
"l",
".",
"endbufferLine",
"++",
"\n",
"l",
".",
"endbufferCol",
"=",
"1",
"\n",
"}",
"else",
"{",
"l",
".",
"endbufferCol",
"++",
"\n",
"}",
"\n",
"l",
".",
"inputIdx",
"++",
"\n",
"return",
"r",
"\n",
"}"
] | // Basic read operations on input | [
"Basic",
"read",
"operations",
"on",
"input"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/lexer.go#L38-L48 | train |
pelletier/go-toml | lexer.go | lexKey | func (l *tomlLexer) lexKey() tomlLexStateFn {
growingString := ""
for r := l.peek(); isKeyChar(r) || r == '\n' || r == '\r'; r = l.peek() {
if r == '"' {
l.next()
str, err := l.lexStringAsString(`"`, false, true)
if err != nil {
return l.errorf(err.Error())
}
growingString += "\"" + str + "\""
l.next()
continue
} else if r == '\'' {
l.next()
str, err := l.lexLiteralStringAsString(`'`, false)
if err != nil {
return l.errorf(err.Error())
}
growingString += "'" + str + "'"
l.next()
continue
} else if r == '\n' {
return l.errorf("keys cannot contain new lines")
} else if isSpace(r) {
break
} else if r == '.' {
// skip
} else if !isValidBareChar(r) {
return l.errorf("keys cannot contain %c character", r)
}
growingString += string(r)
l.next()
}
l.emitWithValue(tokenKey, growingString)
return l.lexVoid
} | go | func (l *tomlLexer) lexKey() tomlLexStateFn {
growingString := ""
for r := l.peek(); isKeyChar(r) || r == '\n' || r == '\r'; r = l.peek() {
if r == '"' {
l.next()
str, err := l.lexStringAsString(`"`, false, true)
if err != nil {
return l.errorf(err.Error())
}
growingString += "\"" + str + "\""
l.next()
continue
} else if r == '\'' {
l.next()
str, err := l.lexLiteralStringAsString(`'`, false)
if err != nil {
return l.errorf(err.Error())
}
growingString += "'" + str + "'"
l.next()
continue
} else if r == '\n' {
return l.errorf("keys cannot contain new lines")
} else if isSpace(r) {
break
} else if r == '.' {
// skip
} else if !isValidBareChar(r) {
return l.errorf("keys cannot contain %c character", r)
}
growingString += string(r)
l.next()
}
l.emitWithValue(tokenKey, growingString)
return l.lexVoid
} | [
"func",
"(",
"l",
"*",
"tomlLexer",
")",
"lexKey",
"(",
")",
"tomlLexStateFn",
"{",
"growingString",
":=",
"\"",
"\"",
"\n\n",
"for",
"r",
":=",
"l",
".",
"peek",
"(",
")",
";",
"isKeyChar",
"(",
"r",
")",
"||",
"r",
"==",
"'\\n'",
"||",
"r",
"==",
"'\\r'",
";",
"r",
"=",
"l",
".",
"peek",
"(",
")",
"{",
"if",
"r",
"==",
"'\"'",
"{",
"l",
".",
"next",
"(",
")",
"\n",
"str",
",",
"err",
":=",
"l",
".",
"lexStringAsString",
"(",
"`\"`",
",",
"false",
",",
"true",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"l",
".",
"errorf",
"(",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n",
"growingString",
"+=",
"\"",
"\\\"",
"\"",
"+",
"str",
"+",
"\"",
"\\\"",
"\"",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"continue",
"\n",
"}",
"else",
"if",
"r",
"==",
"'\\''",
"{",
"l",
".",
"next",
"(",
")",
"\n",
"str",
",",
"err",
":=",
"l",
".",
"lexLiteralStringAsString",
"(",
"`'`",
",",
"false",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"l",
".",
"errorf",
"(",
"err",
".",
"Error",
"(",
")",
")",
"\n",
"}",
"\n",
"growingString",
"+=",
"\"",
"\"",
"+",
"str",
"+",
"\"",
"\"",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"continue",
"\n",
"}",
"else",
"if",
"r",
"==",
"'\\n'",
"{",
"return",
"l",
".",
"errorf",
"(",
"\"",
"\"",
")",
"\n",
"}",
"else",
"if",
"isSpace",
"(",
"r",
")",
"{",
"break",
"\n",
"}",
"else",
"if",
"r",
"==",
"'.'",
"{",
"// skip",
"}",
"else",
"if",
"!",
"isValidBareChar",
"(",
"r",
")",
"{",
"return",
"l",
".",
"errorf",
"(",
"\"",
"\"",
",",
"r",
")",
"\n",
"}",
"\n",
"growingString",
"+=",
"string",
"(",
"r",
")",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"}",
"\n",
"l",
".",
"emitWithValue",
"(",
"tokenKey",
",",
"growingString",
")",
"\n",
"return",
"l",
".",
"lexVoid",
"\n",
"}"
] | // Parse the key and emits its value without escape sequences.
// bare keys, basic string keys and literal string keys are supported. | [
"Parse",
"the",
"key",
"and",
"emits",
"its",
"value",
"without",
"escape",
"sequences",
".",
"bare",
"keys",
"basic",
"string",
"keys",
"and",
"literal",
"string",
"keys",
"are",
"supported",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/lexer.go#L302-L338 | train |
pelletier/go-toml | lexer.go | lexStringAsString | func (l *tomlLexer) lexStringAsString(terminator string, discardLeadingNewLine, acceptNewLines bool) (string, error) {
growingString := ""
if discardLeadingNewLine {
if l.follow("\r\n") {
l.skip()
l.skip()
} else if l.peek() == '\n' {
l.skip()
}
}
for {
if l.follow(terminator) {
return growingString, nil
}
if l.follow("\\") {
l.next()
switch l.peek() {
case '\r':
fallthrough
case '\n':
fallthrough
case '\t':
fallthrough
case ' ':
// skip all whitespace chars following backslash
for strings.ContainsRune("\r\n\t ", l.peek()) {
l.next()
}
case '"':
growingString += "\""
l.next()
case 'n':
growingString += "\n"
l.next()
case 'b':
growingString += "\b"
l.next()
case 'f':
growingString += "\f"
l.next()
case '/':
growingString += "/"
l.next()
case 't':
growingString += "\t"
l.next()
case 'r':
growingString += "\r"
l.next()
case '\\':
growingString += "\\"
l.next()
case 'u':
l.next()
code := ""
for i := 0; i < 4; i++ {
c := l.peek()
if !isHexDigit(c) {
return "", errors.New("unfinished unicode escape")
}
l.next()
code = code + string(c)
}
intcode, err := strconv.ParseInt(code, 16, 32)
if err != nil {
return "", errors.New("invalid unicode escape: \\u" + code)
}
growingString += string(rune(intcode))
case 'U':
l.next()
code := ""
for i := 0; i < 8; i++ {
c := l.peek()
if !isHexDigit(c) {
return "", errors.New("unfinished unicode escape")
}
l.next()
code = code + string(c)
}
intcode, err := strconv.ParseInt(code, 16, 64)
if err != nil {
return "", errors.New("invalid unicode escape: \\U" + code)
}
growingString += string(rune(intcode))
default:
return "", errors.New("invalid escape sequence: \\" + string(l.peek()))
}
} else {
r := l.peek()
if 0x00 <= r && r <= 0x1F && !(acceptNewLines && (r == '\n' || r == '\r')) {
return "", fmt.Errorf("unescaped control character %U", r)
}
l.next()
growingString += string(r)
}
if l.peek() == eof {
break
}
}
return "", errors.New("unclosed string")
} | go | func (l *tomlLexer) lexStringAsString(terminator string, discardLeadingNewLine, acceptNewLines bool) (string, error) {
growingString := ""
if discardLeadingNewLine {
if l.follow("\r\n") {
l.skip()
l.skip()
} else if l.peek() == '\n' {
l.skip()
}
}
for {
if l.follow(terminator) {
return growingString, nil
}
if l.follow("\\") {
l.next()
switch l.peek() {
case '\r':
fallthrough
case '\n':
fallthrough
case '\t':
fallthrough
case ' ':
// skip all whitespace chars following backslash
for strings.ContainsRune("\r\n\t ", l.peek()) {
l.next()
}
case '"':
growingString += "\""
l.next()
case 'n':
growingString += "\n"
l.next()
case 'b':
growingString += "\b"
l.next()
case 'f':
growingString += "\f"
l.next()
case '/':
growingString += "/"
l.next()
case 't':
growingString += "\t"
l.next()
case 'r':
growingString += "\r"
l.next()
case '\\':
growingString += "\\"
l.next()
case 'u':
l.next()
code := ""
for i := 0; i < 4; i++ {
c := l.peek()
if !isHexDigit(c) {
return "", errors.New("unfinished unicode escape")
}
l.next()
code = code + string(c)
}
intcode, err := strconv.ParseInt(code, 16, 32)
if err != nil {
return "", errors.New("invalid unicode escape: \\u" + code)
}
growingString += string(rune(intcode))
case 'U':
l.next()
code := ""
for i := 0; i < 8; i++ {
c := l.peek()
if !isHexDigit(c) {
return "", errors.New("unfinished unicode escape")
}
l.next()
code = code + string(c)
}
intcode, err := strconv.ParseInt(code, 16, 64)
if err != nil {
return "", errors.New("invalid unicode escape: \\U" + code)
}
growingString += string(rune(intcode))
default:
return "", errors.New("invalid escape sequence: \\" + string(l.peek()))
}
} else {
r := l.peek()
if 0x00 <= r && r <= 0x1F && !(acceptNewLines && (r == '\n' || r == '\r')) {
return "", fmt.Errorf("unescaped control character %U", r)
}
l.next()
growingString += string(r)
}
if l.peek() == eof {
break
}
}
return "", errors.New("unclosed string")
} | [
"func",
"(",
"l",
"*",
"tomlLexer",
")",
"lexStringAsString",
"(",
"terminator",
"string",
",",
"discardLeadingNewLine",
",",
"acceptNewLines",
"bool",
")",
"(",
"string",
",",
"error",
")",
"{",
"growingString",
":=",
"\"",
"\"",
"\n\n",
"if",
"discardLeadingNewLine",
"{",
"if",
"l",
".",
"follow",
"(",
"\"",
"\\r",
"\\n",
"\"",
")",
"{",
"l",
".",
"skip",
"(",
")",
"\n",
"l",
".",
"skip",
"(",
")",
"\n",
"}",
"else",
"if",
"l",
".",
"peek",
"(",
")",
"==",
"'\\n'",
"{",
"l",
".",
"skip",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"for",
"{",
"if",
"l",
".",
"follow",
"(",
"terminator",
")",
"{",
"return",
"growingString",
",",
"nil",
"\n",
"}",
"\n\n",
"if",
"l",
".",
"follow",
"(",
"\"",
"\\\\",
"\"",
")",
"{",
"l",
".",
"next",
"(",
")",
"\n",
"switch",
"l",
".",
"peek",
"(",
")",
"{",
"case",
"'\\r'",
":",
"fallthrough",
"\n",
"case",
"'\\n'",
":",
"fallthrough",
"\n",
"case",
"'\\t'",
":",
"fallthrough",
"\n",
"case",
"' '",
":",
"// skip all whitespace chars following backslash",
"for",
"strings",
".",
"ContainsRune",
"(",
"\"",
"\\r",
"\\n",
"\\t",
"\"",
",",
"l",
".",
"peek",
"(",
")",
")",
"{",
"l",
".",
"next",
"(",
")",
"\n",
"}",
"\n",
"case",
"'\"'",
":",
"growingString",
"+=",
"\"",
"\\\"",
"\"",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"case",
"'n'",
":",
"growingString",
"+=",
"\"",
"\\n",
"\"",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"case",
"'b'",
":",
"growingString",
"+=",
"\"",
"\\b",
"\"",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"case",
"'f'",
":",
"growingString",
"+=",
"\"",
"\\f",
"\"",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"case",
"'/'",
":",
"growingString",
"+=",
"\"",
"\"",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"case",
"'t'",
":",
"growingString",
"+=",
"\"",
"\\t",
"\"",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"case",
"'r'",
":",
"growingString",
"+=",
"\"",
"\\r",
"\"",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"case",
"'\\\\'",
":",
"growingString",
"+=",
"\"",
"\\\\",
"\"",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"case",
"'u'",
":",
"l",
".",
"next",
"(",
")",
"\n",
"code",
":=",
"\"",
"\"",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"4",
";",
"i",
"++",
"{",
"c",
":=",
"l",
".",
"peek",
"(",
")",
"\n",
"if",
"!",
"isHexDigit",
"(",
"c",
")",
"{",
"return",
"\"",
"\"",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"code",
"=",
"code",
"+",
"string",
"(",
"c",
")",
"\n",
"}",
"\n",
"intcode",
",",
"err",
":=",
"strconv",
".",
"ParseInt",
"(",
"code",
",",
"16",
",",
"32",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"errors",
".",
"New",
"(",
"\"",
"\\\\",
"\"",
"+",
"code",
")",
"\n",
"}",
"\n",
"growingString",
"+=",
"string",
"(",
"rune",
"(",
"intcode",
")",
")",
"\n",
"case",
"'U'",
":",
"l",
".",
"next",
"(",
")",
"\n",
"code",
":=",
"\"",
"\"",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"8",
";",
"i",
"++",
"{",
"c",
":=",
"l",
".",
"peek",
"(",
")",
"\n",
"if",
"!",
"isHexDigit",
"(",
"c",
")",
"{",
"return",
"\"",
"\"",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"code",
"=",
"code",
"+",
"string",
"(",
"c",
")",
"\n",
"}",
"\n",
"intcode",
",",
"err",
":=",
"strconv",
".",
"ParseInt",
"(",
"code",
",",
"16",
",",
"64",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"errors",
".",
"New",
"(",
"\"",
"\\\\",
"\"",
"+",
"code",
")",
"\n",
"}",
"\n",
"growingString",
"+=",
"string",
"(",
"rune",
"(",
"intcode",
")",
")",
"\n",
"default",
":",
"return",
"\"",
"\"",
",",
"errors",
".",
"New",
"(",
"\"",
"\\\\",
"\"",
"+",
"string",
"(",
"l",
".",
"peek",
"(",
")",
")",
")",
"\n",
"}",
"\n",
"}",
"else",
"{",
"r",
":=",
"l",
".",
"peek",
"(",
")",
"\n\n",
"if",
"0x00",
"<=",
"r",
"&&",
"r",
"<=",
"0x1F",
"&&",
"!",
"(",
"acceptNewLines",
"&&",
"(",
"r",
"==",
"'\\n'",
"||",
"r",
"==",
"'\\r'",
")",
")",
"{",
"return",
"\"",
"\"",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"r",
")",
"\n",
"}",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"growingString",
"+=",
"string",
"(",
"r",
")",
"\n",
"}",
"\n\n",
"if",
"l",
".",
"peek",
"(",
")",
"==",
"eof",
"{",
"break",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"\"",
"\"",
",",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}"
] | // Lex a string and return the results as a string.
// Terminator is the substring indicating the end of the token.
// The resulting string does not include the terminator. | [
"Lex",
"a",
"string",
"and",
"return",
"the",
"results",
"as",
"a",
"string",
".",
"Terminator",
"is",
"the",
"substring",
"indicating",
"the",
"end",
"of",
"the",
"token",
".",
"The",
"resulting",
"string",
"does",
"not",
"include",
"the",
"terminator",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/lexer.go#L414-L520 | train |
pelletier/go-toml | lexer.go | lexInsideTableArrayKey | func (l *tomlLexer) lexInsideTableArrayKey() tomlLexStateFn {
for r := l.peek(); r != eof; r = l.peek() {
switch r {
case ']':
if l.currentTokenStop > l.currentTokenStart {
l.emit(tokenKeyGroupArray)
}
l.next()
if l.peek() != ']' {
break
}
l.next()
l.emit(tokenDoubleRightBracket)
return l.lexVoid
case '[':
return l.errorf("table array key cannot contain ']'")
default:
l.next()
}
}
return l.errorf("unclosed table array key")
} | go | func (l *tomlLexer) lexInsideTableArrayKey() tomlLexStateFn {
for r := l.peek(); r != eof; r = l.peek() {
switch r {
case ']':
if l.currentTokenStop > l.currentTokenStart {
l.emit(tokenKeyGroupArray)
}
l.next()
if l.peek() != ']' {
break
}
l.next()
l.emit(tokenDoubleRightBracket)
return l.lexVoid
case '[':
return l.errorf("table array key cannot contain ']'")
default:
l.next()
}
}
return l.errorf("unclosed table array key")
} | [
"func",
"(",
"l",
"*",
"tomlLexer",
")",
"lexInsideTableArrayKey",
"(",
")",
"tomlLexStateFn",
"{",
"for",
"r",
":=",
"l",
".",
"peek",
"(",
")",
";",
"r",
"!=",
"eof",
";",
"r",
"=",
"l",
".",
"peek",
"(",
")",
"{",
"switch",
"r",
"{",
"case",
"']'",
":",
"if",
"l",
".",
"currentTokenStop",
">",
"l",
".",
"currentTokenStart",
"{",
"l",
".",
"emit",
"(",
"tokenKeyGroupArray",
")",
"\n",
"}",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"if",
"l",
".",
"peek",
"(",
")",
"!=",
"']'",
"{",
"break",
"\n",
"}",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"l",
".",
"emit",
"(",
"tokenDoubleRightBracket",
")",
"\n",
"return",
"l",
".",
"lexVoid",
"\n",
"case",
"'['",
":",
"return",
"l",
".",
"errorf",
"(",
"\"",
"\"",
")",
"\n",
"default",
":",
"l",
".",
"next",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"l",
".",
"errorf",
"(",
"\"",
"\"",
")",
"\n",
"}"
] | // Parse the key till "]]", but only bare keys are supported | [
"Parse",
"the",
"key",
"till",
"]]",
"but",
"only",
"bare",
"keys",
"are",
"supported"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/lexer.go#L564-L585 | train |
pelletier/go-toml | lexer.go | lexInsideTableKey | func (l *tomlLexer) lexInsideTableKey() tomlLexStateFn {
for r := l.peek(); r != eof; r = l.peek() {
switch r {
case ']':
if l.currentTokenStop > l.currentTokenStart {
l.emit(tokenKeyGroup)
}
l.next()
l.emit(tokenRightBracket)
return l.lexVoid
case '[':
return l.errorf("table key cannot contain ']'")
default:
l.next()
}
}
return l.errorf("unclosed table key")
} | go | func (l *tomlLexer) lexInsideTableKey() tomlLexStateFn {
for r := l.peek(); r != eof; r = l.peek() {
switch r {
case ']':
if l.currentTokenStop > l.currentTokenStart {
l.emit(tokenKeyGroup)
}
l.next()
l.emit(tokenRightBracket)
return l.lexVoid
case '[':
return l.errorf("table key cannot contain ']'")
default:
l.next()
}
}
return l.errorf("unclosed table key")
} | [
"func",
"(",
"l",
"*",
"tomlLexer",
")",
"lexInsideTableKey",
"(",
")",
"tomlLexStateFn",
"{",
"for",
"r",
":=",
"l",
".",
"peek",
"(",
")",
";",
"r",
"!=",
"eof",
";",
"r",
"=",
"l",
".",
"peek",
"(",
")",
"{",
"switch",
"r",
"{",
"case",
"']'",
":",
"if",
"l",
".",
"currentTokenStop",
">",
"l",
".",
"currentTokenStart",
"{",
"l",
".",
"emit",
"(",
"tokenKeyGroup",
")",
"\n",
"}",
"\n",
"l",
".",
"next",
"(",
")",
"\n",
"l",
".",
"emit",
"(",
"tokenRightBracket",
")",
"\n",
"return",
"l",
".",
"lexVoid",
"\n",
"case",
"'['",
":",
"return",
"l",
".",
"errorf",
"(",
"\"",
"\"",
")",
"\n",
"default",
":",
"l",
".",
"next",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"l",
".",
"errorf",
"(",
"\"",
"\"",
")",
"\n",
"}"
] | // Parse the key till "]" but only bare keys are supported | [
"Parse",
"the",
"key",
"till",
"]",
"but",
"only",
"bare",
"keys",
"are",
"supported"
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/lexer.go#L588-L605 | train |
pelletier/go-toml | toml.go | TreeFromMap | func TreeFromMap(m map[string]interface{}) (*Tree, error) {
result, err := toTree(m)
if err != nil {
return nil, err
}
return result.(*Tree), nil
} | go | func TreeFromMap(m map[string]interface{}) (*Tree, error) {
result, err := toTree(m)
if err != nil {
return nil, err
}
return result.(*Tree), nil
} | [
"func",
"TreeFromMap",
"(",
"m",
"map",
"[",
"string",
"]",
"interface",
"{",
"}",
")",
"(",
"*",
"Tree",
",",
"error",
")",
"{",
"result",
",",
"err",
":=",
"toTree",
"(",
"m",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"return",
"result",
".",
"(",
"*",
"Tree",
")",
",",
"nil",
"\n",
"}"
] | // TreeFromMap initializes a new Tree object using the given map. | [
"TreeFromMap",
"initializes",
"a",
"new",
"Tree",
"object",
"using",
"the",
"given",
"map",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L41-L47 | train |
pelletier/go-toml | toml.go | Has | func (t *Tree) Has(key string) bool {
if key == "" {
return false
}
return t.HasPath(strings.Split(key, "."))
} | go | func (t *Tree) Has(key string) bool {
if key == "" {
return false
}
return t.HasPath(strings.Split(key, "."))
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"Has",
"(",
"key",
"string",
")",
"bool",
"{",
"if",
"key",
"==",
"\"",
"\"",
"{",
"return",
"false",
"\n",
"}",
"\n",
"return",
"t",
".",
"HasPath",
"(",
"strings",
".",
"Split",
"(",
"key",
",",
"\"",
"\"",
")",
")",
"\n",
"}"
] | // Has returns a boolean indicating if the given key exists. | [
"Has",
"returns",
"a",
"boolean",
"indicating",
"if",
"the",
"given",
"key",
"exists",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L55-L60 | train |
pelletier/go-toml | toml.go | HasPath | func (t *Tree) HasPath(keys []string) bool {
return t.GetPath(keys) != nil
} | go | func (t *Tree) HasPath(keys []string) bool {
return t.GetPath(keys) != nil
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"HasPath",
"(",
"keys",
"[",
"]",
"string",
")",
"bool",
"{",
"return",
"t",
".",
"GetPath",
"(",
"keys",
")",
"!=",
"nil",
"\n",
"}"
] | // HasPath returns true if the given path of keys exists, false otherwise. | [
"HasPath",
"returns",
"true",
"if",
"the",
"given",
"path",
"of",
"keys",
"exists",
"false",
"otherwise",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L63-L65 | train |
pelletier/go-toml | toml.go | GetPath | func (t *Tree) GetPath(keys []string) interface{} {
if len(keys) == 0 {
return t
}
subtree := t
for _, intermediateKey := range keys[:len(keys)-1] {
value, exists := subtree.values[intermediateKey]
if !exists {
return nil
}
switch node := value.(type) {
case *Tree:
subtree = node
case []*Tree:
// go to most recent element
if len(node) == 0 {
return nil
}
subtree = node[len(node)-1]
default:
return nil // cannot navigate through other node types
}
}
// branch based on final node type
switch node := subtree.values[keys[len(keys)-1]].(type) {
case *tomlValue:
return node.value
default:
return node
}
} | go | func (t *Tree) GetPath(keys []string) interface{} {
if len(keys) == 0 {
return t
}
subtree := t
for _, intermediateKey := range keys[:len(keys)-1] {
value, exists := subtree.values[intermediateKey]
if !exists {
return nil
}
switch node := value.(type) {
case *Tree:
subtree = node
case []*Tree:
// go to most recent element
if len(node) == 0 {
return nil
}
subtree = node[len(node)-1]
default:
return nil // cannot navigate through other node types
}
}
// branch based on final node type
switch node := subtree.values[keys[len(keys)-1]].(type) {
case *tomlValue:
return node.value
default:
return node
}
} | [
"func",
"(",
"t",
"*",
"Tree",
")",
"GetPath",
"(",
"keys",
"[",
"]",
"string",
")",
"interface",
"{",
"}",
"{",
"if",
"len",
"(",
"keys",
")",
"==",
"0",
"{",
"return",
"t",
"\n",
"}",
"\n",
"subtree",
":=",
"t",
"\n",
"for",
"_",
",",
"intermediateKey",
":=",
"range",
"keys",
"[",
":",
"len",
"(",
"keys",
")",
"-",
"1",
"]",
"{",
"value",
",",
"exists",
":=",
"subtree",
".",
"values",
"[",
"intermediateKey",
"]",
"\n",
"if",
"!",
"exists",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"switch",
"node",
":=",
"value",
".",
"(",
"type",
")",
"{",
"case",
"*",
"Tree",
":",
"subtree",
"=",
"node",
"\n",
"case",
"[",
"]",
"*",
"Tree",
":",
"// go to most recent element",
"if",
"len",
"(",
"node",
")",
"==",
"0",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"subtree",
"=",
"node",
"[",
"len",
"(",
"node",
")",
"-",
"1",
"]",
"\n",
"default",
":",
"return",
"nil",
"// cannot navigate through other node types",
"\n",
"}",
"\n",
"}",
"\n",
"// branch based on final node type",
"switch",
"node",
":=",
"subtree",
".",
"values",
"[",
"keys",
"[",
"len",
"(",
"keys",
")",
"-",
"1",
"]",
"]",
".",
"(",
"type",
")",
"{",
"case",
"*",
"tomlValue",
":",
"return",
"node",
".",
"value",
"\n",
"default",
":",
"return",
"node",
"\n",
"}",
"\n",
"}"
] | // GetPath returns the element in the tree indicated by 'keys'.
// If keys is of length zero, the current tree is returned. | [
"GetPath",
"returns",
"the",
"element",
"in",
"the",
"tree",
"indicated",
"by",
"keys",
".",
"If",
"keys",
"is",
"of",
"length",
"zero",
"the",
"current",
"tree",
"is",
"returned",
"."
] | 728039f679cbcd4f6a54e080d2219a4c4928c546 | https://github.com/pelletier/go-toml/blob/728039f679cbcd4f6a54e080d2219a4c4928c546/toml.go#L92-L122 | train |
Subsets and Splits
SQL Console for semeru/code-text-go
Retrieves a limited set of code samples with their languages, with a specific case adjustment for 'Go' language.