Skip to content

Commit

Permalink
Refactor: rename consts to follow Go idiom. Fix const comment style.
Browse files Browse the repository at this point in the history
  • Loading branch information
keuin committed Jul 1, 2023
1 parent 8f07f6f commit b8d0d0c
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 32 deletions.
4 changes: 2 additions & 2 deletions bilibili/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
)

const kInitReadBytes = 4096 // 4KiB
const InitReadBytes = 4096 // 4KiB

// CopyLiveStream read data from a livestream video stream, copy them to a writer.
func (b Bilibili) CopyLiveStream(
Expand Down Expand Up @@ -58,7 +58,7 @@ func (b Bilibili) CopyLiveStream(
// read some first bytes to ensure that the live is really started,
// so we don't create blank files if the live room is open
// but the live hasn't started yet
initBytes := make([]byte, kInitReadBytes)
initBytes := make([]byte, InitReadBytes)
_, err = io.ReadFull(resp.Body, initBytes)
if err != nil {
b.logger.Error("Failed to read stream initial bytes: %v", err)
Expand Down
8 changes: 4 additions & 4 deletions danmaku/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"nhooyr.io/websocket"
)

// Bilibili uses only binary WebSocket messages
const kBilibiliWebSocketMessageType = websocket.MessageBinary
// BilibiliWebSocketMessageType Bilibili uses only binary WebSocket messages
const BilibiliWebSocketMessageType = websocket.MessageBinary

type DanmakuClient struct {
ws *websocket.Conn
Expand All @@ -34,15 +34,15 @@ type wsDatagramIO struct {
}

func (w *wsDatagramIO) Consume(data []byte) error {
return w.ws.Write(w.ctx, kBilibiliWebSocketMessageType, data)
return w.ws.Write(w.ctx, BilibiliWebSocketMessageType, data)
}

func (w *wsDatagramIO) Get() (data []byte, err error) {
typ, data, err := w.ws.Read(w.ctx)
if err != nil {
return
}
if typ != kBilibiliWebSocketMessageType {
if typ != BilibiliWebSocketMessageType {
err = fmt.Errorf("invalid message type: expected a binary WebSocket message, however got %v", typ.String())
}
return
Expand Down
8 changes: 4 additions & 4 deletions danmaku/dmmsg/danmu.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func (dm DanMuMessage) String() string {
dm.SourceUser.Nickname, dm.SourceUser.UID, dm.Content)
}

const kInvalidDanmakuJson = "invalid danmaku JSON document"
const InvalidDanmakuJson = "invalid danmaku JSON document"

func ParseDanmakuMessage(body RawDanMuMessage) (dmm DanMuMessage, err error) {
if len(body.Info) != 16 {
err = fmt.Errorf("%s: \"info\" length != 16", kInvalidDanmakuJson)
err = fmt.Errorf("%s: \"info\" length != 16", InvalidDanmakuJson)
return
}

Expand All @@ -41,14 +41,14 @@ func ParseDanmakuMessage(body RawDanMuMessage) (dmm DanMuMessage, err error) {
var ok bool
uid, ok := userInfo[0].(float64)
if !ok {
err = fmt.Errorf("%s: uid is not a float64: %v", kInvalidDanmakuJson, userInfo[0])
err = fmt.Errorf("%s: uid is not a float64: %v", InvalidDanmakuJson, userInfo[0])
return
}
dmm.SourceUser.UID = int64(uid)

dmm.SourceUser.Nickname, ok = userInfo[1].(string)
if !ok {
err = fmt.Errorf("%s: nickname is not a string: %v", kInvalidDanmakuJson, userInfo[1])
err = fmt.Errorf("%s: nickname is not a string: %v", InvalidDanmakuJson, userInfo[1])
return
}
return
Expand Down
2 changes: 1 addition & 1 deletion danmaku/dmmsg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func castValue[T any](obj interface{}) (thing T, err error) {
casted, ok := (obj).(T)
if !ok {
err = fmt.Errorf("%s: required value is not of type \"%v\": %v",
kInvalidDanmakuJson, reflect.TypeOf(thing).String(), obj)
InvalidDanmakuJson, reflect.TypeOf(thing).String(), obj)
return
}
thing = casted
Expand Down
6 changes: 3 additions & 3 deletions danmaku/dmpkg/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ type authInfo struct {
// NewAuth creates a new authentication exchange.
func NewAuth(protocol ProtocolVer, roomId common.RoomId, authKey string) (exc DanmakuExchange) {
exc, _ = NewPlainExchange(OpConnect, authInfo{
UID: kUidGuest,
UID: UidGuest,
RoomId: uint64(roomId),
ProtoVer: int(protocol),
Platform: kPlatformWeb,
Type: kAuthTypeDefault,
Platform: PlatformWeb,
Type: AuthTypeDefault,
Key: authKey,
})
return
Expand Down
10 changes: 5 additions & 5 deletions danmaku/dmpkg/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import (
)

func DecodeExchange(data []byte) (exc DanmakuExchange, err error) {
if ln := len(data); ln < kHeaderLength {
err = fmt.Errorf("incomplete datagram: length = %v < %v", ln, kHeaderLength)
if ln := len(data); ln < HeaderLength {
err = fmt.Errorf("incomplete datagram: length = %v < %v", ln, HeaderLength)
return
}

// unpack header
var exchangeHeader DanmakuExchangeHeader
err = struc.Unpack(bytes.NewReader(data[:kHeaderLength]), &exchangeHeader)
err = struc.Unpack(bytes.NewReader(data[:HeaderLength]), &exchangeHeader)
if err != nil {
err = fmt.Errorf("cannot unpack exchange header: %w", err)
return
}
headerLength := exchangeHeader.HeaderLength

// validate header length, fail fast if not match
if headerLength != kHeaderLength {
if headerLength != HeaderLength {
err = fmt.Errorf("invalid header length, "+
"the protocol implementation might be obsolete: %v != %v", headerLength, kHeaderLength)
"the protocol implementation might be obsolete: %v != %v", headerLength, HeaderLength)
return
}

Expand Down
15 changes: 10 additions & 5 deletions danmaku/dmpkg/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ func (e *DanmakuExchange) String() string {
e.Length, e.ProtocolVer, e.Operation, e.Body)
}

const kHeaderLength = 16
const kSequenceId = 1
const (
HeaderLength = 16
SequenceId = 1
)

type ProtocolVer uint16

Expand All @@ -47,9 +49,12 @@ const (
ProtoBrotli ProtocolVer = 3
)

const kUidGuest = 0
const kPlatformWeb = "web"
const kAuthTypeDefault = 2 // magic number, not sure what does it mean
const (
UidGuest = 0
PlatformWeb = "web"
// AuthTypeDefault magic number, not sure what does it mean
AuthTypeDefault = 2
)

func (e *DanmakuExchange) Marshal() (data []byte, err error) {
var buffer bytes.Buffer
Expand Down
12 changes: 6 additions & 6 deletions danmaku/dmpkg/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"math"
)

const kMaxBodyLength = math.MaxUint32 - uint64(kHeaderLength)
const MaxBodyLength = math.MaxUint32 - uint64(HeaderLength)

// NewPlainExchange creates a new exchange with raw body specified.
// body: a struct or a raw string
Expand All @@ -28,18 +28,18 @@ func NewPlainExchange(operation Operation, body interface{}) (exc DanmakuExchang
}
}

length := uint64(kHeaderLength + len(bodyData))
if length > kMaxBodyLength {
err = fmt.Errorf("body is too large (> %d)", kMaxBodyLength)
length := uint64(HeaderLength + len(bodyData))
if length > MaxBodyLength {
err = fmt.Errorf("body is too large (> %d)", MaxBodyLength)
return
}
exc = DanmakuExchange{
DanmakuExchangeHeader: DanmakuExchangeHeader{
Length: uint32(length),
HeaderLength: kHeaderLength,
HeaderLength: HeaderLength,
ProtocolVer: ProtoPlainJson,
Operation: operation,
SequenceId: kSequenceId,
SequenceId: SequenceId,
},
Body: bodyData,
}
Expand Down
4 changes: 2 additions & 2 deletions recording/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type TaskResult struct {
Error error
}

const kSpecialExtName = "partial"
const SpecialExtName = "partial"

var errLiveEnded = common.NewRecoverableTaskError("live is ended", nil)

Expand Down Expand Up @@ -281,7 +281,7 @@ func record(
originalExtName := mo.TupleToResult(myurl.Url(streamSource.URL).FileExtension()).OrElse("flv")

if task.Download.UseSpecialExtNameBeforeFinishing {
extName = kSpecialExtName
extName = SpecialExtName
} else {
extName = originalExtName
}
Expand Down

0 comments on commit b8d0d0c

Please sign in to comment.