]> Cypherpunks repositories - keks.git/commitdiff
More docstrings
authorSergey Matveev <stargrave@stargrave.org>
Thu, 12 Dec 2024 09:55:15 +0000 (12:55 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 12 Dec 2024 10:14:59 +0000 (13:14 +0300)
14 files changed:
gyac/atom/dec.go
gyac/atom/enc.go
gyac/atom/raw.go
gyac/blob.go
gyac/cmd/test-vector-anys/main.go
gyac/cmd/test-vector-manual/main.go
gyac/dec.go
gyac/doc.go [new file with mode: 0644]
gyac/enc.go
gyac/fromgo.go
gyac/mapstruct/dec.go
gyac/mapstruct/map.go
gyac/sort.go
gyac/togo.go

index 6fccb3f6d32831cc937e764a19ceeb1f4a87a11e76a2dcaff0a5c7bc486ce3dc..8f36ad2b692270bff2dd13eaf2a2969ea91627e9f7daee9aeb7cfcc94ba4b3c4 100644 (file)
@@ -37,6 +37,8 @@ var (
        ErrBadInt        = errors.New("bad int value")
 )
 
+// Decode a single YAC-encoded atom. Atom means that it does not decode
+// full lists, maps, blobs and may return types.EOC.
 func Decode(buf []byte) (t types.Type, v any, off int, err error) {
        off = 1
        if len(buf) < 1 {
index 39c4af01ac08561e794effe943f5627f6cde004e45d4a178d41cbeacd99b218d..a6b757bc034d827d00768a8310267dc754316a650a7ec01c9b62a4ba75553ad8 100644 (file)
@@ -52,14 +52,17 @@ const (
        IsUTF8  = 0x40
 )
 
+// Append an encoded EOC atom to the buf.
 func EOCEncode(buf []byte) []byte {
        return append(buf, byte(EOC))
 }
 
+// Append an encoded NIL atom to the buf.
 func NILEncode(buf []byte) []byte {
        return append(buf, byte(NIL))
 }
 
+// Append an encoded TRUE/FALSE atom to the buf.
 func BoolEncode(buf []byte, v bool) []byte {
        if v {
                return append(buf, byte(True))
@@ -67,6 +70,7 @@ func BoolEncode(buf []byte, v bool) []byte {
        return append(buf, byte(False))
 }
 
+// Append an encoded UUID atom to the buf.
 func UUIDEncode(buf []byte, v uuid.UUID) []byte {
        return append(append(buf, byte(UUID)), v[:]...)
 }
@@ -86,10 +90,13 @@ func atomUintEncode(v uint64) (buf []byte) {
        return BinEncode(nil, buf)
 }
 
+// Append an encoded +INT atom to the buf.
 func UIntEncode(buf []byte, v uint64) []byte {
        return append(buf, append([]byte{byte(PInt)}, atomUintEncode(v)...)...)
 }
 
+// Append an encoded -INT atom to the buf if v is negative.
+// +INT atom otherwise, same as UIntEncode.
 func IntEncode(buf []byte, v int64) []byte {
        if v >= 0 {
                return UIntEncode(buf, uint64(v))
@@ -98,8 +105,8 @@ func IntEncode(buf []byte, v int64) []byte {
                atomUintEncode(uint64(-(v+1)))...)...)
 }
 
+// Append an encoded ±INT atom to the buf.
 func BigIntEncode(buf []byte, v *big.Int) []byte {
-       // TODO: fallback to U?IntEncode for small values
        if v.Cmp(bigIntZero) >= 0 {
                return append(buf, BinEncode([]byte{byte(PInt)}, v.Bytes())...)
        }
@@ -109,14 +116,21 @@ func BigIntEncode(buf []byte, v *big.Int) []byte {
        return append(buf, BinEncode([]byte{byte(NInt)}, v.Bytes())...)
 }
 
+// Append an encoded LIST atom to the buf.
+// You have to manually terminate it with EOCEncode.
 func ListEncode(buf []byte) []byte {
        return append(buf, byte(List))
 }
 
+// Append an encoded MAP atom to the buf.
+// You have to manually terminate it with EOCEncode.
 func MapEncode(buf []byte) []byte {
        return append(buf, byte(Map))
 }
 
+// Append an encoded BLOB atom to the buf.
+// You have to manually provide necessary chunks and
+// properly terminate it with BinEncode.
 func BlobEncode(buf []byte, chunkLen int) []byte {
        l := make([]byte, 9)
        l[0] = byte(Blob)
@@ -148,18 +162,23 @@ func atomStrEncode(buf, data []byte, utf8 bool) []byte {
        return append(append(append(buf, b), l...), data...)
 }
 
+// Append an encoded STR atom to the buf.
 func StrEncode(buf []byte, str string) []byte {
        return atomStrEncode(buf, []byte(str), true)
 }
 
+// Append an encoded BIN atom to the buf.
 func BinEncode(buf, bin []byte) []byte {
        return atomStrEncode(buf, bin, false)
 }
 
+// Append an encoded CHUNK atom to the buf.
+// That is basically an appended NIL with the chunk value.
 func ChunkEncode(buf, chunk []byte) []byte {
        return append(append(buf, byte(NIL)), chunk...)
 }
 
+// Append an encoded TAI64* atom to the buf.
 func TAI64Encode(buf, tai []byte) []byte {
        switch len(tai) {
        case 8:
@@ -173,6 +192,7 @@ func TAI64Encode(buf, tai []byte) []byte {
        }
 }
 
+// Append an encoded raw atom's value to the buf.
 func RawEncode(buf []byte, raw Raw) []byte {
        return append(append(buf, byte(raw.T)), raw.V...)
 }
index ddc30d8581bb72bdc0f0d7516fec9afd3013d166061674afc72f4a437abaa957..337bea56fe50b6a06f849d7a518c0d0d1658d066e73d068c5a0a47b63d7144ec 100644 (file)
@@ -1,3 +1,18 @@
+// gyac -- Go YAC encoder implementation
+// Copyright (C) 2024-2025 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
 package atom
 
 import (
@@ -5,6 +20,8 @@ import (
        "fmt"
 )
 
+// Raw atom storage, keeping the tag T and contents V after it.
+// Used for keeping data that can not be represented in native Go types.
 type Raw struct {
        V []byte
        T Type
index 1566e21dd8ed73e684ef846bfdba95ab11ad53e54d051240296bdd75aacbf5b3..23e0d38259b3abea4d26d45ab636c74f3ecdacc9e01e4538432ecf77dce2d349 100644 (file)
@@ -17,6 +17,7 @@ package gyac
 
 import "fmt"
 
+// BLOB object, that keeps data splitted on ChunkLen chunks.
 type Blob struct {
        Chunks   [][]byte
        ChunkLen int
index ae4bad7a02a03451abb4f1aa74db87cb0e396fe05a9c67a49d8b55718ef392f9..c1713d851972d17cece72c28f6d7eee3f6086d65f83916167e11fbcd31ad5c72 100644 (file)
@@ -8,6 +8,7 @@ import (
        "time"
 
        "github.com/google/uuid"
+
        "go.cypherpunks.su/yac/gyac"
        "go.cypherpunks.su/yac/gyac/atom"
 )
index c34e17e2258c19a2f6c64123252fa1ac163b96e1572d97a570f0e1e03ad85958..c070c266da0baa50291649bc45164588b4c5a01004c344bffeb0ecdb487f3cdf 100644 (file)
@@ -9,6 +9,7 @@ import (
 
        "github.com/google/uuid"
        "go.cypherpunks.su/tai64n/v4"
+
        "go.cypherpunks.su/yac/gyac/atom"
 )
 
index a3561d8c3ed5e8acd04d2a925ef9fa9f2a504a18afddc0cd91dbdae668b86f72..de994d67049c3c9487476d278d962367a11067f39cc4c8a4d1e5c97c89c6b6ef 100644 (file)
@@ -32,6 +32,23 @@ var (
        ErrUnexpectedEOC = errors.New("unexpected EOC")
 )
 
+// Item is the base object, holding the type and corresponding value.
+// Depending on the type, you have to type assert the value.
+//   - EOC, NIL holds nothing
+//   - Bool holds bool
+//   - UUID holds uuid.UUID
+//   - UInt holds uint64
+//   - Int holds negative int64
+//   - BigInt holds *big.Int
+//   - List holds []Item
+//   - Map holds map[string]Item
+//   - Blob holds Blob
+//   - Float (currently) holds atom.Raw
+//   - TAI64 holds []byte with with the TAI64 in external format.
+//     Look at its length to determine exact value
+//   - Bin holds []byte
+//   - Str holds string
+//   - Raw holds the atom.Raw
 type Item struct {
        V any
        T types.Type
@@ -170,6 +187,7 @@ func decode(
        return
 }
 
-func Decode(buf []byte) (item *Item, tail []byte, err error) {
+// Decode single YAC-encoded data item. Remaining data will be kept in tail.
+func Decode(buf []byte) (item Item, tail []byte, err error) {
        return decode(buf, true, false, 0)
 }
diff --git a/gyac/doc.go b/gyac/doc.go
new file mode 100644 (file)
index 0000000..67ebca6
--- /dev/null
@@ -0,0 +1,6 @@
+// YAC (http://www.yac.cypherpunks.su) is yet another binary
+// serialisation encoding format. It is aimed to be lightweight in terms
+// of CPU, memory, storage and codec implementation size usage. YAC is
+// deterministic and streamable. It supports wide range of data types,
+// making it able to transparently replace JSON.
+package gyac
index 92de56a2b49457e79beace83a112f0ec7a58130f11b448f09e1a0d7e6dd0f931..c9c912d5af6983572578dc6871b27fd103831ff4d030e029566f4f8fa87c92bc 100644 (file)
@@ -25,6 +25,7 @@ import (
        "go.cypherpunks.su/yac/gyac/types"
 )
 
+// Encode an item appending to the buf.
 func (item Item) Encode(buf []byte) []byte {
        switch item.T {
        case types.NIL:
index 5ea9bae3e774e145b25986b1f9fb731eee634e97cf70129c1b478e6e51017038..f5f61af13b86cb0a61a136a733f4a4b53ce6fa86873f80c6619488f67d1220e3 100644 (file)
@@ -44,6 +44,23 @@ func structTagRead(f reflect.StructField) (name string, omit bool) {
        return
 }
 
+// Create an Item from native Go type, for its future encoding.
+// Allowable types:
+//   - [*]atom.Raw
+//   - [*]Blob
+//   - *big.Int
+//   - []any
+//   - []byte -- will be converted to binary string
+//   - bool
+//   - int, int8, int16, int32, int64
+//   - map[string]any
+//   - nil
+//   - string
+//   - struct -- will be interpreted as map[string]any
+//   - time.Time -- will be converted either to
+//     TAI64 (if nanoseconds=0), or TAI64N
+//   - uint, uint8, uint16, uint32, uint64
+//   - uuid.UUID
 func FromGo(v any) Item {
        if v == nil {
                return Item{T: types.NIL}
index bcba9ffcd96fd75ad3a91a51c6074231056b6597807c5d8f5f7f84fb0537adb0..5d214f9690a189c83e277fbe46cfc81dcf93b2c6ca7184ed7d69f0c3651f3945 100644 (file)
@@ -22,6 +22,8 @@ import (
        "go.cypherpunks.su/yac/gyac/types"
 )
 
+// Decode YAC-encoded data to the dst structure.
+// It will return an error if decoded data is not map.
 func Decode(dst any, raw []byte) (tail []byte, err error) {
        var item gyac.Item
        item, tail, err = gyac.Decode(raw)
index bc3b3a205cd7714c512ad6e5d1e8649d601987b47d0052d045bb6b4834c312a6..ffcd49314b86cb3bb5d830d2bb477613b872eea57385a21a4ba9605de6685bc9 100644 (file)
@@ -19,6 +19,7 @@ import (
        "github.com/mitchellh/mapstructure"
 )
 
+// Fill up dst structure with the contents taken from the src map.
 func FromMap(dst any, src map[string]any) error {
        decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
                Result: dst, TagName: "yac",
index 4259644b8f9afa15b7494703b005b1215cf618e34dff82a25cb9468bcb2170ae..66f751fca0a35bb1b7f936a0a77c7e42ce7834e2cc18434a0896f3e891b97570 100644 (file)
@@ -1,5 +1,6 @@
 package gyac
 
+// Bitewise sorting by length first.
 type ByLenFirst []string
 
 func (a ByLenFirst) Len() int {
index 4dc62f9cb0fbf5297d26bd4c9df83c19b6ee4a7cc7f920664f38b6980f6707ca..f0782afa12f67b7c9435611e51ad7193c7ec42293f6f869c5bd54919b0998fda 100644 (file)
@@ -25,6 +25,8 @@ import (
        "go.cypherpunks.su/yac/gyac/types"
 )
 
+// Convert an item to various native Go types, atom.Raw, Blob, uuid.UUID.
+// Pay attention that f TAI equals to leap second, then it is converted to Raw.
 func (item Item) ToGo() any {
        switch item.T {
        case types.NIL: