Commit ebac0628 authored by Kirill Smelkov's avatar Kirill Smelkov

go/neo/proto: protogen: Factor-out code to lookup object for <pkg>.<name> into common function

Besides proto.customCodec we will need to lookup more objects.
parent b0c747c4
...@@ -253,10 +253,7 @@ func main() { ...@@ -253,10 +253,7 @@ func main() {
protoPkg = loadPkg("lab.nexedi.com/kirr/neo/go/neo/proto", "proto.go") protoPkg = loadPkg("lab.nexedi.com/kirr/neo/go/neo/proto", "proto.go")
// extract neo.customCodec // extract neo.customCodec
cc := protoPkg.Scope().Lookup("customCodec") cc := xlookup(protoPkg, "customCodec")
if cc == nil {
log.Fatal("cannot find `customCodec`")
}
var ok bool var ok bool
neo_customCodec, ok = cc.Type().Underlying().(*types.Interface) neo_customCodec, ok = cc.Type().Underlying().(*types.Interface)
if !ok { if !ok {
...@@ -1252,6 +1249,16 @@ func generateCodecCode(typespec *ast.TypeSpec, codegen CodeGenerator) string { ...@@ -1252,6 +1249,16 @@ func generateCodecCode(typespec *ast.TypeSpec, codegen CodeGenerator) string {
return codegen.generatedCode() return codegen.generatedCode()
} }
// xlookup looks up <pkg>.<name> object.
// It is fatal error if the object cannot be found.
func xlookup(pkg *types.Package, name string) types.Object {
obj := pkg.Scope().Lookup(name)
if obj == nil {
log.Fatalf("cannot find `%s.%s`", pkg.Name(), name)
}
return obj
}
// isByte returns whether typ represents byte. // isByte returns whether typ represents byte.
func isByte(typ types.Type) bool { func isByte(typ types.Type) bool {
t, ok := typ.(*types.Basic) t, ok := typ.(*types.Basic)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment