Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

type_bytes.go 900B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package config
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/alecthomas/units"
  6. )
  7. type TypeBytes struct {
  8. value units.Base2Bytes
  9. }
  10. func (c *TypeBytes) UnmarshalText(data []byte) error {
  11. if len(data) == 0 {
  12. return nil
  13. }
  14. normalizedData := strings.ToUpper(string(data))
  15. normalizedData = strings.ReplaceAll(normalizedData, "IB", "iB")
  16. value, err := units.ParseBase2Bytes(normalizedData)
  17. if err != nil {
  18. return fmt.Errorf("incorrect bytes value: %w", err)
  19. }
  20. if value < 0 {
  21. return fmt.Errorf("%d should be positive number", value)
  22. }
  23. c.value = value
  24. return nil
  25. }
  26. func (c TypeBytes) MarshalText() ([]byte, error) {
  27. return []byte(c.String()), nil
  28. }
  29. func (c TypeBytes) String() string {
  30. if c.value == 0 {
  31. return ""
  32. }
  33. return strings.ToLower(c.value.String())
  34. }
  35. func (c TypeBytes) Value(defaultValue uint) uint {
  36. if c.value == 0 {
  37. return defaultValue
  38. }
  39. return uint(c.value)
  40. }