-
Notifications
You must be signed in to change notification settings - Fork 0
/
postprocess.go
84 lines (70 loc) · 3.19 KB
/
postprocess.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2020 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package postprocess handles post processing during the mapping process.
package postprocess
import (
"fmt"
"github.com/Lakshmi-Priya-Ramisetty/healthcare-data-harmonization/mapping_engine/errors" /* copybara-comment: errors */
"github.com/Lakshmi-Priya-Ramisetty/healthcare-data-harmonization/mapping_engine/mapping" /* copybara-comment: mapping */
"github.com/Lakshmi-Priya-Ramisetty/healthcare-data-harmonization/mapping_engine/projector" /* copybara-comment: projector */
"github.com/Lakshmi-Priya-Ramisetty/healthcare-data-harmonization/mapping_engine/types" /* copybara-comment: types */
"github.com/Lakshmi-Priya-Ramisetty/healthcare-data-harmonization/mapping_engine/util/jsonutil" /* copybara-comment: jsonutil */
mappb "github.com/Lakshmi-Priya-Ramisetty/healthcare-data-harmonization/mapping_engine/proto" /* copybara-comment: mapping_go_proto */
)
const (
bundleResourceType = "Bundle"
transactionType = "transaction"
)
// Process handles post processing logic for the mapping library.
func Process(pctx *types.Context, config *mappb.MappingConfig, skipBundling bool, e mapping.Engine) (jsonutil.JSONToken, error) {
var result jsonutil.JSONToken
errLocation := errors.FnLocationf("Post Processing")
var p types.Projector
switch proj := config.PostProcess.(type) {
case *mappb.MappingConfig_PostProcessProjectorDefinition:
p = projector.FromDef(proj.PostProcessProjectorDefinition, e)
case *mappb.MappingConfig_PostProcessProjectorName:
fp, err := pctx.Registry.FindProjector(proj.PostProcessProjectorName)
if err != nil {
return nil, errors.Wrap(errLocation, fmt.Errorf("post_process projector %v not found", proj.PostProcessProjectorName))
}
p = fp
}
result = *pctx.Output
if len(pctx.TopLevelObjects) > 0 {
if err := jsonutil.Merge(convertTopLevelObjectsToContainer(pctx), &result, true, false); err != nil {
return nil, errors.Wrap(errLocation, fmt.Errorf("attempt to merge root mappings with target_object (Output Key) mappings failed: %v. target_object is deprecated, consider using target_root_field", err))
}
}
if p != nil && !skipBundling {
jmn, err := jsonutil.TokenToNode(result)
if err != nil {
return nil, errors.Wrap(errLocation, err)
}
res, err := p([]jsonutil.JSONMetaNode{jmn}, pctx)
if err != nil {
return nil, errors.Wrap(errLocation, err)
}
result = res
}
return result, nil
}
func convertTopLevelObjectsToContainer(ctx *types.Context) jsonutil.JSONContainer {
cont := make(jsonutil.JSONContainer)
for name, arr := range ctx.TopLevelObjects {
jarr := jsonutil.JSONToken(jsonutil.JSONArr(arr))
cont[name] = &jarr
}
return cont
}