-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Makefile): add 'test' target to run Go tests
feat(go.mod): add 'github.com/google/go-cmp' dependency for testing feat(cli.go): add 'Update' and 'Dry' options for updating missing fields and dry run feat(cli.go): add logic to handle 'Update' and 'Dry' options in 'Run' method feat(json.go): add new file for JSON diff, extract, and merge operations test(json_test.go): add tests for JSON diff and extract functions feat(client.go): add 'ResponseFormat' option to configure OpenAI API response format This commit introduces several new features and tests. The 'test' target in the Makefile and the 'go-cmp' dependency are added to facilitate testing. In 'cli.go', 'Update' and 'Dry' options are introduced to allow updating missing fields in the output file and performing a dry run respectively. The handling of these options is also added in the 'Run' method. A new file 'json.go' is added to perform JSON diff, extract, and merge operations. Tests for these operations are added in 'json_test.go'. Finally, the 'ResponseFormat' option is added in 'client.go' to configure the format of the response from the OpenAI API.
- Loading branch information
Showing
7 changed files
with
368 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
.PHONY: docs | ||
docs: | ||
@./docs.sh | ||
|
||
.PHONY: test | ||
test: | ||
go test ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package dragoman | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// JSONPath represents a sequence of keys that specify a unique path through a | ||
// JSON object hierarchy, similar to an address for locating a specific value | ||
// within a nested JSON structure. It is used to traverse and extract data from | ||
// complex JSON documents. | ||
type JSONPath []string | ||
|
||
// JSONDiff identifies the differences between two JSON objects or two raw JSON | ||
// byte representations. It returns a slice of JSONPaths that represent the | ||
// hierarchical structure of keys where differences exist, and an error if any | ||
// occur during the process. The function is generic and can accept either raw | ||
// bytes or maps as inputs for comparison. | ||
func JSONDiff[TInput []byte | map[string]any](source, target TInput) ([]JSONPath, error) { | ||
var sourceMap, targetMap map[string]any | ||
|
||
switch source := any(source).(type) { | ||
case []byte: | ||
if err := json.Unmarshal(source, &sourceMap); err != nil { | ||
return nil, fmt.Errorf("unmarshal source: %w", err) | ||
} | ||
|
||
if err := json.Unmarshal(any(target).([]byte), &targetMap); err != nil { | ||
return nil, fmt.Errorf("unmarshal target: %w", err) | ||
} | ||
case map[string]any: | ||
sourceMap = source | ||
targetMap = any(target).(map[string]any) | ||
} | ||
|
||
return jsonDiffPaths(sourceMap, targetMap) | ||
} | ||
|
||
func jsonDiffPaths(source, target map[string]any) (paths []JSONPath, _ error) { | ||
for k, v := range source { | ||
switch v := v.(type) { | ||
case map[string]any: | ||
targetValue, ok := target[k] | ||
if ok { | ||
targetMap, ok := targetValue.(map[string]any) | ||
if !ok { | ||
return paths, fmt.Errorf("target value at %q is not a map", k) | ||
} | ||
|
||
subPaths, err := jsonDiffPaths(v, targetMap) | ||
if err != nil { | ||
return paths, err | ||
} | ||
|
||
subPaths = mapSlice(subPaths, func(p JSONPath) JSONPath { | ||
return append(JSONPath{k}, p...) | ||
}) | ||
|
||
paths = append(paths, subPaths...) | ||
} else { | ||
subKeys := allKeys(v) | ||
subKeys = mapSlice(subKeys, func(p JSONPath) JSONPath { | ||
return append(JSONPath{k}, p...) | ||
}) | ||
|
||
paths = append(paths, subKeys...) | ||
} | ||
default: | ||
if _, ok := target[k]; !ok { | ||
paths = append(paths, JSONPath{k}) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
// JSONExtract extracts values from a JSON document according to specified paths | ||
// and returns them as a map. It supports both raw JSON bytes and already-parsed | ||
// maps as input. If any path does not exist or leads to an unexpected type, an | ||
// error is returned alongside the partial output. | ||
func JSONExtract[TData []byte | map[string]any](data TData, paths []JSONPath) (map[string]any, error) { | ||
var dataMap map[string]any | ||
switch data := any(data).(type) { | ||
case []byte: | ||
if err := json.Unmarshal(data, &dataMap); err != nil { | ||
return nil, fmt.Errorf("unmarshal data: %w", err) | ||
} | ||
case map[string]any: | ||
dataMap = data | ||
} | ||
|
||
out := make(map[string]any) | ||
for _, path := range paths { | ||
if err := jsonExtract(dataMap, path, out); err != nil { | ||
return out, err | ||
} | ||
} | ||
return out, nil | ||
} | ||
|
||
func jsonExtract(data map[string]any, path JSONPath, out map[string]any) error { | ||
if len(path) == 0 { | ||
return nil | ||
} | ||
|
||
key := path[0] | ||
value, ok := data[key] | ||
if !ok { | ||
return fmt.Errorf("key %q not found", key) | ||
} | ||
|
||
if len(path) == 1 { | ||
out[key] = value | ||
return nil | ||
} | ||
|
||
subPath := path[1:] | ||
subMap, ok := value.(map[string]any) | ||
if !ok { | ||
return fmt.Errorf("value at %q is not a map", key) | ||
} | ||
|
||
if _, ok := out[key]; !ok { | ||
outSubMap := make(map[string]any) | ||
out[key] = outSubMap | ||
} | ||
|
||
outSubMap := out[key].(map[string]any) | ||
|
||
return jsonExtract(subMap, subPath, outSubMap) | ||
} | ||
|
||
// JSONMerge combines the contents of two JSON object maps, where 'from' is | ||
// merged into 'into'. If there are matching keys, the values from 'from' will | ||
// overwrite those in 'into'. For nested maps, merging is performed recursively. | ||
// This function modifies the 'into' map directly and does not return a new map. | ||
func JSONMerge(into map[string]any, from map[string]any) { | ||
for k, v := range from { | ||
switch v := v.(type) { | ||
case map[string]any: | ||
intoValue, ok := into[k] | ||
if ok { | ||
intoMap, ok := intoValue.(map[string]any) | ||
if !ok { | ||
intoMap = make(map[string]any) | ||
into[k] = intoMap | ||
} | ||
JSONMerge(intoMap, v) | ||
} else { | ||
into[k] = v | ||
} | ||
default: | ||
into[k] = v | ||
} | ||
} | ||
} | ||
|
||
func mapSlice[V, O any](s []V, fn func(V) O) []O { | ||
out := make([]O, len(s)) | ||
for i, v := range s { | ||
out[i] = fn(v) | ||
} | ||
return out | ||
} | ||
|
||
func allKeys(m map[string]any) []JSONPath { | ||
var keys []JSONPath | ||
for k, v := range m { | ||
switch v := v.(type) { | ||
case map[string]any: | ||
subKeys := allKeys(v) | ||
subKeys = mapSlice(subKeys, func(p JSONPath) JSONPath { | ||
return append(JSONPath{k}, p...) | ||
}) | ||
keys = append(keys, subKeys...) | ||
default: | ||
keys = append(keys, JSONPath{k}) | ||
} | ||
} | ||
return keys | ||
} |
Oops, something went wrong.