Skip to content

Commit

Permalink
Export QueryStruct (#2160)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak authored Oct 31, 2023
1 parent dbb7c91 commit 5148be4
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 99 deletions.
14 changes: 7 additions & 7 deletions pkg/sdk/network_policies_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import g "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/gen
//go:generate go run ./poc/main.go

var (
ip = g.QueryStruct("IP").
ip = g.NewQueryStruct("IP").
Text("IP", g.KeywordOptions().SingleQuotes().Required())

NetworkPoliciesDef = g.NewInterface(
Expand All @@ -15,7 +15,7 @@ var (
).
CreateOperation(
"https://docs.snowflake.com/en/sql-reference/sql/create-network-policy",
g.QueryStruct("CreateNetworkPolicies").
g.NewQueryStruct("CreateNetworkPolicies").
Create().
OrReplace().
SQL("NETWORK POLICY").
Expand All @@ -27,14 +27,14 @@ var (
).
AlterOperation(
"https://docs.snowflake.com/en/sql-reference/sql/alter-network-policy",
g.QueryStruct("AlterNetworkPolicy").
g.NewQueryStruct("AlterNetworkPolicy").
Alter().
SQL("NETWORK POLICY").
IfExists().
Name().
OptionalQueryStructField(
"Set",
g.QueryStruct("NetworkPolicySet").
g.NewQueryStruct("NetworkPolicySet").
ListQueryStructField("AllowedIpList", ip, g.ParameterOptions().SQL("ALLOWED_IP_LIST").Parentheses()).
ListQueryStructField("BlockedIpList", ip, g.ParameterOptions().SQL("BLOCKED_IP_LIST").Parentheses()).
OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()).
Expand All @@ -49,7 +49,7 @@ var (
).
DropOperation(
"https://docs.snowflake.com/en/sql-reference/sql/drop-network-policy",
g.QueryStruct("DropNetworkPolicy").
g.NewQueryStruct("DropNetworkPolicy").
Drop().
SQL("NETWORK POLICY").
IfExists().
Expand All @@ -70,7 +70,7 @@ var (
Field("Comment", "string").
Field("EntriesInAllowedIpList", "int").
Field("EntriesInBlockedIpList", "int"),
g.QueryStruct("ShowNetworkPolicies").
g.NewQueryStruct("ShowNetworkPolicies").
Show().
SQL("NETWORK POLICIES"),
).
Expand All @@ -83,7 +83,7 @@ var (
g.PlainStruct("NetworkPolicyDescription").
Field("Name", "string").
Field("Value", "string"),
g.QueryStruct("DescribeNetworkPolicy").
g.NewQueryStruct("DescribeNetworkPolicy").
Describe().
SQL("NETWORK POLICY").
Name().
Expand Down
12 changes: 6 additions & 6 deletions pkg/sdk/poc/example/database_role_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import (
//go:generate go run ../main.go

var (
dbRoleRename = g.QueryStruct("DatabaseRoleRename").
dbRoleRename = g.NewQueryStruct("DatabaseRoleRename").
// Fields
Identifier("Name", g.KindOfT[DatabaseObjectIdentifier](), g.IdentifierOptions().Required()).
// Validations
WithValidation(g.ValidIdentifier, "Name")

nestedThirdLevel = g.QueryStruct("NestedThirdLevel").
nestedThirdLevel = g.NewQueryStruct("NestedThirdLevel").
// Fields
Identifier("Field", g.KindOfT[DatabaseObjectIdentifier](), g.IdentifierOptions().Required()).
// Validations
WithValidation(g.AtLeastOneValueSet, "Field")

dbRoleSet = g.QueryStruct("DatabaseRoleSet").
dbRoleSet = g.NewQueryStruct("DatabaseRoleSet").
// Fields
TextAssignment("COMMENT", g.ParameterOptions().SingleQuotes().Required()).
OptionalQueryStructField("NestedThirdLevel", nestedThirdLevel, g.ListOptions().NoParentheses().SQL("NESTED"))

dbRoleUnset = g.QueryStruct("DatabaseRoleUnset").
dbRoleUnset = g.NewQueryStruct("DatabaseRoleUnset").
// Fields
OptionalSQL("COMMENT").
// Validations
Expand All @@ -37,7 +37,7 @@ var (
).
CreateOperation(
"https://docs.snowflake.com/en/sql-reference/sql/create-database-role",
g.QueryStruct("CreateDatabaseRole").
g.NewQueryStruct("CreateDatabaseRole").
// Fields
Create().
OrReplace().
Expand All @@ -51,7 +51,7 @@ var (
).
AlterOperation(
"https://docs.snowflake.com/en/sql-reference/sql/alter-database-role",
g.QueryStruct("AlterDatabaseRole").
g.NewQueryStruct("AlterDatabaseRole").
// Fields
Alter().
SQL("DATABASE ROLE").
Expand Down
6 changes: 3 additions & 3 deletions pkg/sdk/poc/generator/identifier_builders.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package generator

// Name adds identifier with field name "name" and type will be inferred from interface definition
func (v *queryStruct) Name() *queryStruct {
func (v *QueryStruct) Name() *QueryStruct {
identifier := NewField("name", "<will be replaced>", Tags().Identifier(), IdentifierOptions().Required())
v.identifierField = identifier
v.fields = append(v.fields, identifier)
return v
}

func (v *queryStruct) Identifier(fieldName string, kind string, transformer *IdentifierTransformer) *queryStruct {
func (v *QueryStruct) Identifier(fieldName string, kind string, transformer *IdentifierTransformer) *QueryStruct {
v.fields = append(v.fields, NewField(fieldName, kind, Tags().Identifier(), transformer))
return v
}

func (v *queryStruct) OptionalIdentifier(name string, kind string, transformer *IdentifierTransformer) *queryStruct {
func (v *QueryStruct) OptionalIdentifier(name string, kind string, transformer *IdentifierTransformer) *QueryStruct {
if len(kind) > 0 && kind[0] != '*' {
kind = KindOfPointer(kind)
}
Expand Down
36 changes: 18 additions & 18 deletions pkg/sdk/poc/generator/keyword_builders.go
Original file line number Diff line number Diff line change
@@ -1,87 +1,87 @@
package generator

func (v *queryStruct) OptionalSQL(sql string) *queryStruct {
func (v *QueryStruct) OptionalSQL(sql string) *QueryStruct {
v.fields = append(v.fields, NewField(sqlToFieldName(sql, true), "*bool", Tags().Keyword().SQL(sql), nil))
return v
}

func (v *queryStruct) OrReplace() *queryStruct {
func (v *QueryStruct) OrReplace() *QueryStruct {
return v.OptionalSQL("OR REPLACE")
}

func (v *queryStruct) IfNotExists() *queryStruct {
func (v *QueryStruct) IfNotExists() *QueryStruct {
return v.OptionalSQL("IF NOT EXISTS")
}

func (v *queryStruct) IfExists() *queryStruct {
func (v *QueryStruct) IfExists() *QueryStruct {
return v.OptionalSQL("IF EXISTS")
}

func (v *queryStruct) Terse() *queryStruct {
func (v *QueryStruct) Terse() *QueryStruct {
return v.OptionalSQL("TERSE")
}

func (v *queryStruct) Text(name string, transformer *KeywordTransformer) *queryStruct {
func (v *QueryStruct) Text(name string, transformer *KeywordTransformer) *QueryStruct {
v.fields = append(v.fields, NewField(name, "string", Tags().Keyword(), transformer))
return v
}

func (v *queryStruct) OptionalText(name string, transformer *KeywordTransformer) *queryStruct {
func (v *QueryStruct) OptionalText(name string, transformer *KeywordTransformer) *QueryStruct {
v.fields = append(v.fields, NewField(name, "*string", Tags().Keyword(), transformer))
return v
}

// SessionParameters *SessionParameters `ddl:"list,no_parentheses"`
func (v *queryStruct) SessionParameters() *queryStruct {
func (v *QueryStruct) SessionParameters() *QueryStruct {
v.fields = append(v.fields, NewField("SessionParameters", "*SessionParameters", Tags().List().NoParentheses(), nil).withValidations(NewValidation(ValidateValue, "SessionParameters")))
return v
}

func (v *queryStruct) OptionalSessionParameters() *queryStruct {
func (v *QueryStruct) OptionalSessionParameters() *QueryStruct {
v.fields = append(v.fields, NewField("SessionParameters", "*SessionParameters", Tags().List().NoParentheses(), nil).withValidations(NewValidation(ValidateValue, "SessionParameters")))
return v
}

func (v *queryStruct) OptionalSessionParametersUnset() *queryStruct {
func (v *QueryStruct) OptionalSessionParametersUnset() *QueryStruct {
v.fields = append(v.fields, NewField("SessionParametersUnset", "*SessionParametersUnset", Tags().List().NoParentheses(), nil).withValidations(NewValidation(ValidateValue, "SessionParametersUnset")))
return v
}

func (v *queryStruct) WithTags() *queryStruct {
func (v *QueryStruct) WithTags() *QueryStruct {
v.fields = append(v.fields, NewField("Tag", "[]TagAssociation", Tags().Keyword().Parentheses().SQL("TAG"), nil))
return v
}

func (v *queryStruct) SetTags() *queryStruct {
func (v *QueryStruct) SetTags() *QueryStruct {
v.fields = append(v.fields, NewField("SetTags", "[]TagAssociation", Tags().Keyword().SQL("SET TAG"), nil))
return v
}

func (v *queryStruct) UnsetTags() *queryStruct {
func (v *QueryStruct) UnsetTags() *QueryStruct {
v.fields = append(v.fields, NewField("UnsetTags", "[]ObjectIdentifier", Tags().Keyword().SQL("UNSET TAG"), nil))
return v
}

func (v *queryStruct) OptionalLike() *queryStruct {
func (v *QueryStruct) OptionalLike() *QueryStruct {
v.fields = append(v.fields, NewField("Like", "*Like", Tags().Keyword().SQL("LIKE"), nil))
return v
}

func (v *queryStruct) OptionalIn() *queryStruct {
func (v *QueryStruct) OptionalIn() *QueryStruct {
v.fields = append(v.fields, NewField("In", "*In", Tags().Keyword().SQL("IN"), nil))
return v
}

func (v *queryStruct) OptionalStartsWith() *queryStruct {
func (v *QueryStruct) OptionalStartsWith() *QueryStruct {
v.fields = append(v.fields, NewField("StartsWith", "*string", Tags().Parameter().NoEquals().SingleQuotes().SQL("STARTS WITH"), nil))
return v
}

func (v *queryStruct) OptionalLimit() *queryStruct {
func (v *QueryStruct) OptionalLimit() *QueryStruct {
v.fields = append(v.fields, NewField("Limit", "*LimitFrom", Tags().Keyword().SQL("LIMIT"), nil))
return v
}

func (v *queryStruct) OptionalCopyGrants() *queryStruct {
func (v *QueryStruct) OptionalCopyGrants() *QueryStruct {
return v.OptionalSQL("COPY GRANTS")
}
16 changes: 8 additions & 8 deletions pkg/sdk/poc/generator/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (i *Interface) newNoSqlOperation(kind string) *Interface {
return i
}

func (i *Interface) newSimpleOperation(kind string, doc string, queryStruct *queryStruct, helperStructs ...IntoField) *Interface {
func (i *Interface) newSimpleOperation(kind string, doc string, queryStruct *QueryStruct, helperStructs ...IntoField) *Interface {
if queryStruct.identifierField != nil {
queryStruct.identifierField.Kind = i.IdentifierKind
}
Expand All @@ -112,7 +112,7 @@ func (i *Interface) newOperationWithDBMapping(
doc string,
dbRepresentation *dbStruct,
resourceRepresentation *plainStruct,
queryStruct *queryStruct,
queryStruct *QueryStruct,
addMappingFunc func(op *Operation, from, to *Field),
) *Operation {
db := dbRepresentation.IntoField()
Expand All @@ -133,19 +133,19 @@ type IntoField interface {
IntoField() *Field
}

func (i *Interface) CreateOperation(doc string, queryStruct *queryStruct, helperStructs ...IntoField) *Interface {
func (i *Interface) CreateOperation(doc string, queryStruct *QueryStruct, helperStructs ...IntoField) *Interface {
return i.newSimpleOperation(string(OperationKindCreate), doc, queryStruct, helperStructs...)
}

func (i *Interface) AlterOperation(doc string, queryStruct *queryStruct) *Interface {
func (i *Interface) AlterOperation(doc string, queryStruct *QueryStruct) *Interface {
return i.newSimpleOperation(string(OperationKindAlter), doc, queryStruct)
}

func (i *Interface) DropOperation(doc string, queryStruct *queryStruct) *Interface {
func (i *Interface) DropOperation(doc string, queryStruct *QueryStruct) *Interface {
return i.newSimpleOperation(string(OperationKindDrop), doc, queryStruct)
}

func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *queryStruct) *Interface {
func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct) *Interface {
i.newOperationWithDBMapping(string(OperationKindShow), doc, dbRepresentation, resourceRepresentation, queryStruct, addShowMapping)
return i
}
Expand All @@ -154,12 +154,12 @@ func (i *Interface) ShowByIdOperation() *Interface {
return i.newNoSqlOperation(string(OperationKindShowByID))
}

func (i *Interface) DescribeOperation(describeKind DescriptionMappingKind, doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *queryStruct) *Interface {
func (i *Interface) DescribeOperation(describeKind DescriptionMappingKind, doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct) *Interface {
op := i.newOperationWithDBMapping(string(OperationKindDescribe), doc, dbRepresentation, resourceRepresentation, queryStruct, addDescriptionMapping)
op.DescribeKind = &describeKind
return i
}

func (i *Interface) CustomOperation(kind string, doc string, queryStruct *queryStruct) *Interface {
func (i *Interface) CustomOperation(kind string, doc string, queryStruct *QueryStruct) *Interface {
return i.newSimpleOperation(kind, doc, queryStruct)
}
26 changes: 13 additions & 13 deletions pkg/sdk/poc/generator/parameter_builders.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package generator

func (v *queryStruct) assignment(name string, kind string, transformer *ParameterTransformer) *queryStruct {
func (v *QueryStruct) assignment(name string, kind string, transformer *ParameterTransformer) *QueryStruct {
v.fields = append(v.fields, NewField(name, kind, Tags().Parameter(), transformer))
return v
}

func (v *queryStruct) Assignment(sqlPrefix string, kind string, transformer *ParameterTransformer) *queryStruct {
func (v *QueryStruct) Assignment(sqlPrefix string, kind string, transformer *ParameterTransformer) *QueryStruct {
if transformer != nil {
transformer = transformer.SQL(sqlPrefix)
} else {
Expand All @@ -14,49 +14,49 @@ func (v *queryStruct) Assignment(sqlPrefix string, kind string, transformer *Par
return v.assignment(sqlToFieldName(sqlPrefix, true), kind, transformer)
}

func (v *queryStruct) OptionalAssignment(sqlPrefix string, kind string, transformer *ParameterTransformer) *queryStruct {
func (v *QueryStruct) OptionalAssignment(sqlPrefix string, kind string, transformer *ParameterTransformer) *QueryStruct {
if len(kind) > 0 && kind[0] != '*' {
kind = KindOfPointer(kind)
}
return v.Assignment(sqlPrefix, kind, transformer)
}

func (v *queryStruct) ListAssignment(sqlPrefix string, listItemKind string, transformer *ParameterTransformer) *queryStruct {
func (v *QueryStruct) ListAssignment(sqlPrefix string, listItemKind string, transformer *ParameterTransformer) *QueryStruct {
return v.Assignment(sqlPrefix, KindOfSlice(listItemKind), transformer)
}

func (v *queryStruct) NumberAssignment(sqlPrefix string, transformer *ParameterTransformer) *queryStruct {
func (v *QueryStruct) NumberAssignment(sqlPrefix string, transformer *ParameterTransformer) *QueryStruct {
return v.Assignment(sqlPrefix, "int", transformer)
}

func (v *queryStruct) OptionalNumberAssignment(sqlPrefix string, transformer *ParameterTransformer) *queryStruct {
func (v *QueryStruct) OptionalNumberAssignment(sqlPrefix string, transformer *ParameterTransformer) *QueryStruct {
return v.Assignment(sqlPrefix, "*int", transformer)
}

func (v *queryStruct) TextAssignment(sqlPrefix string, transformer *ParameterTransformer) *queryStruct {
func (v *QueryStruct) TextAssignment(sqlPrefix string, transformer *ParameterTransformer) *QueryStruct {
return v.Assignment(sqlPrefix, "string", transformer)
}

func (v *queryStruct) OptionalTextAssignment(sqlPrefix string, transformer *ParameterTransformer) *queryStruct {
func (v *QueryStruct) OptionalTextAssignment(sqlPrefix string, transformer *ParameterTransformer) *QueryStruct {
return v.Assignment(sqlPrefix, "*string", transformer)
}

func (v *queryStruct) BooleanAssignment(sqlPrefix string, transformer *ParameterTransformer) *queryStruct {
func (v *QueryStruct) BooleanAssignment(sqlPrefix string, transformer *ParameterTransformer) *QueryStruct {
return v.Assignment(sqlPrefix, "bool", transformer)
}

func (v *queryStruct) OptionalBooleanAssignment(sqlPrefix string, transformer *ParameterTransformer) *queryStruct {
func (v *QueryStruct) OptionalBooleanAssignment(sqlPrefix string, transformer *ParameterTransformer) *QueryStruct {
return v.Assignment(sqlPrefix, "*bool", transformer)
}

func (v *queryStruct) OptionalIdentifierAssignment(sqlPrefix string, identifierKind string, transformer *ParameterTransformer) *queryStruct {
func (v *QueryStruct) OptionalIdentifierAssignment(sqlPrefix string, identifierKind string, transformer *ParameterTransformer) *QueryStruct {
return v.OptionalAssignment(sqlPrefix, identifierKind, transformer)
}

func (v *queryStruct) OptionalComment() *queryStruct {
func (v *QueryStruct) OptionalComment() *QueryStruct {
return v.OptionalTextAssignment("COMMENT", ParameterOptions().SingleQuotes())
}

func (v *queryStruct) SetComment() *queryStruct {
func (v *QueryStruct) SetComment() *QueryStruct {
return v.OptionalTextAssignment("SET COMMENT", ParameterOptions().SingleQuotes())
}
Loading

0 comments on commit 5148be4

Please sign in to comment.