importing proto from different files - protocol-buffers

I maintain all my proto files in one folder. An overview:
protos
|
|__auth
|
|__some_other
Inside auth directory I have auth_service.proto user.proto etc.
But I am having issues with importing definitions form user.proto
In auth_service.proto:
syntax="proto3";
package auth;
import "auth/user.proto"; // Import "auth/user.proto" was not found or had errors.
message SomeMessage {
User user = 1; // user is not defined
}
user.proto has the same package name:
syntax="proto3";
package auth;
message User{}
Protoc version: libprotoc : 3.19.4

This can be tricky.
One way to think about it is that you need to determine the root of the import path and then proto imports are relative to it.
Assuming ${PWD} is the parent of protos.
Example #1:
If you use ${PWD} as the proto_path:
protoc \
--proto_path=${PWD} \
--go_out=${PWD} \
${PWD}/protos/auth/auth_service.proto \
${PWD}/protos/auth/user.proto
Then the import should be import "protos/auth/user.proto";
Example #2:
If you use protos as the proto_path:
protoc \
--proto_path=${PWD}/protos \
--go_out=${PWD} \
${PWD}/protos/auth/auth_service.proto \
${PWD}/protos/auth/user.proto
Then the import should be import "auth/user.proto";.
NOTE In all cases, the proto file references must be prefixed by (one of) a proto_path.
settings.json:
"protoc": {
"path": "${workspaceRoot}/protoc-21.2-linux-x86_64/bin/protoc",
"compile_on_save": true,
"options": [
"--proto_path=${workspaceRoot}/protoc-21.2-linux-x86_64/include",
"--proto_path=${workspaceRoot}/protos",
"--go_out=${workspaceRoot}"
]
}

Related

Is it possible to make the internal dependencies inside a python module user selectable?

After testing the logger library locally, I uploaded it to pypi.
Afterwards, when I proceeded with pip install, there was an error saying that the module inside the library could not be found.
So, as a temporary measure, I added a syntax to register all .py in init.py in the library package folder, and I want to improve this. This is because you have to install all dependencies for features that users may not be using
What improvements can I take in this situation?
If possible, I would like to know how to lazy use only the modules used by the user instead of registering all .py in init.py .
Or is there something structurally I'm overlooking?
Here is the project structure I used
project_name
- pacakge_name
- __init__.py. <- all loggers were registered
- file_logger.py
- console_logger.py
- ...
- fluent_logger.py <- used external library
- scribe_logger.py <- used external library
init.py
"""
Description for Package
"""
from .composite_logger import CompositeLogger
from .console_logger import ConsoleLogger
from .file_logger import FileLogger
from .fluent_logger import FluentLogger
from .jandi_logger import JandiLogger
from .line_logger import LineLogger
from .logger_impl import LoggerImpl
from .logger_interface import LoggerInterface
from .logger import Logger
from .memory_logger import MemoryLogger
from .null_logger import NullLogger
from .scribe_logger import ScribeLogger
from .telegram_logger import TelegramLogger
from .retry import Retry
__all__ = [
'CompositeLogger',
'ConsoleLogger',
'FileLogger',
'FluentLogger',
'JandiLogger',
'LineLogger',
'LoggerImpl',
'LoggerInterface',
'Logger',
'MemoryLogger',
'NullLogger',
'ScribeLogger',
'TelegramLogger',
'Retry',
]
setup.py
import setuptools
from distutils.core import setup
with open("README.md", "r", encoding="utf-8") as f:
long_descriprion = f.read()
setuptools.setup(
name = 'project_name',
version = '0.0.1',
description = 'python logger libary',
long_description = long_descriprion,
long_description_content_type = "text/markdown",
author = 'abc',
author_email = 'abc#gmail.com',
url = "https://github.com/##/##",
packages = ["pacakge_name"],
install_requires=[ <- contains too many external libraries
'requests>=2.0.0',
'thrift>=0.16.0',
'facebook-scribe>=2.0.post1',
'fluent-logger>=0.10.0'
],
keywords = ['logger'],
python_requires = '>=3.7',
classifiers = [
'Programming Language :: Python :: 3.7',
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent"
],
)

How to read value of custom options from protobuf.js generated code?

