github.com/mweagle/Sparta@v1.15.0/aws/step/state_operators.go (about)

     1  package step
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  )
     7  
     8  /*******************************************************************************
     9     ___ ___  __  __ ___  _   ___ ___ ___  ___  _  _ ___
    10    / __/ _ \|  \/  | _ \/_\ | _ \_ _/ __|/ _ \| \| / __|
    11   | (_| (_) | |\/| |  _/ _ \|   /| |\__ \ (_) | .` \__ \
    12    \___\___/|_|  |_|_|/_/ \_\_|_\___|___/\___/|_|\_|___/
    13  
    14  /******************************************************************************/
    15  
    16  // Comparison is the generic comparison operator interface
    17  type Comparison interface {
    18  	json.Marshaler
    19  }
    20  
    21  // ChoiceBranch represents a type for a ChoiceState "Choices" entry
    22  type ChoiceBranch interface {
    23  	nextState() MachineState
    24  }
    25  
    26  ////////////////////////////////////////////////////////////////////////////////
    27  // StringEquals
    28  ////////////////////////////////////////////////////////////////////////////////
    29  
    30  /**
    31  
    32  Validations
    33  	- JSONPath: https://github.com/NodePrime/jsonpath
    34  	- Choices lead to existing states
    35  	- Choice statenames are scoped to same depth
    36  */
    37  
    38  // StringEquals comparison
    39  type StringEquals struct {
    40  	Comparison
    41  	Variable string
    42  	Value    string
    43  }
    44  
    45  // MarshalJSON for custom marshalling
    46  func (cmp *StringEquals) MarshalJSON() ([]byte, error) {
    47  	return json.Marshal(&struct {
    48  		Variable     string
    49  		StringEquals string
    50  	}{
    51  		Variable:     cmp.Variable,
    52  		StringEquals: cmp.Value,
    53  	})
    54  }
    55  
    56  ////////////////////////////////////////////////////////////////////////////////
    57  // StringLessThan
    58  ////////////////////////////////////////////////////////////////////////////////
    59  
    60  // StringLessThan comparison
    61  type StringLessThan struct {
    62  	Comparison
    63  	Variable string
    64  	Value    string
    65  }
    66  
    67  // MarshalJSON for custom marshalling
    68  func (cmp *StringLessThan) MarshalJSON() ([]byte, error) {
    69  	return json.Marshal(&struct {
    70  		Variable       string
    71  		StringLessThan string
    72  	}{
    73  		Variable:       cmp.Variable,
    74  		StringLessThan: cmp.Value,
    75  	})
    76  }
    77  
    78  ////////////////////////////////////////////////////////////////////////////////
    79  // StringGreaterThan
    80  ////////////////////////////////////////////////////////////////////////////////
    81  
    82  // StringGreaterThan comparison
    83  type StringGreaterThan struct {
    84  	Comparison
    85  	Variable string
    86  	Value    string
    87  }
    88  
    89  // MarshalJSON for custom marshalling
    90  func (cmp *StringGreaterThan) MarshalJSON() ([]byte, error) {
    91  	return json.Marshal(&struct {
    92  		Variable          string
    93  		StringGreaterThan string
    94  	}{
    95  		Variable:          cmp.Variable,
    96  		StringGreaterThan: cmp.Value,
    97  	})
    98  }
    99  
   100  ////////////////////////////////////////////////////////////////////////////////
   101  // StringLessThanEquals
   102  ////////////////////////////////////////////////////////////////////////////////
   103  
   104  // StringLessThanEquals comparison
   105  type StringLessThanEquals struct {
   106  	Variable string
   107  	Value    string
   108  }
   109  
   110  // MarshalJSON for custom marshalling
   111  func (cmp *StringLessThanEquals) MarshalJSON() ([]byte, error) {
   112  	return json.Marshal(&struct {
   113  		Variable             string
   114  		StringLessThanEquals string
   115  	}{
   116  		Variable:             cmp.Variable,
   117  		StringLessThanEquals: cmp.Value,
   118  	})
   119  }
   120  
   121  ////////////////////////////////////////////////////////////////////////////////
   122  // StringGreaterThanEquals
   123  ////////////////////////////////////////////////////////////////////////////////
   124  
   125  // StringGreaterThanEquals comparison
   126  type StringGreaterThanEquals struct {
   127  	Comparison
   128  	Variable string
   129  	Value    string
   130  }
   131  
   132  // MarshalJSON for custom marshalling
   133  func (cmp *StringGreaterThanEquals) MarshalJSON() ([]byte, error) {
   134  	return json.Marshal(&struct {
   135  		Variable                string
   136  		StringGreaterThanEquals string
   137  	}{
   138  		Variable:                cmp.Variable,
   139  		StringGreaterThanEquals: cmp.Value,
   140  	})
   141  }
   142  
   143  ////////////////////////////////////////////////////////////////////////////////
   144  // NumericEquals
   145  ////////////////////////////////////////////////////////////////////////////////
   146  
   147  // NumericEquals comparison
   148  type NumericEquals struct {
   149  	Comparison
   150  	Variable string
   151  	Value    int64
   152  }
   153  
   154  // MarshalJSON for custom marshalling
   155  func (cmp *NumericEquals) MarshalJSON() ([]byte, error) {
   156  	return json.Marshal(&struct {
   157  		Variable      string
   158  		NumericEquals int64
   159  	}{
   160  		Variable:      cmp.Variable,
   161  		NumericEquals: cmp.Value,
   162  	})
   163  }
   164  
   165  ////////////////////////////////////////////////////////////////////////////////
   166  // NumericLessThan
   167  ////////////////////////////////////////////////////////////////////////////////
   168  
   169  // NumericLessThan comparison
   170  type NumericLessThan struct {
   171  	Comparison
   172  	Variable string
   173  	Value    int64
   174  }
   175  
   176  // MarshalJSON for custom marshalling
   177  func (cmp *NumericLessThan) MarshalJSON() ([]byte, error) {
   178  	return json.Marshal(&struct {
   179  		Variable        string
   180  		NumericLessThan int64
   181  	}{
   182  		Variable:        cmp.Variable,
   183  		NumericLessThan: cmp.Value,
   184  	})
   185  }
   186  
   187  ////////////////////////////////////////////////////////////////////////////////
   188  // NumericGreaterThan
   189  ////////////////////////////////////////////////////////////////////////////////
   190  
   191  // NumericGreaterThan comparison
   192  type NumericGreaterThan struct {
   193  	Comparison
   194  	Variable string
   195  	Value    int64
   196  }
   197  
   198  // MarshalJSON for custom marshalling
   199  func (cmp *NumericGreaterThan) MarshalJSON() ([]byte, error) {
   200  	return json.Marshal(&struct {
   201  		Variable           string
   202  		NumericGreaterThan int64
   203  	}{
   204  		Variable:           cmp.Variable,
   205  		NumericGreaterThan: cmp.Value,
   206  	})
   207  }
   208  
   209  ////////////////////////////////////////////////////////////////////////////////
   210  // NumericLessThanEquals
   211  ////////////////////////////////////////////////////////////////////////////////
   212  
   213  // NumericLessThanEquals comparison
   214  type NumericLessThanEquals struct {
   215  	Comparison
   216  	Variable string
   217  	Value    int64
   218  }
   219  
   220  // MarshalJSON for custom marshalling
   221  func (cmp *NumericLessThanEquals) MarshalJSON() ([]byte, error) {
   222  	return json.Marshal(&struct {
   223  		Variable              string
   224  		NumericLessThanEquals int64
   225  	}{
   226  		Variable:              cmp.Variable,
   227  		NumericLessThanEquals: cmp.Value,
   228  	})
   229  }
   230  
   231  ////////////////////////////////////////////////////////////////////////////////
   232  // NumericGreaterThanEquals
   233  ////////////////////////////////////////////////////////////////////////////////
   234  
   235  // NumericGreaterThanEquals comparison
   236  type NumericGreaterThanEquals struct {
   237  	Comparison
   238  	Variable string
   239  	Value    int64
   240  }
   241  
   242  // MarshalJSON for custom marshalling
   243  func (cmp *NumericGreaterThanEquals) MarshalJSON() ([]byte, error) {
   244  	return json.Marshal(&struct {
   245  		Variable                 string
   246  		NumericGreaterThanEquals int64
   247  	}{
   248  		Variable:                 cmp.Variable,
   249  		NumericGreaterThanEquals: cmp.Value,
   250  	})
   251  }
   252  
   253  ////////////////////////////////////////////////////////////////////////////////
   254  // BooleanEquals
   255  ////////////////////////////////////////////////////////////////////////////////
   256  
   257  // BooleanEquals comparison
   258  type BooleanEquals struct {
   259  	Comparison
   260  	Variable string
   261  	Value    interface{}
   262  }
   263  
   264  // MarshalJSON for custom marshalling
   265  func (cmp *BooleanEquals) MarshalJSON() ([]byte, error) {
   266  	return json.Marshal(&struct {
   267  		Variable      string
   268  		BooleanEquals interface{}
   269  	}{
   270  		Variable:      cmp.Variable,
   271  		BooleanEquals: cmp.Value,
   272  	})
   273  }
   274  
   275  ////////////////////////////////////////////////////////////////////////////////
   276  // TimestampEquals
   277  ////////////////////////////////////////////////////////////////////////////////
   278  
   279  // TimestampEquals comparison
   280  type TimestampEquals struct {
   281  	Comparison
   282  	Variable string
   283  	Value    time.Time
   284  }
   285  
   286  // MarshalJSON for custom marshalling
   287  func (cmp *TimestampEquals) MarshalJSON() ([]byte, error) {
   288  	return json.Marshal(&struct {
   289  		Variable        string
   290  		TimestampEquals string
   291  	}{
   292  		Variable:        cmp.Variable,
   293  		TimestampEquals: cmp.Value.Format(time.RFC3339),
   294  	})
   295  }
   296  
   297  ////////////////////////////////////////////////////////////////////////////////
   298  // TimestampLessThan
   299  ////////////////////////////////////////////////////////////////////////////////
   300  
   301  // TimestampLessThan comparison
   302  type TimestampLessThan struct {
   303  	Comparison
   304  	Variable string
   305  	Value    time.Time
   306  }
   307  
   308  // MarshalJSON for custom marshalling
   309  func (cmp *TimestampLessThan) MarshalJSON() ([]byte, error) {
   310  	return json.Marshal(&struct {
   311  		Variable          string
   312  		TimestampLessThan string
   313  	}{
   314  		Variable:          cmp.Variable,
   315  		TimestampLessThan: cmp.Value.Format(time.RFC3339),
   316  	})
   317  }
   318  
   319  ////////////////////////////////////////////////////////////////////////////////
   320  // TimestampGreaterThan
   321  ////////////////////////////////////////////////////////////////////////////////
   322  
   323  // TimestampGreaterThan comparison
   324  type TimestampGreaterThan struct {
   325  	Variable string
   326  	Value    time.Time
   327  }
   328  
   329  // MarshalJSON for custom marshalling
   330  func (cmp *TimestampGreaterThan) MarshalJSON() ([]byte, error) {
   331  	return json.Marshal(&struct {
   332  		Variable             string
   333  		TimestampGreaterThan string
   334  	}{
   335  		Variable:             cmp.Variable,
   336  		TimestampGreaterThan: cmp.Value.Format(time.RFC3339),
   337  	})
   338  }
   339  
   340  ////////////////////////////////////////////////////////////////////////////////
   341  // TimestampLessThanEquals
   342  ////////////////////////////////////////////////////////////////////////////////
   343  
   344  // TimestampLessThanEquals comparison
   345  type TimestampLessThanEquals struct {
   346  	Comparison
   347  	Variable string
   348  	Value    time.Time
   349  }
   350  
   351  // MarshalJSON for custom marshalling
   352  func (cmp *TimestampLessThanEquals) MarshalJSON() ([]byte, error) {
   353  	return json.Marshal(&struct {
   354  		Variable                string
   355  		TimestampLessThanEquals string
   356  	}{
   357  		Variable:                cmp.Variable,
   358  		TimestampLessThanEquals: cmp.Value.Format(time.RFC3339),
   359  	})
   360  }
   361  
   362  ////////////////////////////////////////////////////////////////////////////////
   363  // TimestampGreaterThanEquals
   364  ////////////////////////////////////////////////////////////////////////////////
   365  
   366  // TimestampGreaterThanEquals comparison
   367  type TimestampGreaterThanEquals struct {
   368  	Comparison
   369  	Variable string
   370  	Value    time.Time
   371  }
   372  
   373  // MarshalJSON for custom marshalling
   374  func (cmp *TimestampGreaterThanEquals) MarshalJSON() ([]byte, error) {
   375  	return json.Marshal(&struct {
   376  		Variable                   string
   377  		TimestampGreaterThanEquals string
   378  	}{
   379  		Variable:                   cmp.Variable,
   380  		TimestampGreaterThanEquals: cmp.Value.Format(time.RFC3339),
   381  	})
   382  }
   383  
   384  /*******************************************************************************
   385     ___  ___ ___ ___    _ _____ ___  ___  ___
   386    / _ \| _ \ __| _ \  /_\_   _/ _ \| _ \/ __|
   387   | (_) |  _/ _||   / / _ \| || (_) |   /\__ \
   388    \___/|_| |___|_|_\/_/ \_\_| \___/|_|_\|___/
   389  /******************************************************************************/
   390  
   391  ////////////////////////////////////////////////////////////////////////////////
   392  // And
   393  ////////////////////////////////////////////////////////////////////////////////
   394  
   395  // And operator
   396  type And struct {
   397  	ChoiceBranch
   398  	Comparison []Comparison
   399  	Next       MachineState
   400  }
   401  
   402  func (andOperation *And) nextState() MachineState {
   403  	return andOperation.Next
   404  }
   405  
   406  // MarshalJSON for custom marshalling
   407  func (andOperation *And) MarshalJSON() ([]byte, error) {
   408  	return json.Marshal(&struct {
   409  		Comparison []Comparison `json:"And,omitempty"`
   410  		Next       string       `json:",omitempty"`
   411  	}{
   412  		Comparison: andOperation.Comparison,
   413  		Next:       andOperation.Next.Name(),
   414  	})
   415  }
   416  
   417  ////////////////////////////////////////////////////////////////////////////////
   418  // Or
   419  ////////////////////////////////////////////////////////////////////////////////
   420  
   421  // Or operator
   422  type Or struct {
   423  	ChoiceBranch
   424  	Comparison []Comparison
   425  	Next       MachineState
   426  }
   427  
   428  func (orOperation *Or) nextState() MachineState {
   429  	return orOperation.Next
   430  }
   431  
   432  // MarshalJSON for custom marshalling
   433  func (orOperation *Or) MarshalJSON() ([]byte, error) {
   434  	return json.Marshal(&struct {
   435  		Comparison []Comparison `json:"Or,omitempty"`
   436  		Next       string       `json:",omitempty"`
   437  	}{
   438  		Comparison: orOperation.Comparison,
   439  		Next:       orOperation.Next.Name(),
   440  	})
   441  }
   442  
   443  ////////////////////////////////////////////////////////////////////////////////
   444  // Not
   445  ////////////////////////////////////////////////////////////////////////////////
   446  
   447  // Not operator
   448  type Not struct {
   449  	ChoiceBranch
   450  	Comparison Comparison
   451  	Next       MachineState
   452  }
   453  
   454  func (notOperation *Not) nextState() MachineState {
   455  	return notOperation.Next
   456  }
   457  
   458  // MarshalJSON for custom marshalling
   459  func (notOperation *Not) MarshalJSON() ([]byte, error) {
   460  	return json.Marshal(&struct {
   461  		Not  Comparison
   462  		Next string
   463  	}{
   464  		Not:  notOperation.Comparison,
   465  		Next: notOperation.Next.Name(),
   466  	})
   467  }