Beispiel #1
0
func equaljson(p, q []byte) bool {
	cp := bytes.NewBuffer([]byte{})

	if err := json.Compact(cp, p); err != nil {
		log.Printf("unable to compact cp json for equaljson: %+v", err)
		return false
	}

	cq := bytes.NewBuffer([]byte{})

	if err := json.Compact(cq, q); err != nil {
		log.Printf("unable to compact cq json for equaljson: %+v", err)
		return false
	}

	if len(cp.Bytes()) != len(cq.Bytes()) {
		return false
	}

	cpb, cqb := cp.Bytes(), cq.Bytes()

	for i, b := range cpb {
		if b != cqb[i] {
			return false
		}
	}

	return true
}
Beispiel #2
0
func processJson(filename string, src []byte, opt *JsonFmtOption) ([]byte, error) {
	if opt.Compact {
		var out bytes.Buffer
		err := json.Compact(&out, src)
		if err != nil {
			return nil, err
		}
		return out.Bytes(), nil
	} else {
		var out bytes.Buffer
		var err error
		if opt.IndentTab {
			err = json.Indent(&out, src, "", "\t")
		} else {
			var indent string
			for i := 0; i < opt.TabWidth; i++ {
				indent += " "
			}
			err = json.Indent(&out, src, "", indent)
		}
		if err != nil {
			return nil, err
		}
		return out.Bytes(), nil
	}
	return src, nil
}
Beispiel #3
0
func pathRolesWrite(
	req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
	var buf bytes.Buffer

	uip, err := useInlinePolicy(d)
	if err != nil {
		return nil, err
	}

	if uip {
		if err := json.Compact(&buf, []byte(d.Get("policy").(string))); err != nil {
			return logical.ErrorResponse(fmt.Sprintf(
				"Error compacting policy: %s", err)), nil
		}
		// Write the policy into storage
		err := req.Storage.Put(&logical.StorageEntry{
			Key:   "policy/" + d.Get("name").(string),
			Value: buf.Bytes(),
		})
		if err != nil {
			return nil, err
		}
	} else {
		// Write the arn ref into storage
		err := req.Storage.Put(&logical.StorageEntry{
			Key:   "policy/" + d.Get("name").(string),
			Value: []byte(d.Get("arn").(string)),
		})
		if err != nil {
			return nil, err
		}
	}

	return nil, nil
}
Beispiel #4
0
func readFiles(files ...string) []byte {
	lfs := len(files)
	chContent := make(chan []byte, lfs)

	for _, file := range files {
		go func(chContent chan []byte, configPath string) {
			chContent <- format(configPath)
		}(chContent, file)
	}

	bytess := make([][]byte, 0, lfs)

	for i := 1; i <= lfs; i++ {
		content := <-chContent

		if len(content) != 0 {
			bytess = append(bytess, content)
		}
	}

	buf := bytes.NewBufferString(`{`)
	buf.Write(bytes.Join(bytess, []byte(`,`)))
	buf.WriteString(`}`)

	var contentBuf bytes.Buffer
	err := json.Compact(&contentBuf, buf.Bytes())
	if err != nil {
		log.Debug("<readFiles> jsonData: ", buf.String())
		log.Fatal("<readFiles> error: ", err)
	}

	return contentBuf.Bytes()
}
func prepAndPostEvent(eventFile string, preFunc PreFunc) (err error) {
	rawEvent, err := ioutil.ReadFile(eventFile)
	if err != nil {
		return err
	}

	event := &Event{}
	err = json.Unmarshal(rawEvent, &event)
	if err != nil {
		return err
	}
	preFunc(event)
	rawEvent, err = json.Marshal(event)
	if err != nil {
		return err
	}

	buffer := new(bytes.Buffer)
	err = json.Compact(buffer, rawEvent)
	if err != nil {
		return err
	}
	http.Post(pushUrl, "application/json", buffer)

	return nil
}
Beispiel #6
0
func compactJSON(js []byte) []byte {
	buf := new(bytes.Buffer)
	if err := json.Compact(buf, js); err != nil {
		return nil
	}
	return buf.Bytes()
}
Beispiel #7
0
func testJSONMarshal(t *testing.T, v interface{}, expect string) {
	j, err := json.Marshal(v)
	if err != nil {
		t.Errorf("Unable to marshal JSON for %v", v)
	}

	w := new(bytes.Buffer)
	err = json.Compact(w, []byte(expect))
	if err != nil {
		t.Errorf("String is not valid json: %s", expect)
	}

	if w.String() != string(j) {
		t.Errorf("json.Marshal(%q) returned %s, expect %s", v, j, w)
	}

	u := reflect.ValueOf(v).Interface()
	if err := json.Unmarshal([]byte(expect), u); err != nil {
		t.Errorf("Unable to unmarshal JSON for %v", expect)
	}

	if !reflect.DeepEqual(v, u) {
		t.Errorf("json.Unmarshal(%q) returned %s, expect %s", expect, u, v)
	}
}
Beispiel #8
0
// Return json supplied in the argument, or look for a file by the name given.
// Is the name is "STDIN", read the json from stdin
func jsonFromArg(arg string) ([]byte, error) {
	var jsonArg []byte
	var err error

	arg = strings.TrimSpace(arg)

	// assume that an opening bracket mean the json is given directly
	if strings.HasPrefix(arg, "{") {
		jsonArg = []byte(arg)
	} else if arg == "STDIN" {
		jsonArg, err = ioutil.ReadAll(os.Stdin)
		if err != nil {
			return nil, err
		}
	} else {
		// all else fails, look for a file
		jsonArg, err = ioutil.ReadFile(arg)
		if err != nil {
			return nil, err
		}
	}

	// verify the json by compacting it
	buf := bytes.NewBuffer(nil)
	err = json.Compact(buf, jsonArg)
	if err != nil {
		return nil, err
	}

	return buf.Bytes(), nil
}
Beispiel #9
0
// takes a local seccomp daemon, reads the file contents for sending to the daemon
func parseSecurityOpts(securityOpts []string) ([]string, error) {
	for key, opt := range securityOpts {
		con := strings.SplitN(opt, "=", 2)
		if len(con) == 1 && con[0] != "no-new-privileges" {
			if strings.Index(opt, ":") != -1 {
				con = strings.SplitN(opt, ":", 2)
			} else {
				return securityOpts, fmt.Errorf("Invalid --security-opt: %q", opt)
			}
		}
		if con[0] == "seccomp" && con[1] != "unconfined" {
			f, err := ioutil.ReadFile(con[1])
			if err != nil {
				return securityOpts, fmt.Errorf("opening seccomp profile (%s) failed: %v", con[1], err)
			}
			b := bytes.NewBuffer(nil)
			if err := json.Compact(b, f); err != nil {
				return securityOpts, fmt.Errorf("compacting json for seccomp profile (%s) failed: %v", con[1], err)
			}
			securityOpts[key] = fmt.Sprintf("seccomp=%s", b.Bytes())
		}
	}

	return securityOpts, nil
}
Beispiel #10
0
// UnmarshalJSON deserializes a JSON-serialized CardDescription
func (c *CardDescription) UnmarshalJSON(data []byte) error {
	// Compact the JSON to make it easier to process below
	buffer := bytes.NewBuffer([]byte{})
	err := json.Compact(buffer, data)
	if err != nil {
		return err
	}
	data = buffer.Bytes()

	// Since Description can be either a string value or an object, we
	// must check and deserialize appropriately

	if data[0] == 123 { // == }
		obj := make(map[string]string)

		err = json.Unmarshal(data, &obj)
		if err != nil {
			return err
		}

		c.Format = obj["format"]
		c.Value = obj["value"]
	} else {
		c.Format = ""
		err = json.Unmarshal(data, &c.Value)
	}

	if err != nil {
		return err
	}

	return nil
}
Beispiel #11
0
func generateQuery(queryString, appLogsIdentifier, appLogsValue string, timestamp time.Time, from int) []byte {
	query := `{
	"fields": ["@timestamp", "message", "` + appLogsIdentifier + `"],
	"query": {
		"wildcard": {
			"message": "` + queryString + `"
		}
	},
	"filter": {
		"bool": {
			"must": [
				{"term": {"` + appLogsIdentifier + `": "` + appLogsValue + `"}},
				{"range": {"@timestamp": {"gt": "` + fmt.Sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", timestamp.Year(), timestamp.Month(), timestamp.Day(), timestamp.Hour(), timestamp.Minute(), timestamp.Second()) + `"}}}
			]
		}
	},
	"sort": {
		"@timestamp": {
			"order": "asc"
		},
		"message": {
			"order": "asc"
		}
	},
	"from": ` + fmt.Sprintf("%d", from) + `,
	"size": ` + fmt.Sprintf("%d", size) + `
	}`
	var buf bytes.Buffer
	json.Compact(&buf, []byte(query))
	return buf.Bytes()
}
Beispiel #12
0
func HandleListing(writer http.ResponseWriter, request *http.Request, dir_path string) {
	info, err := ioutil.ReadDir(dir_path)
	if err != nil {
		writer.WriteHeader(http.StatusNotFound)
		return
	}

	fmt.Fprint(writer, "{\"files\":[")
	for i, item := range info {
		if i != 0 {
			fmt.Fprint(writer, ",")
		}

		b, err := json.Marshal(FsEntry{
			path:     dir_path + item.Name(),
			mod_time: item.ModTime(),
			size:     item.Size(),
			entries:  nil,
		})
		if err != nil {
			writer.WriteHeader(http.StatusInternalServerError)
			return
		}

		var buf bytes.Buffer
		if err := json.Compact(&buf, b); err != nil {
			writer.WriteHeader(http.StatusInternalServerError)
			return
		}
		buf.WriteTo(writer)
	}
	fmt.Fprint(writer, "]}")
}
Beispiel #13
0
func iterateExecFile(name string) (testCaseIterator chan testCase) {
	name = locateFile(name)
	fd, err := os.OpenFile(name, os.O_RDONLY, 0)
	if err != nil {
		panic(fmt.Sprintf("Could not open file %s", name))
	}
	testCaseIterator = make(chan testCase)
	go func() {
		defer close(testCaseIterator)

		r := bufio.NewReader(fd)
		lineno := 0
		for {
			binput, err := r.ReadBytes('\n')
			if err != nil {
				if err != io.EOF {
					fmt.Printf("Line: %d\n", lineno)
					panic(fmt.Errorf("Error reading file %s: %s", name, err.Error()))
				}
				break
			}
			lineno++
			input := string(binput)
			if input == "" || input == "\n" || input[0] == '#' || strings.HasPrefix(input, "Length:") {
				//fmt.Printf("%s\n", input)
				continue
			}
			err = json.Unmarshal(binput, &input)
			if err != nil {
				fmt.Printf("Line: %d, input: %s\n", lineno, binput)
				panic(err)
			}
			input = strings.Trim(input, "\"")
			var output []byte
			for {
				l, err := r.ReadBytes('\n')
				lineno++
				if err != nil {
					fmt.Printf("Line: %d\n", lineno)
					panic(fmt.Errorf("Error reading file %s: %s", name, err.Error()))
				}
				output = append(output, l...)
				if l[0] == '}' {
					output = output[:len(output)-1]
					b := bytes.NewBuffer(make([]byte, 0, 64))
					if err := json.Compact(b, output); err == nil {
						output = b.Bytes()
					}
					break
				}
				if l[0] == '"' {
					output = output[1 : len(output)-2]
					break
				}
			}
			testCaseIterator <- testCase{name, lineno, input, string(output)}
		}
	}()
	return testCaseIterator
}
Beispiel #14
0
// Jsonize returns raw response on one line with no extra space.
func (f Formatter) Jsonize(input io.Reader) string {
	var s bytes.Buffer
	b, e := ioutil.ReadAll(input)
	Check(e == nil, "failed to read input", e)
	json.Compact(&s, b)
	return s.String()
}
Beispiel #15
0
// Helper function to test that a value is marshalled to JSON as expected.
func testJSONMarshal(t *testing.T, v interface{}, want string) {
	j, err := json.Marshal(v)
	if err != nil {
		t.Errorf("Unable to marshal JSON for %v", v)
	}

	w := new(bytes.Buffer)
	err = json.Compact(w, []byte(want))
	if err != nil {
		t.Errorf("String is not valid json: %s", want)
	}

	if w.String() != string(j) {
		t.Errorf("json.Marshal(%q) returned %s, want %s", v, j, w)
	}

	// now go the other direction and make sure things unmarshal as expected
	u := reflect.ValueOf(v).Interface()
	if err := json.Unmarshal([]byte(want), u); err != nil {
		t.Errorf("Unable to unmarshal JSON for %v", want)
	}

	if !reflect.DeepEqual(v, u) {
		t.Errorf("json.Unmarshal(%q) returned %s, want %s", want, u, v)
	}
}
Beispiel #16
0
func NewRequestMaker(data Data) (reqMaker *RequestMaker, err error) {
	u, err := url.Parse(data.Url)
	if err != nil {
		return
	}

	var contentType string
	var body string

	switch data.Enctype {
	case "x_www":
		contentType = "application/x-www-form-urlencoded"

		querys := u.Query()
		forms := make(url.Values)

		for _, arg := range data.Args {
			switch arg.Method {
			case "GET":
				querys.Add(arg.Key, arg.Value)

			case "POST":
				forms.Add(arg.Key, arg.Value)
			}
		}

		u.RawQuery = querys.Encode()

		body = forms.Encode()

	case "json":
		contentType = "text/json"

		content := strings.TrimSpace(data.JsonContent)
		if len(content) == 0 {
			break
		}

		buffer := new(bytes.Buffer)
		err = json.Compact(buffer, []byte(content))
		if err != nil {
			err = errors.ErrJsonCompact.NewMessageSpf(err)
			return
		}
		body = buffer.String()

	case "plain":
		contentType = "text/plain"
		body = data.PlainContent
	}

	reqMaker = &RequestMaker{
		Method:      data.Method,
		Url:         u,
		ContentType: contentType,
		Body:        body,
		Headers:     data.Headers,
	}
	return
}
Beispiel #17
0
func TestTasks(t *testing.T) {
	for _, tst := range tt {
		req, err := http.NewRequest(tst.method, tst.req, strings.NewReader(tst.payload))
		if err != nil {
			t.Error(err)
		}

		w := httptest.NewRecorder()
		tst.hfn(tst.ctx, w, req)

		if w.Code != tst.rc {
			t.Errorf("%s: Response Code mismatch: expected %d, got %d", tst.description, tst.rc, w.Code)
			continue
		}

		if len(tst.body) == 0 {
			continue
		}

		if equaljson([]byte(w.Body.String()), []byte(tst.body)) == false {
			body := bytes.NewBuffer([]byte{})
			json.Compact(body, []byte(tst.body))
			t.Errorf("%s: Body mismatch:\nexpected %s\ngot      %s", tst.description, string(body.Bytes()), w.Body.String())
			continue
		}
	}
}
Beispiel #18
0
func mustMinifyJSON(input string) string {
	output := bytes.NewBuffer(nil)
	if err := json.Compact(output, []byte(input)); err != nil {
		panic(err)
	}
	return output.String()
}
func (c Cluster) RenderStackTemplate(opts StackTemplateOptions) ([]byte, error) {
	stackConfig, err := c.stackConfig(opts, true)
	if err != nil {
		return nil, err
	}

	rendered, err := execute(opts.StackTemplateTmplFile, stackConfig, false)
	if err != nil {
		return nil, err
	}

	//Use unmarshal function to do syntax validation
	renderedBytes := []byte(rendered)
	var jsonHolder map[string]interface{}
	if err := json.Unmarshal(renderedBytes, &jsonHolder); err != nil {
		syntaxError, ok := err.(*json.SyntaxError)
		if ok {
			contextString := getContextString(renderedBytes, int(syntaxError.Offset), 3)
			return nil, fmt.Errorf("%v:\njson syntax error (offset=%d), in this region:\n-------\n%s\n-------\n", err, syntaxError.Offset, contextString)
		}
		return nil, err
	}

	// minify JSON
	var buff bytes.Buffer
	if err := json.Compact(&buff, renderedBytes); err != nil {
		return nil, err
	}
	return buff.Bytes(), nil
}
func (db *simpleJSONDB) UpdateRecord(id uint32, data string) error {
	var jsonBuffer bytes.Buffer
	if err := json.Compact(&jsonBuffer, []byte(data)); err != nil {
		return err
	}
	record := &core.Record{ID: id, Data: jsonBuffer.Bytes()}
	return actions.Update(db.index, db.buffer, record)
}
Beispiel #21
0
// Bytes returns a compact UTF-8 string representation
func (self *Builder) Bytes() []byte {
	var buffer bytes.Buffer
	err := json.Compact(&buffer, self.buffer.Bytes())
	if err != nil {
		panic(err)
	}
	return buffer.Bytes()
}
Beispiel #22
0
func compacted(in string) string {
	dst := bytes.NewBuffer(nil)
	err := json.Compact(dst, []byte(in))
	if err != nil {
		panic(err)
	}
	return dst.String()
}
Beispiel #23
0
// Helper functions
func compactJSON(inJSON []byte) ([]byte, error) {
	var buf bytes.Buffer
	err := json.Compact(&buf, inJSON)
	if err != nil {
		return []byte(nil), err
	}
	return buf.Bytes(), nil
}
Beispiel #24
0
// packs string literal into json object structure around variable "varName"
// data string should already be in json format
func (this *Neo4j) pack(name string, data string) ([]byte, error) {
	buf := new(bytes.Buffer)
	err := json.Compact(buf, []byte("{ \""+name+"\": "+data+" } ")) // pkg data into new json string then compact() it onto our empty buffer
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), err
}
func TestGenerateResponseJSONFor(t *testing.T) {
	expected := new(bytes.Buffer)
	_ = json.Compact(expected, responseJSON)

	result := GenerateResponseJSONFor("789")

	if !bytes.Equal(expected.Bytes(), result) {
		t.Error(fmt.Sprintf("Expected:\n%s\n\nGot:\n%s\n", expected, result))
	}
}
func RegisterSchemaDefinitions(defs []SchemaDefinition) {
	var buf bytes.Buffer
	for _, def := range defs {
		buf.Reset()
		if err := json.Compact(&buf, def.Definition); err == nil {
			def.Definition = append(def.Definition[:0], buf.Bytes()...)
		}
		definitions[def.Name] = def
	}
}
Beispiel #27
0
func checkMarshalJSON(v interface{}, e string) {
	want, err := json.Marshal(v)
	So(err, ShouldBeNil)

	given := new(bytes.Buffer)
	err = json.Compact(given, []byte(e))
	So(err, ShouldBeNil)

	So(given.String(), ShouldEqual, string(want))
}
func getJSON(header http.Header, body *bytes.Buffer) (*bytes.Buffer, bool) {
	if header.Get("Content-Type") != "application/json" {
		return nil, false
	}
	buf := new(bytes.Buffer)
	if err := json.Compact(buf, body.Bytes()); err != nil {
		return nil, false
	}
	return buf, true
}
Beispiel #29
0
func Load(configFile string, packages ...Package) {
	debug.Nop()
	data, err := ioutil.ReadFile(configFile)
	if err != nil {
		log.Panicf("Error while reading config file %s: %s", configFile, err)
	}
	Packages = packages
	switch path.Ext(configFile) {
	case ".json":
		// Compact JSON to make it easier to extract JSON per package
		var buf bytes.Buffer
		err = json.Compact(&buf, data)
		if err != nil {
			log.Panicf("Error in JSON config file %s: %s", configFile, err)
		}
		data = buf.Bytes()

		// Unmarshal and init packages in given order
		for _, pkg := range packages {
			// Extract JSON only for this package
			key := []byte(`"` + pkg.Name() + `":{`)
			begin := bytes.Index(data, key)
			if begin == -1 {
				continue
			}
			begin += len(key) - 1
			end := 0
			braceCounter := 0
			for i := begin; i < len(data); i++ {
				switch data[i] {
				case '{':
					braceCounter++
				case '}':
					braceCounter--
				}
				if braceCounter == 0 {
					end = i + 1
					break
				}
			}

			err = json.Unmarshal(data[begin:end], pkg)
			if err != nil {
				log.Panicf("Error while unmarshalling JSON from config file %s: %s", configFile, err)
			}
			err := pkg.Init()
			if err != nil {
				log.Panicf("Error while initializing package %s: %s", pkg.Name(), err)
			}
		}

	default:
		panic("Unsupported config file: " + configFile)
	}
}
Beispiel #30
0
func (n *lazyNode) compact() []byte {
	buf := &bytes.Buffer{}

	err := json.Compact(buf, *n.raw)

	if err != nil {
		return *n.raw
	}

	return buf.Bytes()
}