-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbuildexpression.go
More file actions
1024 lines (865 loc) · 24 KB
/
buildexpression.go
File metadata and controls
1024 lines (865 loc) · 24 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package buildexpression
import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"time"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/internal/rtutils/ptr"
"github.com/ActiveState/cli/internal/sliceutils"
"github.com/ActiveState/cli/pkg/platform/api/buildplanner/model"
"github.com/go-openapi/strfmt"
)
const (
SolveFuncName = "solve"
SolveLegacyFuncName = "solve_legacy"
RequirementsKey = "requirements"
PlatformsKey = "platforms"
AtTimeKey = "at_time"
RequirementNameKey = "name"
RequirementNamespaceKey = "namespace"
RequirementVersionRequirementsKey = "version_requirements"
RequirementVersionKey = "version"
RequirementComparatorKey = "comparator"
ctxLet = "let"
ctxIn = "in"
ctxAp = "ap"
ctxValue = "value"
ctxAssignments = "assignments"
ctxIsAp = "isAp"
)
var funcNodeNotFoundError = errors.New("Could not find function node")
type BuildExpression struct {
Let *Let
Assignments []*Value
}
type Let struct {
// Let statements can be nested.
// Each let will contain its own assignments and an in statement.
Let *Let
Assignments []*Var
In *In
}
type Var struct {
Name string
Value *Value
}
type Value struct {
Ap *Ap
List *[]*Value
Str *string
Null *Null
Float *float64
Assignment *Var
Object *[]*Var
Ident *string
}
type Null struct {
Null string
}
type Ap struct {
Name string
Arguments []*Value
}
type In struct {
FuncCall *Ap
Name *string
}
type Fail struct {
Message string
}
// New creates a BuildExpression from a JSON byte array.
// The JSON must be a valid BuildExpression in the following format:
//
// {
// "let": {
// "runtime": {
// "solve_legacy": {
// "at_time": "2023-04-27T17:30:05.999000Z",
// "build_flags": [],
// "camel_flags": [],
// "platforms": [
// "96b7e6f2-bebf-564c-bc1c-f04482398f38"
// ],
// "requirements": [
// {
// "name": "requests",
// "namespace": "language/python"
// },
// {
// "name": "python",
// "namespace": "language",
// "version_requirements": [
// {
// "comparator": "eq",
// "version": "3.10.10"
// }
// ]
// },
// ],
// "solver_version": null
// }
// },
// "in": "$runtime"
// }
// }
func New(data []byte) (*BuildExpression, error) {
rawBuildExpression := make(map[string]interface{})
err := json.Unmarshal(data, &rawBuildExpression)
if err != nil {
return nil, errs.Wrap(err, "Could not unmarshal build expression")
}
if len(rawBuildExpression) != 1 {
return nil, errs.New("Build expression must have exactly one key")
}
expr := &BuildExpression{}
var path []string
for key, value := range rawBuildExpression {
switch v := value.(type) {
case map[string]interface{}:
// At this level the key must either be a let, an ap, or an assignment.
if key == "let" {
let, err := newLet(path, v)
if err != nil {
return nil, errs.Wrap(err, "Could not parse 'let' key")
}
expr.Let = let
} else if key == "fail" {
fail, err := newFail(path, v)
if err != nil {
return nil, errs.Wrap(err, "Could not parse 'fail' key")
}
return nil, locale.NewError("err_build_expression_fail", "", fail.Message)
} else if isAp(path, v) {
ap, err := newAp(path, v)
if err != nil {
return nil, errs.Wrap(err, "Could not parse '%s' key", key)
}
expr.Assignments = append(expr.Assignments, &Value{Ap: ap})
} else {
assignments, err := newAssignments(path, v)
if err != nil {
return nil, errs.Wrap(err, "Could not parse assignments")
}
expr.Assignments = append(expr.Assignments, &Value{Assignment: &Var{Name: key, Value: &Value{Object: &assignments}}})
}
default:
return nil, errs.New("Build expression's value must be a map[string]interface{}")
}
}
err = expr.validateRequirements()
if err != nil {
return nil, errs.Wrap(err, "Could not validate requirements")
}
return expr, nil
}
// NewEmpty creates a minimal, empty buildexpression.
func NewEmpty() (*BuildExpression, error) {
// At this time, there is no way to ask the Platform for an empty buildexpression, so build one
// manually.
expr, err := New([]byte(`
{
"let": {
"runtime": {
"solve_legacy": {
"at_time": "",
"build_flags": [],
"camel_flags": [],
"platforms": [],
"requirements": [],
"solver_version": null
}
},
"in": "$runtime"
}
}
`))
if err != nil {
return nil, errs.Wrap(err, "Unable to create initial buildexpression")
}
return expr, nil
}
func newLet(path []string, m map[string]interface{}) (*Let, error) {
path = append(path, ctxLet)
defer func() {
_, _, err := sliceutils.Pop(path)
if err != nil {
multilog.Error("Could not pop context: %v", err)
}
}()
inValue, ok := m["in"]
if !ok {
return nil, errs.New("Build expression's 'let' object has no 'in' key")
}
in, err := newIn(path, inValue)
if err != nil {
return nil, errs.Wrap(err, "Could not parse 'in' key's value: %v", inValue)
}
// Delete in so it doesn't get parsed as an assignment.
delete(m, "in")
result := &Let{In: in}
let, ok := m["let"]
if ok {
letMap, ok := let.(map[string]interface{})
if !ok {
return nil, errs.New("'let' key's value is not a map[string]interface{}")
}
l, err := newLet(path, letMap)
if err != nil {
return nil, errs.Wrap(err, "Could not parse 'let' key")
}
result.Let = l
// Delete let so it doesn't get parsed as an assignment.
delete(m, "let")
}
assignments, err := newAssignments(path, m)
if err != nil {
return nil, errs.Wrap(err, "Could not parse assignments")
}
result.Assignments = assignments
return result, nil
}
func isAp(path []string, value map[string]interface{}) bool {
path = append(path, ctxIsAp)
defer func() {
_, _, err := sliceutils.Pop(path)
if err != nil {
multilog.Error("Could not pop context: %v", err)
}
}()
_, hasIn := value["in"]
if hasIn && !sliceutils.Contains(path, ctxAssignments) {
return false
}
return true
}
func newValue(path []string, valueInterface interface{}) (*Value, error) {
path = append(path, ctxValue)
defer func() {
_, _, err := sliceutils.Pop(path)
if err != nil {
multilog.Error("Could not pop context: %v", err)
}
}()
value := &Value{}
switch v := valueInterface.(type) {
case map[string]interface{}:
// Examine keys first to see if this is a function call.
for key, val := range v {
if _, ok := val.(map[string]interface{}); !ok {
continue
}
// If the length of the value is greater than 1,
// then it's not a function call. It's an object
// and will be set as such outside the loop.
if len(v) > 1 {
continue
}
if isAp(path, val.(map[string]interface{})) {
f, err := newAp(path, v)
if err != nil {
return nil, errs.Wrap(err, "Could not parse '%s' function's value: %v", key, v)
}
value.Ap = f
}
}
if value.Ap == nil {
// It's not a function call, but an object.
object, err := newAssignments(path, v)
if err != nil {
return nil, errs.Wrap(err, "Could not parse object: %v", v)
}
value.Object = &object
}
case []interface{}:
values := []*Value{}
for _, item := range v {
value, err := newValue(path, item)
if err != nil {
return nil, errs.Wrap(err, "Could not parse list: %v", v)
}
values = append(values, value)
}
value.List = &values
case string:
if sliceutils.Contains(path, ctxIn) {
value.Ident = &v
} else {
value.Str = ptr.To(v)
}
case float64:
value.Float = ptr.To(v)
case nil:
// An empty value is interpreted as JSON null.
value.Null = &Null{}
default:
logging.Debug("Unknown type: %T at path %s", v, strings.Join(path, "."))
// An empty value is interpreted as JSON null.
value.Null = &Null{}
}
return value, nil
}
func newAp(path []string, m map[string]interface{}) (*Ap, error) {
path = append(path, ctxAp)
defer func() {
_, _, err := sliceutils.Pop(path)
if err != nil {
multilog.Error("Could not pop context: %v", err)
}
}()
// m is a mapping of function name to arguments. There should only be one
// set of arugments. Since the arguments are key-value pairs, it should be
// a map[string]interface{}.
if len(m) > 1 {
return nil, errs.New("Function call has more than one argument mapping")
}
// Look in the given object for the function's name and argument mapping.
var name string
var argsInterface interface{}
for key, value := range m {
_, ok := value.(map[string]interface{})
if !ok {
return nil, errs.New("Incorrect argument format")
}
name = key
argsInterface = value
}
args := []*Value{}
switch v := argsInterface.(type) {
case map[string]interface{}:
for key, valueInterface := range v {
value, err := newValue(path, valueInterface)
if err != nil {
return nil, errs.Wrap(err, "Could not parse '%s' function's argument '%s': %v", name, key, valueInterface)
}
args = append(args, &Value{Assignment: &Var{Name: key, Value: value}})
}
sort.SliceStable(args, func(i, j int) bool { return args[i].Assignment.Name < args[j].Assignment.Name })
case []interface{}:
for _, item := range v {
value, err := newValue(path, item)
if err != nil {
return nil, errs.Wrap(err, "Could not parse '%s' function's argument list item: %v", name, item)
}
args = append(args, value)
}
default:
return nil, errs.New("Function '%s' expected to be object or list", name)
}
return &Ap{Name: name, Arguments: args}, nil
}
func newAssignments(path []string, m map[string]interface{}) ([]*Var, error) {
path = append(path, ctxAssignments)
defer func() {
_, _, err := sliceutils.Pop(path)
if err != nil {
multilog.Error("Could not pop context: %v", err)
}
}()
assignments := []*Var{}
for key, valueInterface := range m {
value, err := newValue(path, valueInterface)
if err != nil {
return nil, errs.Wrap(err, "Could not parse '%s' key's value: %v", key, valueInterface)
}
assignments = append(assignments, &Var{Name: key, Value: value})
}
sort.SliceStable(assignments, func(i, j int) bool {
return assignments[i].Name < assignments[j].Name
})
return assignments, nil
}
func newIn(path []string, inValue interface{}) (*In, error) {
path = append(path, ctxIn)
defer func() {
_, _, err := sliceutils.Pop(path)
if err != nil {
multilog.Error("Could not pop context: %v", err)
}
}()
in := &In{}
switch v := inValue.(type) {
case map[string]interface{}:
f, err := newAp(path, v)
if err != nil {
return nil, errs.Wrap(err, "'in' object is not a function call")
}
in.FuncCall = f
case string:
in.Name = ptr.To(strings.TrimPrefix(v, "$"))
default:
return nil, errs.New("'in' value expected to be a function call or string")
}
return in, nil
}
func newFail(path []string, m map[string]interface{}) (*Fail, error) {
path = append(path, "fail")
defer func() {
_, _, err := sliceutils.Pop(path)
if err != nil {
multilog.Error("Could not pop context: %v", err)
}
}()
message, ok := m["message"]
if !ok {
return nil, errs.New("Build expression's 'fail' object has no 'message' key")
}
messageStr, ok := message.(string)
if !ok {
return nil, errs.New("'message' key's value is not a string")
}
return &Fail{Message: messageStr}, nil
}
// validateRequirements ensures that the requirements in the BuildExpression contain
// both the name and namespace fields. These fileds are used for requirement operations.
func (e *BuildExpression) validateRequirements() error {
requirements, err := e.getRequirementsNode()
if err != nil {
return errs.Wrap(err, "Could not get requirements node")
}
for _, r := range requirements {
if r.Object == nil {
continue
}
// The requirement object needs to have a name and value field.
// The value can be a string (in the case of name or namespace)
// or a list (in the case of version requirements).
for _, o := range *r.Object {
if o.Name == "" {
return errs.New("Requirement object missing name field")
}
if o.Value == nil {
return errs.New("Requirement object missing value field")
}
if o.Name == RequirementNameKey || o.Name == RequirementNamespaceKey {
if o.Value.Str == nil {
return errs.New("Requirement object value is not set to a string")
}
}
if o.Name == RequirementVersionRequirementsKey {
if o.Value.List == nil {
return errs.New("Requirement object value is not set to a list")
}
}
}
}
return nil
}
// Requirements returns the requirements in the BuildExpression.
// It returns an error if the requirements are not found or if they are malformed.
// It expects the JSON representation of the solve node to be formatted as follows:
//
// {
// "requirements": [
// {
// "name": "requests",
// "namespace": "language/python"
// },
// {
// "name": "python",
// "namespace": "language",
// "version_requirements": [{
// "comparator": "eq",
// "version": "3.10.10"
// }]
// }
// ]
// }
func (e *BuildExpression) Requirements() ([]model.Requirement, error) {
requirementsNode, err := e.getRequirementsNode()
if err != nil {
return nil, errs.Wrap(err, "Could not get requirements node")
}
var requirements []model.Requirement
for _, r := range requirementsNode {
if r.Object == nil {
continue
}
var req model.Requirement
for _, o := range *r.Object {
if o.Name == RequirementNameKey {
req.Name = *o.Value.Str
}
if o.Name == RequirementNamespaceKey {
req.Namespace = *o.Value.Str
}
if o.Name == RequirementVersionRequirementsKey {
req.VersionRequirement = getVersionRequirements(o.Value.List)
}
}
requirements = append(requirements, req)
}
return requirements, nil
}
func (e *BuildExpression) getRequirementsNode() ([]*Value, error) {
solveAp, err := e.getSolveNode()
if err != nil {
return nil, errs.Wrap(err, "Could not get solve node")
}
var reqs []*Value
for _, arg := range solveAp.Arguments {
if arg.Assignment == nil {
continue
}
if arg.Assignment.Name == RequirementsKey && arg.Assignment.Value != nil {
reqs = *arg.Assignment.Value.List
}
}
return reqs, nil
}
func getVersionRequirements(v *[]*Value) []model.VersionRequirement {
var reqs []model.VersionRequirement
if v == nil {
return reqs
}
for _, r := range *v {
if r.Object == nil {
continue
}
versionReq := make(model.VersionRequirement)
for _, o := range *r.Object {
if o.Name == RequirementComparatorKey {
versionReq[RequirementComparatorKey] = *o.Value.Str
}
if o.Name == RequirementVersionKey {
versionReq[RequirementVersionKey] = *o.Value.Str
}
}
reqs = append(reqs, versionReq)
}
return reqs
}
// getSolveNode returns the solve node from the build expression.
// It returns an error if the solve node is not found.
// Currently, the solve node can have the name of "solve" or "solve_legacy".
// It expects the JSON representation of the build expression to be formatted as follows:
//
// {
// "let": {
// "runtime": {
// "solve": {
// }
// }
// }
// }
func (e *BuildExpression) getSolveNode() (*Ap, error) {
// First, try to find the solve node via lets.
if e.Let != nil {
solveAp, err := recurseLets(e.Let)
if err != nil {
return nil, errs.Wrap(err, "Could not recurse lets")
}
return solveAp, nil
}
// Search for solve node in the top level assignments.
for _, a := range e.Assignments {
if a.Assignment == nil {
continue
}
if a.Assignment.Name == "" {
continue
}
if a.Assignment.Value == nil {
continue
}
if a.Assignment.Value.Ap == nil {
continue
}
if a.Assignment.Value.Ap.Name == SolveFuncName || a.Assignment.Value.Ap.Name == SolveLegacyFuncName {
return a.Assignment.Value.Ap, nil
}
}
return nil, funcNodeNotFoundError
}
// recurseLets recursively searches for the solve node in the let statements.
// The solve node is specified by the name "runtime" and the function name "solve"
// or "solve_legacy".
func recurseLets(let *Let) (*Ap, error) {
for _, a := range let.Assignments {
if a.Value == nil {
continue
}
if a.Value.Ap == nil {
continue
}
if a.Name == "" {
continue
}
if a.Value.Ap.Name == SolveFuncName || a.Value.Ap.Name == SolveLegacyFuncName {
return a.Value.Ap, nil
}
}
// The highest level solve node is not found, so recurse into the next let.
if let.Let != nil {
return recurseLets(let.Let)
}
return nil, funcNodeNotFoundError
}
func (e *BuildExpression) getSolveNodeArguments() ([]*Value, error) {
solveAp, err := e.getSolveNode()
if err != nil {
return nil, errs.Wrap(err, "Could not get solve node")
}
return solveAp.Arguments, nil
}
func (e *BuildExpression) getPlatformsNode() (*[]*Value, error) {
solveAp, err := e.getSolveNode()
if err != nil {
return nil, errs.Wrap(err, "Could not get solve node")
}
for _, arg := range solveAp.Arguments {
if arg.Assignment == nil {
continue
}
if arg.Assignment.Name == PlatformsKey && arg.Assignment.Value != nil {
return arg.Assignment.Value.List, nil
}
}
return nil, errs.New("Could not find platforms node")
}
// Update updates the BuildExpression's requirements based on the operation and requirement.
func (e *BuildExpression) UpdateRequirement(operation model.Operation, requirement model.Requirement) error {
var err error
switch operation {
case model.OperationAdded:
err = e.addRequirement(requirement)
case model.OperationRemoved:
err = e.removeRequirement(requirement)
case model.OperationUpdated:
err = e.removeRequirement(requirement)
if err != nil {
break
}
err = e.addRequirement(requirement)
default:
return errs.New("Unsupported operation")
}
if err != nil {
return errs.Wrap(err, "Could not update BuildExpression's requirements")
}
return nil
}
func (e *BuildExpression) addRequirement(requirement model.Requirement) error {
obj := []*Var{
{Name: RequirementNameKey, Value: &Value{Str: ptr.To(requirement.Name)}},
{Name: RequirementNamespaceKey, Value: &Value{Str: ptr.To(requirement.Namespace)}},
}
if requirement.VersionRequirement != nil {
for _, r := range requirement.VersionRequirement {
obj = append(obj, &Var{Name: RequirementVersionRequirementsKey, Value: &Value{List: &[]*Value{
{Object: &[]*Var{
{Name: RequirementComparatorKey, Value: &Value{Str: ptr.To(r[RequirementComparatorKey])}},
{Name: RequirementVersionKey, Value: &Value{Str: ptr.To(r[RequirementVersionKey])}},
}}},
}})
}
}
requirementsNode, err := e.getRequirementsNode()
if err != nil {
return errs.Wrap(err, "Could not get requirements node")
}
requirementsNode = append(requirementsNode, &Value{Object: &obj})
arguments, err := e.getSolveNodeArguments()
if err != nil {
return errs.Wrap(err, "Could not get solve node arguments")
}
for _, arg := range arguments {
if arg.Assignment == nil {
continue
}
if arg.Assignment.Name == RequirementsKey {
arg.Assignment.Value.List = &requirementsNode
}
}
return nil
}
type RequirementNotFoundError struct {
Name string
*locale.LocalizedError // for legacy non-user-facing error usages
}
func (e *BuildExpression) removeRequirement(requirement model.Requirement) error {
requirementsNode, err := e.getRequirementsNode()
if err != nil {
return errs.Wrap(err, "Could not get requirements node")
}
var found bool
for i, r := range requirementsNode {
if r.Object == nil {
continue
}
for _, o := range *r.Object {
if o.Name == RequirementNameKey && *o.Value.Str == requirement.Name {
requirementsNode = append(requirementsNode[:i], requirementsNode[i+1:]...)
found = true
break
}
}
}
if !found {
return &RequirementNotFoundError{
requirement.Name,
locale.NewInputError("err_remove_requirement_not_found", "", requirement.Name),
}
}
solveNode, err := e.getSolveNode()
if err != nil {
return errs.Wrap(err, "Could not get solve node")
}
for _, arg := range solveNode.Arguments {
if arg.Assignment == nil {
continue
}
if arg.Assignment.Name == RequirementsKey {
arg.Assignment.Value.List = &requirementsNode
}
}
return nil
}
func (e *BuildExpression) UpdatePlatform(operation model.Operation, platformID strfmt.UUID) error {
var err error
switch operation {
case model.OperationAdded:
err = e.addPlatform(platformID)
case model.OperationRemoved:
err = e.removePlatform(platformID)
default:
return errs.New("Unsupported operation")
}
if err != nil {
return errs.Wrap(err, "Could not update BuildExpression's platform")
}
return nil
}
func (e *BuildExpression) addPlatform(platformID strfmt.UUID) error {
platformsNode, err := e.getPlatformsNode()
if err != nil {
return errs.Wrap(err, "Could not get platforms node")
}
*platformsNode = append(*platformsNode, &Value{Str: ptr.To(platformID.String())})
return nil
}
func (e *BuildExpression) removePlatform(platformID strfmt.UUID) error {
platformsNode, err := e.getPlatformsNode()
if err != nil {
return errs.Wrap(err, "Could not get platforms node")
}
var found bool
for i, p := range *platformsNode {
if p.Str == nil {
continue
}
if *p.Str == platformID.String() {
*platformsNode = append((*platformsNode)[:i], (*platformsNode)[i+1:]...)
found = true
break
}
}
if !found {
return errs.New("Could not find platform")
}
return nil
}
func (e *BuildExpression) UpdateTimestamp(timestamp strfmt.DateTime) error {
formatted, err := time.Parse(time.RFC3339, timestamp.String())
if err != nil {
return errs.Wrap(err, "Could not parse latest timestamp")
}
solveNode, err := e.getSolveNode()
if err != nil {
return errs.Wrap(err, "Could not get solve node")
}
for _, arg := range solveNode.Arguments {
if arg.Assignment == nil {
continue
}
if arg.Assignment.Name == "at_time" {
arg.Assignment.Value.Str = ptr.To(formatted.Format(time.RFC3339))
}
}
return nil
}
func (e *BuildExpression) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{})
if e.Let != nil {
m["let"] = e.Let
}
for _, value := range e.Assignments {
if value.Assignment == nil {
continue
}
m[value.Assignment.Name] = value
}
return json.Marshal(m)
}
func (l *Let) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{})
if l.Let != nil {
m["let"] = l.Let
}
for _, v := range l.Assignments {
if v.Value == nil {
continue
}
m[v.Name] = v.Value
}
m["in"] = l.In
return json.Marshal(m)
}
func (a *Var) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{})
m[a.Name] = a.Value
return json.Marshal(m)
}
func (v *Value) MarshalJSON() ([]byte, error) {
switch {
case v.Ap != nil:
return json.Marshal(v.Ap)
case v.List != nil:
return json.Marshal(v.List)
case v.Str != nil:
return json.Marshal(strings.Trim(*v.Str, `"`))
case v.Null != nil:
return json.Marshal(nil)
case v.Assignment != nil:
return json.Marshal(v.Assignment)
case v.Float != nil:
return json.Marshal(*v.Float)
case v.Object != nil:
m := make(map[string]interface{})
for _, assignment := range *v.Object {
m[assignment.Name] = assignment.Value
}
return json.Marshal(m)
case v.Ident != nil:
return json.Marshal(v.Ident)
}
return json.Marshal([]*Value{})
}