Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular lexgen --> cbor_gen dependency #931

Open
iameli opened this issue Feb 5, 2025 · 1 comment
Open

Circular lexgen --> cbor_gen dependency #931

iameli opened this issue Feb 5, 2025 · 1 comment

Comments

@iameli
Copy link

iameli commented Feb 5, 2025

Let's clear out some generated files and regenerate them from the lexicons!

rm -rf api/bsky
make lexgen

[ ... lots of output snipped ... ]

make cbor_gen
go run ./gen
# github.com/bluesky-social/indigo/api/bsky
api/bsky/actorprofile.go:19:46: cannot use &ActorProfile{} (value of type *ActorProfile) as typegen.CBORMarshaler value in argument to util.RegisterType: *ActorProfile does not implement typegen.CBORMarshaler (missing method MarshalCBOR)
api/bsky/embedexternal.go:12:52: cannot use &EmbedExternal{} (value of type *EmbedExternal) as typegen.CBORMarshaler value in argument to util.RegisterType: *EmbedExternal does not implement typegen.CBORMarshaler (missing method MarshalCBOR)
api/bsky/embedimages.go:12:50: cannot use &EmbedImages{} (value of type *EmbedImages) as typegen.CBORMarshaler value in argument to util.RegisterType: *EmbedImages does not implement typegen.CBORMarshaler (missing method MarshalCBOR)
api/bsky/embedrecord.go:16:50: cannot use &EmbedRecord{} (value of type *EmbedRecord) as typegen.CBORMarshaler value in argument to util.RegisterType: *EmbedRecord does not implement typegen.CBORMarshaler (missing method MarshalCBOR)
api/bsky/embedrecordWithMedia.go:18:59: cannot use &EmbedRecordWithMedia{} (value of type *EmbedRecordWithMedia) as typegen.CBORMarshaler value in argument to util.RegisterType: *EmbedRecordWithMedia does not implement typegen.CBORMarshaler (missing method MarshalCBOR)
api/bsky/embedrecordWithMedia.go:77:24: t.EmbedImages.MarshalCBOR undefined (type *EmbedImages has no field or method MarshalCBOR)
api/bsky/embedrecordWithMedia.go:80:23: t.EmbedVideo.MarshalCBOR undefined (type *EmbedVideo has no field or method MarshalCBOR)
api/bsky/embedrecordWithMedia.go:83:26: t.EmbedExternal.MarshalCBOR undefined (type *EmbedExternal has no field or method MarshalCBOR)
api/bsky/embedrecordWithMedia.go:96:24: t.EmbedImages.UnmarshalCBOR undefined (type *EmbedImages has no field or method UnmarshalCBOR)
api/bsky/embedrecordWithMedia.go:99:23: t.EmbedVideo.UnmarshalCBOR undefined (type *EmbedVideo has no field or method UnmarshalCBOR)
api/bsky/embedrecordWithMedia.go:99:23: too many errors
make: *** [Makefile:74: cborgen] Error 1

Apparently cborgen needs UnmarshalCBOR and MarshalCBOR to already be implemented in order to generate those functions. Hm.

This isn't a huge deal in the repo itself (though I'm curious how you get around it!) but it kind of stops lexgen from being useful to those of us that would like to generate Go types of our own Lexicons.

@iameli
Copy link
Author

iameli commented Feb 5, 2025

Don't worry, I got Go code generation going with the following simple, elegant code:

.PHONY: go-lexicons
go-lexicons:
       rm -rf ./pkg/streamplace \
       && mkdir -p ./pkg/streamplace \
       && $(MAKE) lexgen \
       && sed -i.bak 's/\tutil/\/\/\tutil/' $$(find ./pkg/streamplace -type f) \
       && sed -i.bak -E 's/^(.+)github(.+)//' $$(find ./pkg/streamplace -type f) \
       && go run ./pkg/gen/gen.go \
       && $(MAKE) lexgen \
       && rm -rf ./pkg/streamplace/*.bak

.PHONY: lexgen
lexgen:
       go run github.com/bluesky-social/indigo/cmd/lexgen --package streamplace \
               --types-import place.stream:stream.place/streamplace/pkg/streamplace --outdir ./pkg/streamplace --prefix place.stream ->
               '[{"package": "streamplace","prefix": "place.stream","outdir": "./pkg/streamplace","import":"stream.place/streamplace"}>

gen.go:

package main

import (
    "reflect"

    "github.com/bluesky-social/indigo/mst"
    "stream.place/streamplace/pkg/streamplace"

    cbg "github.com/whyrusleeping/cbor-gen"
)

func main() {
    var typVals []any
    for _, typ := range mst.CBORTypes() {
        typVals = append(typVals, reflect.New(typ).Elem().Interface())
    }

    genCfg := cbg.Gen{
        MaxStringLength: 1_000_000,
    }

    if err := genCfg.WriteMapEncodersToFile("pkg/streamplace/cbor_gen.go", "streamplace",
        streamplace.Key{}, streamplace.Livestream{},
    ); err != nil {
        panic(err)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant