I have many JSON files in a Vue.js application, with numerous references to global constants. Is it possible to import constants into a json file, or to refer to .env constants from my .json files?
The following examples break my tests:
"href": process.env.SOME_EXTERNAL_LINK, or
"href": "$(process.env.SOME_EXTERNAL_LINK)", or
"href": `${process.env.SOME_EXTERNAL_LINK}`
Is there a "standard" way to do this?
The suggestion of Estus Flask is correct - this is the best way forward.
Related
I am not a software guy, but I am preparing a small script for delarative onboarding work using terraform. The declaration is in a json file and it is send to the device through terraform. But I do not want to keep the sensitive data like passwords and secret keys in the json file. I want to inject them from a seperate file in a vault to the json file when I am executing it in terraform. I can use terraform to read the data from a vault or a s3 bucket, but I am not sure how to add it to the json declaration. I saw in terraform there is a functions like jsondecode and jsonencode. But I am not sure whether they will be any use to my requirement.
Could someone help me on this please ?
Thansk.
Maybe this is something which could help you?
Create a data source which loads your template file, here you can pass in some vars.
some examples for variales:
data "template_file" "my_template" {
template = templatefile("template.json", {
foo = "bar"
another_foo = aws_s3_bucket.my_bucket.id
more_foo = var.my_variable
})
}
The template file could look like this:
{
"Value": "123",
"value_2": "${foo}",
"value_3": "${another_foo}",
"value_4": ${more_foo},
}
We're gluing together cloud formation template .yaml files with boto3. My strong inclination is to use the .ini format for parameter and tag files because
.ini format is easier to read than either YAML or json
the python 'configparser' library supports a 'default' section
which will reduce typing a lot.
Two possible disadvantages to this approach are:
native .ini doesn't support lists
We might want to feed the parameter files to the aws cli
There are a bunch of ways to extend configparser, the standard python library for handling ini files to handle lists.
I don't think we will want or need to feed yaml to the aws cli and converting ini to YAML doesn't seem hard.
What disadvantage of ini am I missing ?
For comparison same file in ini / yaml / json
; ini
[default]
KeyPairName = MyKey
InstanceType = m1.micro
# YAML
---
- ParameterKey: KeyPairName
ParameterValue: MyKey
- ParameterKey: InstanceType
ParameterValue: m1.micro
json:
[
{
"ParameterKey": "KeyPairName",
"ParameterValue": "MyKey"
},
{
"ParameterKey": "InstanceType",
"ParameterValue": "m1.micro"
}
]
You can bypass the problem of .ini not handling list with parameters of type CommaDelimitedList. Considering you only want to use the .ini for parameters and tags, I don't foresee issues of doing this.
Hard to admit it but I struggle to manage state in my cli go app. What I basically want is to manage a list of objects with their properties in a file on disk. I want to be able to add objects with their properties, update objects and/or their properties and remove them if needed.
I thought it would be easy to just have a yml or json file and edit it with some kind of library but it seems harder than it should for a go beginner like me.
An example would be the following json structure.
{
"servers":
[
{ "hostname": "gandalf", "ip": "192.168.1.10", "color": "red" },
{ "hostname": "bilbo", "ip": "192.168.1.11", "color": "blue" },
{ "hostname": "frodo", "ip": "192.168.1.12", "color": "yellow" }
]
}
Now I want to be able to add, remove and edit servers. It doesn't have to be json, yaml is fine as well.
Do you girls and guys have any suggestions (libs and an example) on how to do it? I already tried Viper but adding new objects or even editing the existing ones seems to be impossible.
For settings that need to be human readable and will primarily be edited by a human a yaml or json file seems fine.
If the state is both written and read by the program itself and a full fledged database seems overkill then I would use a file based database. Probably a simple key/value store like boltdb or sqlite if the data needs more structure.
I personally use boltdb in such a case, because it is very lightweight, superfast and I like its simplicity.
-- edit --
With json as a file structure the problem is that you need to write and read the entire file every single time. A edit would be a read of the entire file, unmarshalling the json, changing something in the unmarshalled object, marshalling it back to json and writing the entire file again.
Thats why I would only use this for settings that the program reads once on startup and that's it.
I'm trying to build a JSON schema (draft-06) for an existing JSON data file. The schema has gotten to big to fit in a single file and I am trying to break it up into multiple files. However I keep getting the error can't resovle refrence sectionTableSchema.txt from id http://somesite/section
I've made the following files and placed them in the same directory. These files are not hosted on a webserver.
settingsSchema.txt:
{
"title":"Settings",
"$schema":"http://json-schema.org/draft-06/schema#",
"$id":"http://somesite/settingsSchema.txt",
"properties":{
"section":{
...
...
...
"$id":"/section",
"items":{
"oneOf":[
{"$ref":"#/sectionTableSchema.txt"},
{"$ref":"#/sectionNonTableSchema.txt"}
]
},
"type":"array"
}
},
"type":"object"
}
sectionTableSchema.txt
{
"$schema":"http://json-schema.org/draft-06/schema#",
"$id":"http://somesite/sectionTableSchema.txt",
"definitions":{},
"properties":{
....
....
....
}
"type":"object"
}
I think part of my issue is that I don't understand the $id and $ref keywords enough to know what is going on between absolute uri, relative uri, json-pointers and the like.
What happens?
In the OP's example "$ref":"#/sectionTableSchema.txt" means "inside current schema file, get JSON property called sectionTableSchema.txt from root element".
How $ref works?
$ref can be presented in two parts:
Before "#" sign you can specify URI (in simple words, a path) of schema file. If you start $ref from "#" sign and, therefore, don't provide URI of schema file, this means you want to take schemas from current file.
After "#" sign you can provide a JSON pointer, e. g. a path to the property in JSON that is located in specified file (or current file, depending on provided schema URI before "#" sign).
The same information can be found in documentation.
How to reference other file correctly?
As I understand, JSON Schema drafts say you can specify a valid URI to schema file. But URIs cannot be relative, so there is no standard-defined way of referencing files with relative paths. However, in practice a lot of tools provide abilities to do this, though they are not a part of the standard and may differ from tool to tool.
In AJV you can write your reference as "$ref":"ANY-ID-YOU-WANT" and use ajv.addSchema(schemaJson, 'ANY-ID-YOU-WANT') method, where schemaJson is a variable with require'd sectionTableSchema.txt content.
I am interested in reading a schemas(json formatted text file) and unmarshall it as schemas (for which i have some JSON structures defined in a .GO file) and For each type of structure in the Schema, I want to generate a corresponding .go file which has the code for performing CRUD operations using the template package (http://golang.org/pkg/text/template/) to generate these files.
Example of a structure in a schema file -
{
type struct XYZ {
Type string `json:"type,omitempty"`
ResourceType string `json:"resourceType,omitempty"`
Links map[string]string `json:"links,omitempty"`
}
The text file has a JSON structured data which is something of this form -
{
"type": "collection",
"resourceType": "schema",
"links": {
"self": "…/v1/schemas",
},
"createTypes": { },
"actions": { },
"data": [ 86 items
{
"id": "schema",
"type": "schema",
"links": {
"self": "/schemas/schema",
"collection": "…/schemas",
},
...
}
}
Could somebody help me how could i possibly generate the code for these CRUD operations for different structs using the GO template package.
You might find go generate useful.
proposal: go generate
New go tool subcommand proposed for Go
1.4. Please see the design document and comment in this thread.
http://golang.org/s/go1.4-generate
-rob
Go generate: A Proposal
Introduction
The go build command automates the construction of Go programs but
sometimes preliminary processing is required, processing that go build
does not support. Motivating examples include:
yacc: generating .go files from yacc grammar (.y) files
protobufs: generating .pb.go files from protocol buffer definition (.proto) files
Unicode: generating tables from UnicodeData.txt
HTML: embedding .html files into Go source code
bindata: translating binary files such as JPEGs into byte arrays in Go source
There are other processing steps one can imagine:
string methods: generating String() string methods for types used as enumerated constants
macros: generating customized implementations given generalized packages, such as sort.Ints from ints
This proposal offers a design for smooth automation of such
processing.