Similar to the example at: https://developers.google.com/protocol-buffers/docs/proto#extensions
Suppose I have a proto like:
import "google/protobuf/descriptor.proto";
extend google.protobuf.FieldOptions {
string search_key = 7000;
}
message Person {
string name = 1 [(search_key) = "searchIndex.firstName"];
}
then I use the protobufjs-cli to generate a static module:
pbjs -t static-module -w commonjs -o compiled.js test.proto
How can I then read the descriptor in javascript using the generated module?
As mentioned in a comment, this required access to either the original .proto file or the output of:
pbjs -t proto3 -o compiled.proto myfile.proto
but once I had that it was simply a matter of:
import * as protobuf from 'protobufjs';
const root = protobuf.loadSync('test.proto')
const Person = root.lookupType('main.Person')
console.log(Person.fields.name.options!['(search_key)'])
// logs: searchIndex.firstName

how to remove the unused genreated require when use protobuf anotations

package usegogo.api.v1;
import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/field_mask.proto";
import "gogoproto/gogo.proto";
option (gogoproto.marshaler_all) = false;
I use gogoproto to generate go codes.
But when I generate nodejs code, there is a var gogoproto_gogo_pb = require('../../../gogoproto/gogo_pb.js');
this is generated because i use import "gogoproto/gogo.proto";
is there any way to let protoc ignore the the import "gogoproto/gogo.proto"; sine i don't use this when i genreate nodejs code.
Protoc will actually generate gogo_pb.js if you point it to gogo.proto like you do with your other proto files.

Golang invalid import path when importing from "main" folder

Been trying for the last few days to get rid of "invalid import path:"Atom First project/main/Extension" (build)" error when installing my main.go file but i haven't been able to find the reason behind the error.
OS - Windows 10
IDE - Atom
GOBIN - E:\Github Repository\Programming\Golang\bin
GOPATH - E:\Github Repository\Programming\Golang
File DIR- E:\Github Repository\Programming\Golang\src\Atom First project\main\main.go
E:\Github Repository\Programming\Golang\src\Atom First project\main\Extension/foo.go
main.go
package main
import (
"Atom First project/main/Extension"
)
func main() {
Extension.Extend()
}
foo.go
package Extension
import (
"fmt"
)
func Extend(){
fmt.Println("Hello from Extend func")
}
It's simple: import paths cannot contain spaces. Spec: Import declarations:
Implementation restriction: A compiler may restrict ImportPaths to non-empty strings using only characters belonging to Unicode's L, M, N, P, and S general categories (the Graphic characters without spaces) and may also exclude the characters !"#$%&'()*,:;<=>?[]^`{|} and the Unicode replacement character U+FFFD.
Simply rename your Atom First project folder to e.g. atom-first-project, and change the import declaration.
import (
"atom-first-project/main/Extension"
)
Also note that the package name (which is usually the folder name but not necessarily) must be a valid Go identifier. Spec: Package clause:
A package clause begins each source file and defines the package to which the file belongs.
PackageClause = "package" PackageName .
PackageName = identifier .

proto3 -> go with custom extensions resulting in imports to package ("google/protobuf") in go code

I am prototyping a meta model on top of proto3. To generate domain specific boilerplate as the go proto3 extension syntax is ridiculously expressive. My domain proto files depend on meta.proto which contain the extensions.
I can compile the these to go. When including the meta.proto file the generated go ends up with the following include block:
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import google_protobuf "google/protobuf" <--- this import does not exist !!
My extension file has the following structure(based off this):
syntax = "proto2";
package "...";
option go_package = "...";
import "google/protobuf/descriptor.proto"; <--- this causes the import
// message MyExtensionClass ...
// message MyExtensionField ...
extend google.protobuf.MessageOptions {
optional MyExtensionClass class = 50000;
}
extend google.protobuf.FieldOptions {
optional MyExtensionField field = 50001;
}
I know the solution is likely very simple, the google/protobuf include is meant for C++ generation.
In my workspace the included package should be "github.com/golang/protobuf/protoc-gen-go/descriptor"
Poor mans solution. Not ideal, directing it to the relevant go import works:
sed -i '' -e 's/import google_protobuf \"google\/protobuf\"/import google_protobuf \"github.com\/golang\/protobuf\/protoc-gen-go\/descriptor\"/g' pkg/domain/proto/extensions/*.pb.go

Resources