importing a git-submodule into a golang gae app - google-app-engine

I have a submodule in my golang google-app-engine project that I would like to add to my path.
$ ls ./openid/src/openid
discover.go integration verify.go
discover_test.go nonce_store.go xrds.go
discovery_cache.go nonce_store_test.go xrds_test.go
fake_getter_test.go normalizer.go yadis_discovery.go
getter.go normalizer_test.go yadis_discovery_test.go
html_discovery.go redirect.go
html_discovery_test.go redirect_test.go
In the example code for this package, it imports "openid". I'm new to golang's import rules, and I can't for the life of me figure out what I need to put in the import statement of my main file to import this package. I've tried "openid/src/openid", "myapp/openid/src/openid", etc. Can someone provide some clarification of how this works? Or do I need to actually modify the app.yaml file?

"Organizing Go code" mentions:
An import path is the string with which users import a package.
It specifies the directory (relative to $GOROOT/src/pkg or $GOPATH/src) in which the package's source code resides.
So make sure to use an import statement referring to a path which exists in your $GOPATH/src. (GOPATH being your "workspace", in which you have namespaces, as shown in this video).
Also make sure you build first openid/src/openid sources. And install it (go install).
As detailed in this answer:
Import paths can be be globally unique.
In conjunction with GOPATH, import path can be translated unambiguously to a directory path.
Any directory path under GOPATH can be unambiguously translated to an import path.

Related

How to import a folder of svg files into react as ReactComponents

I am making a real-time card game using react for my front-end (which was made using CRA), and I am storing all of my cards as .svg files in a subfolder in the src folder. I have seen in other questions that for single files you could use import {ReactComponent as Image} from 'whatever.svg' and for an entire folder to use require.context(). While the import statement works, the require.context method just provides me with /static/media/whatever.randomStuff.svg files that I can only import by making an http(s) request to that location on the site. I have tried using #svgr/webpack in the require.context function, but even when installed manually, it gives me the same output. Is there a way for me to use the import statement or something else to import all of the .svg files in that folder as ReactComponents in the file instead of having to make an http(s) request?
The answer I was looking for was in this answer from another question. One thing I didn't know at the time of posting was that the version of webpack that react-scripts was using (v4.44.2) already had #svgr/webpack, so manually installing it with npm was essentially useless because it was probably already using it to generate the /static/media/fileName.randomStuff.svg files in the first place. In the end I used an intermediate file in the svg folder and did this:
interface CardCache {
[key: string]: React.FunctionComponent<React.SVGAttributes<SVGElement>>;
}
const cache: CardCache = {};
/* eslint import/no-webpack-loader-syntax: off */
const files = require.context("!svg-react-loader?name=card!./", true, /\.svg$/);
files.keys().forEach(path => {cache[path] = files(path)});
export {cache};
I used the eslint comment because I did not want to replace react-scripts with something like react-app-rewired just so I could have access to the webpack.config.json file.

Where is React actually being imported from?

The top of most React files has:
import React from 'react'
Yet react is not actually a file. So where is it and how is it being imported from nothing?
When you import from react it first looks into the node_modules/react/index.js like other module looks for the index.js if there's no file specified. And you may also ask why does it look for node_modules? The answer is you have not specified relative or absolute file path for eg. ./components/MyComponent. When you do not specify the specific path, it will look for the node_modules directory.
The index.js exports is like:
if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react.production.min.js');
} else {
module.exports = require('./cjs/react.development.js');
}
So, let's continue with development environment. Now, when you look into the file node_modules/react/csj/react.development.js, you will find several exports statement at the end of the file.
So, you're simply importing React means you're importing all of them. And thus, you can use React.Component, React.Children, etc.
It's not necessary that you must have named React but it's standard. So, even if you do:
import ReactApplication from 'react'
You have access to all of them like ReactApplication.Component. Hope, this clears up things.
Further details:
When you specify ./, it will look for the current directory.
When you specify /, it will look for the root directory.
When you do not specify, it will first look to directory in your project and if it doesn't find, it will look into the node_modules directory.
Other post you may be interested to look into: https://stackoverflow.com/a/27218926/2138752
React is available as a dependency in the node_modules directory.
React must also be in the scope of files containing JSX to enable transpilers like Babel know how to handle that syntax.
React is installed as an npm package, so it can be found in your node_modules folder. That's where it's being imported from.

Importing react module from /src not node_modules

I was using react rc-tree module in my project by importing it like this:
import Tree, { TreeNode } from 'rc-tree';
This worked fine. However after a while I wanted to make changes to the source code of rc-tree, so I cloned the source code from github into my ./src directory. i.e. I now have a directory called ./tree under my ./src directory. How do I call that code, rather than rc-tree in node_modules?
I have tried various import statements but nothing works.
Add .env file in root and add this line
NODE_PATH=src
Now import
import Tree, { TreeNode } from 'tree';
You should try to relative import your code.
Think if you are in this path src/component/childComponent/ where you import your desire package and here you must use a relative path to the package which is inside src.
import Tree, { TreeNode } from '../../tree';
this .. indicates that you go backward inside your current path.
In this example, it means to go up two directories (component and childComponent). here now you are addressing src directory you should write the rest of your path which is /tree.
I take look at rc-tree package and noticed it has been written with typescript. If your app doesn't support typescript you can't use source code of the package and you should build package first and then import build directory.
In case your app supports absolute paths you can easily use:
import Tree, { TreeNode } from 'src/tree';
I couldn't get these suggestions to work, probably my fault... so I just moved the entire rc-tree sub directory from node_modules to under my source tree and called it "tree". I then made a sym-link from node_modules/rc-tree to the ./tree in my source tree.
I just import Tree, TreeNodes etc, like I am using the module, but really it's calling the copy of the code in ./tree.
It all seems to work. The only thing that is not so good is that because the code is now under ./src the linter prints many warnings about the code. Obviously it is not my code, and I would rather not hear about it, but I can live with this.

Why is files saying Module not found: Can't resolve be?

Trying to import my folders but they keep on saying that cannot find folder but the folder I'm import is the correct:
./src/LoginPage/Login.js
Module not found: Can't resolve '../_actions' in >'/Users/mirasmith/Desktop/KPV1-Project-master/src/LoginPage'
I don't know how to show the image so if someone can help fix it. Here the link to the folder hierarchy.
https://gyazo.com/410386debd550039400cf40e9e448196
Does _actions have a index.js? When require is given the path of a folder, it'll look for an index.js file in that folder. If there is one, it uses it. If not, it doesn't know what file you're trying to import.
If you don't have an index.js in there, you'll need something like:
"import actions from ../_actions/filename.js"
If you are trying to do an import of all the files from a folder, this answer talks about how to do that:
https://stackoverflow.com/a/5365577/5216218
import alert from "../_actions/alert.actions.js"
import user from "../_actions/user.actions.js"

Go Package Conflict

I am new to Go and AppEngine. I am trying to figure out how to create packages but I keep running into conflicts. My directory structure is below:
GOPATH
third-party-libs
app
app.yaml
controllers
default.go -- package controllers
models
models.go -- package models
templates
templates.go -- package templates
I am importing the templates package as follows import ("app/templates") inside default.go
When I do goapp serve I get this error:
Failed parsing input: app file templates.go conflicts with
same file imported from GOPATH
I have tried a bunch of things and nothing has worked so far. These are things I have tried:
Changed the templates directory to apptemplates and the corresponding file to apptemplates.go, changed package name to apptemplates. I imported it as app/apptemplates
I tried different combinations by changing the file name but not the package name, vice versa, etc. Either it does not find the file or has a conflict.
I am importing html/template in my templates.go file. So I commented out the entire file just keeping the package declaration but did not make the conflict go away
I thought may be another file is named templates.go but when I do this (at the GOPATH level) find . -name "*.go" | grep "templates.go" I only see the one file I have created.
I am confused as to how packages are created. I have changed the name to something generic so it does not look like a naming issue. Can someone please tell me how I can debug this error?
Thanks!
Rename the package to a non-conflicting name as in #1. Import the package using the path "apptemplates".
Packages inside of the application directory (the directory containing app.yaml) are imported with a path relative to the application directory. See Organizing Go Apps for the complete details.

Resources