Skip to content

Commit

Permalink
syntax: clearer error for trivial empty string case
Browse files Browse the repository at this point in the history
  • Loading branch information
bnewbold committed Sep 25, 2024
1 parent 01a47d4 commit 1224531
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions atproto/syntax/atidentifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type AtIdentifier struct {
}

func ParseAtIdentifier(raw string) (*AtIdentifier, error) {
if raw == "" {
return nil, fmt.Errorf("expected AT account identifier, got empty string")
}
if strings.HasPrefix(raw, "did:") {
did, err := ParseDID(raw)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions atproto/syntax/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type CID string
var cidRegex = regexp.MustCompile(`^[a-zA-Z0-9+=]{8,256}$`)

func ParseCID(raw string) (CID, error) {
if raw == "" {
return "", fmt.Errorf("expected CID, got empty string")
}
if len(raw) > 256 {
return "", fmt.Errorf("CID is too long (256 chars max)")
}
Expand Down
3 changes: 3 additions & 0 deletions atproto/syntax/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type Datetime string
var datetimeRegex = regexp.MustCompile(`^[0-9]{4}-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-6][0-9]:[0-6][0-9](.[0-9]{1,20})?(Z|([+-][0-2][0-9]:[0-5][0-9]))$`)

func ParseDatetime(raw string) (Datetime, error) {
if raw == "" {
return "", fmt.Errorf("expected datetime, got empty string")
}
if len(raw) > 64 {
return "", fmt.Errorf("Datetime too long (max 64 chars)")
}
Expand Down
3 changes: 3 additions & 0 deletions atproto/syntax/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type DID string
var didRegex = regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`)

func ParseDID(raw string) (DID, error) {
if raw == "" {
return "", fmt.Errorf("expected DID, got empty string")
}
if len(raw) > 2*1024 {
return "", fmt.Errorf("DID is too long (2048 chars max)")
}
Expand Down
3 changes: 3 additions & 0 deletions atproto/syntax/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var (
type Handle string

func ParseHandle(raw string) (Handle, error) {
if raw == "" {
return "", fmt.Errorf("expected handle, got empty string")
}
if len(raw) > 253 {
return "", fmt.Errorf("handle is too long (253 chars max)")
}
Expand Down
3 changes: 3 additions & 0 deletions atproto/syntax/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type Language string
var langRegex = regexp.MustCompile(`^(i|[a-z]{2,3})(-[a-zA-Z0-9]+)*$`)

func ParseLanguage(raw string) (Language, error) {
if raw == "" {
return "", fmt.Errorf("expected language code, got empty string")
}
if len(raw) > 128 {
return "", fmt.Errorf("Language is too long (128 chars max)")
}
Expand Down
3 changes: 3 additions & 0 deletions atproto/syntax/nsid.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var nsidRegex = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.
type NSID string

func ParseNSID(raw string) (NSID, error) {
if raw == "" {
return "", fmt.Errorf("expected NSID, got empty string")
}
if len(raw) > 317 {
return "", fmt.Errorf("NSID is too long (317 chars max)")
}
Expand Down
3 changes: 3 additions & 0 deletions atproto/syntax/recordkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ var recordKeyRegex = regexp.MustCompile(`^[a-zA-Z0-9_~.:-]{1,512}$`)
type RecordKey string

func ParseRecordKey(raw string) (RecordKey, error) {
if raw == "" {
return "", fmt.Errorf("expected record key, got empty string")
}
if len(raw) > 512 {
return "", fmt.Errorf("recordkey is too long (512 chars max)")
}
Expand Down
3 changes: 3 additions & 0 deletions atproto/syntax/tid.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type TID string
var tidRegex = regexp.MustCompile(`^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$`)

func ParseTID(raw string) (TID, error) {
if raw == "" {
return "", fmt.Errorf("expected TID, got empty string")
}
if len(raw) != 13 {
return "", fmt.Errorf("TID is wrong length (expected 13 chars)")
}
Expand Down
3 changes: 3 additions & 0 deletions atproto/syntax/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
type URI string

func ParseURI(raw string) (URI, error) {
if raw == "" {
return "", fmt.Errorf("expected URI, got empty string")
}
if len(raw) > 8192 {
return "", fmt.Errorf("URI is too long (8192 chars max)")
}
Expand Down

0 comments on commit 1224531

Please # to comment.