Best Syzkaller code snippet using ast.serialize
ast.go
Source:ast.go
...26 // Serialize writes the statement/expression to the Writer. If an27 // error is returned the Writer may contain partial output.28 Serialize(w Writer) error29}30// Serialize serializes a serializer to a string.31func Serialize(s Serializer) (string, error) {32 w := &standardWriter{}33 if err := s.Serialize(w); err != nil {34 return "", err35 }36 return w.String(), nil37}38// SerializeWithPlaceholders serializes a serializer to a string but without substituting39// values. It may be useful for logging.40func SerializeWithPlaceholders(s Serializer) (string, error) {41 w := &placeholderWriter{}42 if err := s.Serialize(w); err != nil {43 return "", err44 }45 return w.String(), nil46}47// Writer defines an interface for writing a AST as SQL.48type Writer interface {49 io.Writer50 // WriteBytes writes a string of unprintable value.51 WriteBytes(node BytesVal) error52 // WriteEncoded writes an already encoded value.53 WriteEncoded(node EncodedVal) error54 // WriteNum writes a number value.55 WriteNum(node NumVal) error56 // WriteRaw writes a raw Go value.57 WriteRaw(node RawVal) error58 // WriteStr writes a SQL string value.59 WriteStr(node StrVal) error60}61type standardWriter struct {62 bytes.Buffer63}64func (w *standardWriter) WriteRaw(node RawVal) error {65 return encodeSQLValue(w, node.Val)66}67func (w *standardWriter) WriteEncoded(node EncodedVal) error {68 _, err := w.Write(node.Val)69 return err70}71func (w *standardWriter) WriteStr(node StrVal) error {72 return encodeSQLString(w, string(node))73}74func (w *standardWriter) WriteBytes(node BytesVal) error {75 return encodeSQLBytes(w, []byte(node))76}77func (w *standardWriter) WriteNum(node NumVal) error {78 _, err := io.WriteString(w, string(node))79 return err80}81// placeholderWriter will write all SQL value types as ? placeholders.82type placeholderWriter struct {83 bytes.Buffer84}85func (w *placeholderWriter) WriteRaw(node RawVal) error {86 _, err := w.Write(astPlaceholder)87 return err88}89func (w *placeholderWriter) WriteEncoded(node EncodedVal) error {90 _, err := w.Write(astPlaceholder)91 return err92}93func (w *placeholderWriter) WriteStr(node StrVal) error {94 _, err := w.Write(astPlaceholder)95 return err96}97func (w *placeholderWriter) WriteBytes(node BytesVal) error {98 _, err := w.Write(astPlaceholder)99 return err100}101func (w *placeholderWriter) WriteNum(node NumVal) error {102 _, err := w.Write(astPlaceholder)103 return err104}105var (106 // Placeholder is a placeholder for a value in a SQL statement. It is replaced with107 // an actual value when the query is executed.108 Placeholder = PlaceholderVal{}109)110// Statement represents a statement.111type Statement interface {112 Serializer113 statement()114}115func (*Union) statement() {}116func (*Select) statement() {}117func (*Insert) statement() {}118func (*Update) statement() {}119func (*Delete) statement() {}120// SelectStatement any SELECT statement.121type SelectStatement interface {122 Statement123 selectStatement()124 insertRows()125}126func (*Select) selectStatement() {}127func (*Union) selectStatement() {}128// Select represents a SELECT statement.129type Select struct {130 Comments Comments131 Distinct string132 Exprs SelectExprs133 From TableExprs134 Where *Where135 GroupBy GroupBy136 Having *Where137 OrderBy OrderBy138 Limit *Limit139 Lock string140}141// Select.Distinct142const (143 astDistinct = "DISTINCT "144)145// Select.Lock146const (147 astForUpdate = " FOR UPDATE"148 astShareMode = " LOCK IN SHARE MODE"149)150var (151 astSelect = []byte("SELECT ")152 astSelectFrom = []byte(" FROM ")153)154func (node *Select) Serialize(w Writer) error {155 if _, err := w.Write(astSelect); err != nil {156 return err157 }158 if err := node.Comments.Serialize(w); err != nil {159 return err160 }161 if _, err := io.WriteString(w, node.Distinct); err != nil {162 return err163 }164 if err := node.Exprs.Serialize(w); err != nil {165 return err166 }167 if _, err := w.Write(astSelectFrom); err != nil {168 return err169 }170 if err := node.From.Serialize(w); err != nil {171 return err172 }173 if err := node.Where.Serialize(w); err != nil {174 return err175 }176 if err := node.GroupBy.Serialize(w); err != nil {177 return err178 }179 if err := node.Having.Serialize(w); err != nil {180 return err181 }182 if err := node.OrderBy.Serialize(w); err != nil {183 return err184 }185 if err := node.Limit.Serialize(w); err != nil {186 return err187 }188 _, err := io.WriteString(w, node.Lock)189 return err190}191// Union represents a UNION statement.192type Union struct {193 Type string194 Left, Right SelectStatement195}196// Union.Type197const (198 astUnion = "UNION"199 astUnionAll = "UNION ALL"200 astSetMinus = "MINUS"201 astExcept = "EXCEPT"202 astIntersect = "INTERSECT"203)204func (node *Union) Serialize(w Writer) error {205 if _, err := w.Write(astOpenParen); err != nil {206 return err207 }208 if err := node.Left.Serialize(w); err != nil {209 return err210 }211 if _, err := w.Write(astCloseParen); err != nil {212 return err213 }214 if _, err := w.Write(astSpace); err != nil {215 return err216 }217 if _, err := io.WriteString(w, node.Type); err != nil {218 return err219 }220 if _, err := w.Write(astSpace); err != nil {221 return err222 }223 if _, err := w.Write(astOpenParen); err != nil {224 return err225 }226 if err := node.Right.Serialize(w); err != nil {227 return err228 }229 if _, err := w.Write(astCloseParen); err != nil {230 return err231 }232 return nil233}234// Insert represents an INSERT or REPLACE statement.235type Insert struct {236 Kind string237 Comments Comments238 Table *TableName239 Columns Columns240 Rows InsertRows241 OnDup OnDup242}243var (244 astInsertInto = []byte("INTO ")245 astSpace = []byte(" ")246)247func (node *Insert) Serialize(w Writer) error {248 if _, err := io.WriteString(w, node.Kind); err != nil {249 return err250 }251 if _, err := w.Write(astSpace); err != nil {252 return err253 }254 if err := node.Comments.Serialize(w); err != nil {255 return err256 }257 if _, err := w.Write(astInsertInto); err != nil {258 return err259 }260 if err := node.Table.Serialize(w); err != nil {261 return err262 }263 if _, err := w.Write(astSpace); err != nil {264 return err265 }266 if err := node.Columns.Serialize(w); err != nil {267 return err268 }269 if _, err := w.Write(astSpace); err != nil {270 return err271 }272 if err := node.Rows.Serialize(w); err != nil {273 return err274 }275 return node.OnDup.Serialize(w)276}277// InsertRows represents the rows for an INSERT statement.278type InsertRows interface {279 Serializer280 insertRows()281}282func (*Select) insertRows() {}283func (*Union) insertRows() {}284func (Values) insertRows() {}285// Update represents an UPDATE statement.286type Update struct {287 Comments Comments288 Table *TableName289 Tables []*Table290 Exprs UpdateExprs291 Where *Where292 OrderBy OrderBy293 Limit *Limit294}295var (296 astUpdate = []byte("UPDATE ")297 astSet = []byte(" SET ")298)299func (node *Update) Serialize(w Writer) error {300 if _, err := w.Write(astUpdate); err != nil {301 return err302 }303 if err := node.Comments.Serialize(w); err != nil {304 return err305 }306 if err := node.Table.Serialize(w); err != nil {307 return err308 }309 if _, err := w.Write(astSet); err != nil {310 return err311 }312 if err := node.Exprs.Serialize(w); err != nil {313 return err314 }315 if err := node.Where.Serialize(w); err != nil {316 return err317 }318 if err := node.OrderBy.Serialize(w); err != nil {319 return err320 }321 return node.Limit.Serialize(w)322}323// Delete represents a DELETE statement.324type Delete struct {325 Comments Comments326 // Table is either a TableName or a JoinBuilder327 Table abstractTable328 // TableNames is a list of tables to perform the delete on. This is only necessary when doing329 // joins, because you may not want to delete from all the tables involved in the join.330 // In deletes without joins, this may be empty or the table being deleted from.331 TableNames TableNames332 Where *Where333 OrderBy OrderBy334 Limit *Limit335}336var (337 astDelete = []byte("DELETE ")338 astDeleteFrom = []byte("FROM ")339)340func (node *Delete) Serialize(w Writer) error {341 if _, err := w.Write(astDelete); err != nil {342 return err343 }344 if err := node.Comments.Serialize(w); err != nil {345 return err346 }347 if len(node.TableNames) != 0 {348 if err := node.TableNames.Serialize(w); err != nil {349 return err350 }351 if _, err := fmt.Fprintf(w, " "); err != nil {352 return err353 }354 }355 if _, err := w.Write(astDeleteFrom); err != nil {356 return err357 }358 if err := node.Table.tableExpr().Serialize(w); err != nil {359 return err360 }361 if err := node.Where.Serialize(w); err != nil {362 return err363 }364 if err := node.OrderBy.Serialize(w); err != nil {365 return err366 }367 return node.Limit.Serialize(w)368}369// Comments represents a list of comments.370type Comments []string371func (node Comments) Serialize(w Writer) error {372 for _, c := range node {373 if err := encodeSQLComment(w, c); err != nil {374 return nil375 }376 if _, err := w.Write(astSpace); err != nil {377 return nil378 }379 }380 return nil381}382// TableNames represents several table names. It is used in deletes that have joins.383type TableNames []*TableName384func (node TableNames) Serialize(w Writer) error {385 var prefix []byte386 for _, n := range node {387 if _, err := w.Write(prefix); err != nil {388 return err389 }390 if err := n.Serialize(w); err != nil {391 return err392 }393 prefix = astCommaSpace394 }395 return nil396}397// SelectExprs represents SELECT expressions.398type SelectExprs []SelectExpr399var (400 astCommaSpace = []byte(", ")401)402func (node SelectExprs) Serialize(w Writer) error {403 var prefix []byte404 for _, n := range node {405 if _, err := w.Write(prefix); err != nil {406 return err407 }408 if err := n.Serialize(w); err != nil {409 return err410 }411 prefix = astCommaSpace412 }413 return nil414}415// SelectExpr represents a SELECT expression.416type SelectExpr interface {417 Serializer418 selectExpr()419}420func (*StarExpr) selectExpr() {}421func (*NonStarExpr) selectExpr() {}422// StarExpr defines a '*' or 'table.*' expression.423type StarExpr struct {424 TableName string425}426var (427 astStar = []byte("*")428)429func (node *StarExpr) Serialize(w Writer) error {430 if node.TableName != "" {431 if err := quoteName(w, node.TableName); err != nil {432 return err433 }434 if _, err := w.Write(astPeriod); err != nil {435 return err436 }437 }438 _, err := w.Write(astStar)439 return err440}441// NonStarExpr defines a non-'*' select expr.442type NonStarExpr struct {443 Expr Expr444 As string445}446var (447 astAsPrefix = []byte(" AS `")448)449func (node *NonStarExpr) Serialize(w Writer) error {450 if err := node.Expr.Serialize(w); err != nil {451 return err452 }453 if node.As != "" {454 if _, err := w.Write(astAsPrefix); err != nil {455 return err456 }457 if _, err := io.WriteString(w, node.As); err != nil {458 return err459 }460 if _, err := w.Write(astBackquote); err != nil {461 return err462 }463 }464 return nil465}466// Columns represents an insert column list.467// The syntax for Columns is a subset of SelectExprs.468// So, it's castable to a SelectExprs and can be analyzed469// as such.470type Columns []SelectExpr471var (472 astOpenParen = []byte("(")473 astCloseParen = []byte(")")474)475func (node Columns) Serialize(w Writer) error {476 if _, err := w.Write(astOpenParen); err != nil {477 return err478 }479 if err := SelectExprs(node).Serialize(w); err != nil {480 return err481 }482 _, err := w.Write(astCloseParen)483 return err484}485// TableExprs represents a list of table expressions.486type TableExprs []TableExpr487func (node TableExprs) Serialize(w Writer) error {488 var prefix []byte489 for _, n := range node {490 if _, err := w.Write(prefix); err != nil {491 return err492 }493 if err := n.Serialize(w); err != nil {494 return err495 }496 prefix = astCommaSpace497 }498 return nil499}500// TableExpr represents a table expression.501type TableExpr interface {502 Serializer503 tableExpr()504}505func (*AliasedTableExpr) tableExpr() {}506func (*ParenTableExpr) tableExpr() {}507func (*JoinTableExpr) tableExpr() {}508// AliasedTableExpr represents a table expression509// coupled with an optional alias or index hint.510type AliasedTableExpr struct {511 Expr SimpleTableExpr512 As string513 Hints *IndexHints514}515func (node *AliasedTableExpr) Serialize(w Writer) error {516 if err := node.Expr.Serialize(w); err != nil {517 return err518 }519 if node.As != "" {520 if _, err := w.Write(astAsPrefix); err != nil {521 return err522 }523 if _, err := io.WriteString(w, node.As); err != nil {524 return err525 }526 if _, err := w.Write(astBackquote); err != nil {527 return err528 }529 }530 if node.Hints != nil {531 // Hint node provides the space padding.532 if err := node.Hints.Serialize(w); err != nil {533 return err534 }535 }536 return nil537}538// SimpleTableExpr represents a simple table expression.539type SimpleTableExpr interface {540 Serializer541 simpleTableExpr()542}543func (*TableName) simpleTableExpr() {}544func (*Subquery) simpleTableExpr() {}545// TableName represents a table name.546type TableName struct {547 Name, Qualifier string548}549func (node *TableName) Serialize(w Writer) error {550 if node.Qualifier != "" {551 if err := quoteName(w, node.Qualifier); err != nil {552 return err553 }554 if _, err := w.Write(astPeriod); err != nil {555 return err556 }557 }558 return quoteName(w, node.Name)559}560// ParenTableExpr represents a parenthesized TableExpr.561type ParenTableExpr struct {562 Expr TableExpr563}564// JoinTableExpr represents a TableExpr that's a JOIN operation.565type JoinTableExpr struct {566 LeftExpr TableExpr567 Join string568 RightExpr TableExpr569 Cond JoinCond570}571// JoinTableExpr.Join572const (573 astJoin = "JOIN"574 astStraightJoin = "STRAIGHT_JOIN"575 astLeftJoin = "LEFT JOIN"576 astRightJoin = "RIGHT JOIN"577 astCrossJoin = "CROSS JOIN"578 astNaturalJoin = "NATURAL JOIN"579)580func (node *JoinTableExpr) Serialize(w Writer) error {581 if err := node.LeftExpr.Serialize(w); err != nil {582 return err583 }584 if _, err := fmt.Fprintf(w, " %s ", node.Join); err != nil {585 return err586 }587 if err := node.RightExpr.Serialize(w); err != nil {588 return err589 }590 if node.Cond != nil {591 if err := node.Cond.Serialize(w); err != nil {592 return err593 }594 }595 return nil596}597// JoinCond represents a join condition.598type JoinCond interface {599 Serializer600 joinCond()601}602func (*OnJoinCond) joinCond() {}603func (*UsingJoinCond) joinCond() {}604// OnJoinCond represents an ON join condition.605type OnJoinCond struct {606 Expr BoolExpr607}608var (609 astOn = []byte(" ON ")610)611func (node *OnJoinCond) Serialize(w Writer) error {612 if _, err := w.Write(astOn); err != nil {613 return err614 }615 return node.Expr.Serialize(w)616}617// UsingJoinCond represents a USING join condition.618type UsingJoinCond struct {619 Cols Columns620}621var (622 astUsing = []byte(" USING ")623)624func (node *UsingJoinCond) Serialize(w Writer) error {625 if _, err := w.Write(astUsing); err != nil {626 return err627 }628 return node.Cols.Serialize(w)629}630// IndexHints represents a list of index hints.631type IndexHints struct {632 Type string633 Indexes []string634}635const (636 astUse = "USE"637 astIgnore = "IGNORE"638 astForce = "FORCE"639)640func (node *IndexHints) Serialize(w Writer) error {641 if _, err := fmt.Fprintf(w, " %s INDEX ", node.Type); err != nil {642 return err643 }644 prefix := "("645 for _, n := range node.Indexes {646 if _, err := fmt.Fprintf(w, "%s%s", prefix, n); err != nil {647 return err648 }649 prefix = ", "650 }651 _, err := fmt.Fprintf(w, ")")652 return err653}654// Where represents a WHERE or HAVING clause.655type Where struct {656 Type string657 Expr BoolExpr658}659// Where.Type660const (661 astWhere = " WHERE "662 astHaving = " HAVING "663)664// NewWhere creates a WHERE or HAVING clause out665// of a BoolExpr. If the expression is nil, it returns nil.666func NewWhere(typ string, expr BoolExpr) *Where {667 if expr == nil {668 return nil669 }670 return &Where{Type: typ, Expr: expr}671}672func (node *Where) Serialize(w Writer) error {673 if node == nil {674 return nil675 }676 if _, err := io.WriteString(w, node.Type); err != nil {677 return err678 }679 return node.Expr.Serialize(w)680}681// Expr represents an expression.682type Expr interface {683 Serializer684 expr()685}686func (*AndExpr) expr() {}687func (*OrExpr) expr() {}688func (*NotExpr) expr() {}689func (*ParenBoolExpr) expr() {}690func (*ComparisonExpr) expr() {}691func (*RangeCond) expr() {}692func (*NullCheck) expr() {}693func (*ExistsExpr) expr() {}694func (PlaceholderVal) expr() {}695func (RawVal) expr() {}696func (EncodedVal) expr() {}697func (StrVal) expr() {}698func (NumVal) expr() {}699func (ValArg) expr() {}700func (*NullVal) expr() {}701func (*ColName) expr() {}702func (ValTuple) expr() {}703func (*Subquery) expr() {}704func (*BinaryExpr) expr() {}705func (*UnaryExpr) expr() {}706func (*FuncExpr) expr() {}707func (*CaseExpr) expr() {}708// BoolExpr represents a boolean expression.709type BoolExpr interface {710 boolExpr()711 Expr712}713func (*AndExpr) boolExpr() {}714func (*OrExpr) boolExpr() {}715func (*NotExpr) boolExpr() {}716func (*ParenBoolExpr) boolExpr() {}717func (*ComparisonExpr) boolExpr() {}718func (*RangeCond) boolExpr() {}719func (*NullCheck) boolExpr() {}720func (*ExistsExpr) boolExpr() {}721const (722 astAndExpr = " AND "723)724// AndExpr represents an AND expression.725type AndExpr struct {726 Op string727 Exprs []BoolExpr728}729func (node *AndExpr) Serialize(w Writer) error {730 if len(node.Exprs) == 0 {731 _, err := w.Write(astBoolTrue)732 return err733 } else if len(node.Exprs) == 1 {734 return node.Exprs[0].Serialize(w)735 }736 if _, err := w.Write(astOpenParen); err != nil {737 return err738 }739 if err := node.Exprs[0].Serialize(w); err != nil {740 return err741 }742 for _, expr := range node.Exprs[1:] {743 if _, err := io.WriteString(w, node.Op); err != nil {744 return err745 }746 if err := expr.Serialize(w); err != nil {747 return err748 }749 }750 _, err := w.Write(astCloseParen)751 return err752}753const (754 astOrExpr = " OR "755)756// OrExpr represents an OR expression.757type OrExpr struct {758 Op string759 Exprs []BoolExpr760}761func (node *OrExpr) Serialize(w Writer) error {762 if len(node.Exprs) == 0 {763 _, err := w.Write(astBoolFalse)764 return err765 } else if len(node.Exprs) == 1 {766 return node.Exprs[0].Serialize(w)767 }768 if _, err := w.Write(astOpenParen); err != nil {769 return err770 }771 if err := node.Exprs[0].Serialize(w); err != nil {772 return err773 }774 for _, expr := range node.Exprs[1:] {775 if _, err := io.WriteString(w, node.Op); err != nil {776 return err777 }778 if err := expr.Serialize(w); err != nil {779 return err780 }781 }782 _, err := w.Write(astCloseParen)783 return err784}785const (786 astNotExpr = "NOT "787)788// NotExpr represents a NOT expression.789type NotExpr struct {790 Op string791 Expr BoolExpr792}793func (node *NotExpr) Serialize(w Writer) error {794 if _, err := io.WriteString(w, node.Op); err != nil {795 return err796 }797 return node.Expr.Serialize(w)798}799// ParenBoolExpr represents a parenthesized boolean expression.800type ParenBoolExpr struct {801 Expr BoolExpr802}803func (node *ParenBoolExpr) Serialize(w Writer) error {804 if _, err := w.Write(astOpenParen); err != nil {805 return err806 }807 if err := node.Expr.Serialize(w); err != nil {808 return err809 }810 _, err := w.Write(astCloseParen)811 return err812}813// ComparisonExpr represents a two-value comparison expression.814type ComparisonExpr struct {815 Operator string816 Left, Right ValExpr817}818// ComparisonExpr.Operator819const (820 astEQ = " = "821 astLT = " < "822 astGT = " > "823 astLE = " <= "824 astGE = " >= "825 astNE = " != "826 astNSE = " <=> "827 astIn = " IN "828 astNot = " NOT "829 astNotIn = " NOT IN "830 astLike = " LIKE "831 astNotLike = " NOT LIKE "832 astRegExp = " REGEXP "833 astNotRegExp = " NOT REGEXP "834)835func (node *ComparisonExpr) Serialize(w Writer) error {836 if err := node.Left.Serialize(w); err != nil {837 return err838 }839 if _, err := io.WriteString(w, node.Operator); err != nil {840 return err841 }842 return node.Right.Serialize(w)843}844// RangeCond represents a BETWEEN or a NOT BETWEEN expression.845type RangeCond struct {846 Operator string847 Left ValExpr848 From, To ValExpr849}850// RangeCond.Operator851const (852 astBetween = " BETWEEN "853 astNotBetween = " NOT BETWEEN "854)855var (856 astAnd = []byte(" AND ")857)858func (node *RangeCond) Serialize(w Writer) error {859 if err := node.Left.Serialize(w); err != nil {860 return err861 }862 if _, err := io.WriteString(w, node.Operator); err != nil {863 return err864 }865 if err := node.From.Serialize(w); err != nil {866 return err867 }868 if _, err := w.Write(astAnd); err != nil {869 return err870 }871 return node.To.Serialize(w)872}873// NullCheck represents an IS NULL or an IS NOT NULL expression.874type NullCheck struct {875 Operator string876 Expr ValExpr877}878// NullCheck.Operator879const (880 astIsNull = " IS NULL"881 astIsNotNull = " IS NOT NULL"882)883func (node *NullCheck) Serialize(w Writer) error {884 if err := node.Expr.Serialize(w); err != nil {885 return err886 }887 _, err := io.WriteString(w, node.Operator)888 return err889}890// ExistsExpr represents an EXISTS expression.891type ExistsExpr struct {892 Subquery *Subquery893}894// ValExpr represents a value expression.895type ValExpr interface {896 valExpr()897 Expr898}899func (PlaceholderVal) valExpr() {}900func (RawVal) valExpr() {}901func (EncodedVal) valExpr() {}902func (StrVal) valExpr() {}903func (NumVal) valExpr() {}904func (ValArg) valExpr() {}905func (*NullVal) valExpr() {}906func (*ColName) valExpr() {}907func (ValTuple) valExpr() {}908func (*Subquery) valExpr() {}909func (*BinaryExpr) valExpr() {}910func (*UnaryExpr) valExpr() {}911func (*FuncExpr) valExpr() {}912func (*CaseExpr) valExpr() {}913var (914 astPlaceholder = []byte("?")915)916// PlaceholderVal represents a placeholder parameter that will be supplied917// when executing the query. It will be serialized as a ?.918type PlaceholderVal struct{}919func (node PlaceholderVal) Serialize(w Writer) error {920 _, err := w.Write(astPlaceholder)921 return err922}923// RawVal represents a raw go value924type RawVal struct {925 Val interface{}926}927var (928 astBoolTrue = []byte("1")929 astBoolFalse = []byte("0")930)931func (node RawVal) Serialize(w Writer) error {...
scalars_test.go
Source:scalars_test.go
1package scalars2import (3 "github.com/graphql-go/graphql/language/ast"4 "github.com/graphql-go/graphql/language/kinds"5 . "github.com/smartystreets/goconvey/convey"6 "testing"7)8func TestGraphQLInt32Scalar(t *testing.T) {9 Convey("Test GraphQLInt32Scalar.Serialize", t, func() {10 So(GraphQLInt32Scalar.Serialize(int32(534)), ShouldEqual, int32(534))11 So(GraphQLInt32Scalar.Serialize(int(534)), ShouldEqual, int32(534))12 So(GraphQLInt32Scalar.Serialize("123"), ShouldEqual, nil)13 })14 Convey("Test GraphQLInt32Scalar.ParseValue", t, func() {15 So(GraphQLInt32Scalar.ParseValue(int32(534)), ShouldEqual, int32(534))16 So(GraphQLInt32Scalar.ParseValue(int64(534)), ShouldEqual, int32(534))17 So(GraphQLInt32Scalar.ParseValue(float32(534)), ShouldEqual, int32(534))18 So(GraphQLInt32Scalar.ParseValue(float64(534)), ShouldEqual, int32(534))19 So(GraphQLInt32Scalar.ParseValue("123"), ShouldEqual, int32(123))20 So(GraphQLInt32Scalar.ParseValue("s123"), ShouldEqual, nil)21 So(GraphQLInt32Scalar.ParseValue(int(534)), ShouldEqual, nil)22 })23 Convey("Test GraphQLInt32Scalar.ParseLiteral", t, func() {24 So(GraphQLInt32Scalar.ParseLiteral(&ast.IntValue{Kind: kinds.IntValue, Value: "534"}), ShouldEqual, int32(534))25 So(GraphQLInt32Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "534"}), ShouldEqual, int32(534))26 So(GraphQLInt32Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "s534"}), ShouldEqual, nil)27 So(GraphQLInt32Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.BooleanValue, Value: "true"}), ShouldEqual, nil)28 })29}30func TestGraphQLInt64Scalar(t *testing.T) {31 Convey("Test GraphQLInt64Scalar.Serialize", t, func() {32 So(GraphQLInt64Scalar.Serialize(int64(534)), ShouldEqual, int64(534))33 So(GraphQLInt64Scalar.Serialize(int32(534)), ShouldEqual, int64(534))34 So(GraphQLInt64Scalar.Serialize(int(534)), ShouldEqual, int64(534))35 So(GraphQLInt64Scalar.Serialize("123"), ShouldEqual, nil)36 })37 Convey("Test GraphQLInt64Scalar.ParseValue", t, func() {38 So(GraphQLInt64Scalar.ParseValue(int(534)), ShouldEqual, nil)39 So(GraphQLInt64Scalar.ParseValue(int32(534)), ShouldEqual, int64(534))40 So(GraphQLInt64Scalar.ParseValue(int64(534)), ShouldEqual, int64(534))41 So(GraphQLInt64Scalar.ParseValue(float32(534)), ShouldEqual, int64(534))42 So(GraphQLInt64Scalar.ParseValue(float64(534)), ShouldEqual, int64(534))43 So(GraphQLInt64Scalar.ParseValue("123"), ShouldEqual, int64(123))44 So(GraphQLInt64Scalar.ParseValue("s123"), ShouldEqual, nil)45 })46 Convey("Test GraphQLInt64Scalar.ParseLiteral", t, func() {47 So(GraphQLInt64Scalar.ParseLiteral(&ast.IntValue{Kind: kinds.IntValue, Value: "534"}), ShouldEqual, int64(534))48 So(GraphQLInt64Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "534"}), ShouldEqual, int64(534))49 So(GraphQLInt64Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "s534"}), ShouldEqual, nil)50 So(GraphQLInt64Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.BooleanValue, Value: "true"}), ShouldEqual, nil)51 })52}53func TestGraphQLUInt64Scalar(t *testing.T) {54 Convey("Test GraphQLUInt64Scalar.Serialize", t, func() {55 So(GraphQLUInt64Scalar.Serialize(uint64(534)), ShouldEqual, uint64(534))56 So(GraphQLUInt64Scalar.Serialize(uint32(534)), ShouldEqual, uint64(534))57 So(GraphQLUInt64Scalar.Serialize(uint(534)), ShouldEqual, uint64(534))58 So(GraphQLUInt64Scalar.Serialize("123"), ShouldEqual, nil)59 })60 Convey("Test GraphQLUInt64Scalar.ParseValue", t, func() {61 So(GraphQLUInt64Scalar.ParseValue(uint32(534)), ShouldEqual, uint64(534))62 So(GraphQLUInt64Scalar.ParseValue(uint64(534)), ShouldEqual, uint64(534))63 So(GraphQLUInt64Scalar.ParseValue(float32(534)), ShouldEqual, uint64(534))64 So(GraphQLUInt64Scalar.ParseValue(float64(534)), ShouldEqual, uint64(534))65 So(GraphQLUInt64Scalar.ParseValue("123"), ShouldEqual, uint64(123))66 So(GraphQLUInt64Scalar.ParseValue(int(534)), ShouldEqual, nil)67 So(GraphQLUInt64Scalar.ParseValue("s123"), ShouldEqual, nil)68 })69 Convey("Test GraphQLUInt64Scalar.ParseLiteral", t, func() {70 So(GraphQLUInt64Scalar.ParseLiteral(&ast.IntValue{Kind: kinds.IntValue, Value: "534"}), ShouldEqual, uint64(534))71 So(GraphQLUInt64Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "534"}), ShouldEqual, uint64(534))72 So(GraphQLUInt64Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "s534"}), ShouldEqual, nil)73 So(GraphQLUInt64Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.BooleanValue, Value: "true"}), ShouldEqual, nil)74 })75}76func TestGraphQLUInt32Scalar(t *testing.T) {77 Convey("Test GraphQLUInt32Scalar.Serialize", t, func() {78 So(GraphQLUInt32Scalar.Serialize(uint32(534)), ShouldEqual, uint32(534))79 So(GraphQLUInt32Scalar.Serialize(uint(534)), ShouldEqual, uint32(534))80 So(GraphQLUInt32Scalar.Serialize("123"), ShouldEqual, nil)81 So(GraphQLUInt32Scalar.Serialize(uint64(534)), ShouldEqual, nil)82 })83 Convey("Test GraphQLUInt32Scalar.ParseValue", t, func() {84 So(GraphQLUInt32Scalar.ParseValue(uint32(534)), ShouldEqual, uint32(534))85 So(GraphQLUInt32Scalar.ParseValue(float32(534)), ShouldEqual, uint32(534))86 So(GraphQLUInt32Scalar.ParseValue(float64(534)), ShouldEqual, uint32(534))87 So(GraphQLUInt32Scalar.ParseValue("123"), ShouldEqual, uint32(123))88 So(GraphQLUInt32Scalar.ParseValue(int(534)), ShouldEqual, nil)89 So(GraphQLUInt32Scalar.ParseValue("s123"), ShouldEqual, nil)90 })91 Convey("Test GraphQLUInt32Scalar.ParseLiteral", t, func() {92 So(GraphQLUInt32Scalar.ParseLiteral(&ast.IntValue{Kind: kinds.IntValue, Value: "534"}), ShouldEqual, uint32(534))93 So(GraphQLUInt32Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "534"}), ShouldEqual, uint32(534))94 So(GraphQLUInt32Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "s534"}), ShouldEqual, nil)95 So(GraphQLUInt32Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.BooleanValue, Value: "true"}), ShouldEqual, nil)96 })97}98func TestGraphQLFloat32Scalar(t *testing.T) {99 Convey("Test GraphQLFloat32Scalar.Serialize", t, func() {100 So(GraphQLFloat32Scalar.Serialize(float32(534)), ShouldEqual, float32(534))101 So(GraphQLFloat32Scalar.Serialize(uint64(534)), ShouldEqual, nil)102 })103 Convey("Test GraphQLFloat32Scalar.ParseValue", t, func() {104 So(GraphQLFloat32Scalar.ParseValue(float32(534)), ShouldEqual, float32(534))105 So(GraphQLFloat32Scalar.ParseValue("123"), ShouldEqual, float32(123))106 So(GraphQLFloat32Scalar.ParseValue(int(534)), ShouldEqual, nil)107 So(GraphQLFloat32Scalar.ParseValue("s123"), ShouldEqual, nil)108 })109 Convey("Test GraphQLFloat32Scalar.ParseLiteral", t, func() {110 So(GraphQLFloat32Scalar.ParseLiteral(&ast.IntValue{Kind: kinds.IntValue, Value: "534"}), ShouldEqual, float32(534))111 So(GraphQLFloat32Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "534"}), ShouldEqual, float32(534))112 So(GraphQLFloat32Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "s534"}), ShouldEqual, nil)113 So(GraphQLFloat32Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.BooleanValue, Value: "true"}), ShouldEqual, nil)114 })115}116func TestGraphQLFloat64Scalar(t *testing.T) {117 Convey("Test GraphQLFloat64Scalar.Serialize", t, func() {118 So(GraphQLFloat64Scalar.Serialize(float32(534)), ShouldEqual, float64(534))119 So(GraphQLFloat64Scalar.Serialize(float64(534)), ShouldEqual, float64(534))120 So(GraphQLFloat64Scalar.Serialize(uint64(534)), ShouldEqual, nil)121 })122 Convey("Test GraphQLFloat64Scalar.ParseValue", t, func() {123 So(GraphQLFloat64Scalar.ParseValue(float32(534)), ShouldEqual, float64(534))124 So(GraphQLFloat64Scalar.ParseValue(float64(534)), ShouldEqual, float64(534))125 So(GraphQLFloat64Scalar.ParseValue("123"), ShouldEqual, float64(123))126 So(GraphQLFloat64Scalar.ParseValue(int(534)), ShouldEqual, nil)127 So(GraphQLFloat64Scalar.ParseValue("s123"), ShouldEqual, nil)128 })129 Convey("Test GraphQLFloat64Scalar.ParseLiteral", t, func() {130 So(GraphQLFloat64Scalar.ParseLiteral(&ast.IntValue{Kind: kinds.IntValue, Value: "534"}), ShouldEqual, float64(534))131 So(GraphQLFloat64Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "534"}), ShouldEqual, float64(534))132 So(GraphQLFloat64Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "s534"}), ShouldEqual, nil)133 So(GraphQLFloat64Scalar.ParseLiteral(&ast.StringValue{Kind: kinds.BooleanValue, Value: "true"}), ShouldEqual, nil)134 })135}136func TestNoDataScalar(t *testing.T) {137 Convey("Test NoDataScalar.Serialize", t, func() {138 So(NoDataScalar.Serialize(float32(534)), ShouldEqual, nil)139 })140 Convey("Test NoDataScalar.ParseValue", t, func() {141 So(NoDataScalar.ParseValue(float32(534)), ShouldEqual, 0)142 })143 Convey("Test NoDataScalar.ParseLiteral", t, func() {144 So(NoDataScalar.ParseLiteral(&ast.StringValue{Kind: kinds.StringValue, Value: "534"}), ShouldEqual, 0)145 })146}...
serialize
Using AI Code Generation
1import (2func main() {3 fset := token.NewFileSet()4 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)5 if err != nil {6 log.Fatal(err)7 }8 fmt.Println("imports:")9 for _, s := range f.Imports {10 fmt.Println(s.Path.Value)11 }12 fmt.Println("")13 ast.Print(fset, f)14}15imports:16import (17func main() {18 f, err := os.Open("1.go")19 if err != nil {20 log.Fatal(err)21 }22 defer f.Close()23 scanner := bufio.NewScanner(f)24 for scanner.Scan() {25 fmt.Println(strings.ToUpper(scanner.Text()))26 }27 if err := scanner.Err(); err != nil {28 log.Fatal(err)29 }30}31import (32func main() {33 f, err := os.Open("1.go")34 if err != nil {35 log.Fatal(err)36 }37 defer f.Close()38 scanner := bufio.NewScanner(f)39 for scanner.Scan() {40 fmt.Println(strings.ToUpper(scanner.Text()))41 }42 if err := scanner.Err(); err != nil {43 log.Fatal(err)44 }45}
serialize
Using AI Code Generation
1import (2func main() {3 fset := token.NewFileSet()4 node, err := parser.ParseFile(fset, "2.go", nil, parser.ParseComments)5 if err != nil {6 fmt.Println(err)7 }8 data, err := json.MarshalIndent(node, "", " ")9 if err != nil {10 fmt.Println(err)11 }12 fmt.Println(string(data))13}14{15 "Name": {16 },17 {
serialize
Using AI Code Generation
1import (2type AST struct {3}4func main() {5 root.Children = append(root.Children, child1)6 root.Children = append(root.Children, child2)7 root.print()8 root.serialize()9 root.deserialize()10 root.print()11}12func (ast *AST) print() {13 fmt.Println(ast.Name)14 for _, child := range ast.Children {15 child.print()16 }17}18func (ast *AST) serialize() {19 file, _ := os.Create("ast.json")20 encoder := json.NewEncoder(file)21 encoder.Encode(ast)22}23func (ast *AST) deserialize() {24 file, _ := os.Open("ast.json")25 decoder := json.NewDecoder(file)26 decoder.Decode(ast)27}
serialize
Using AI Code Generation
1import "ast"2func main() {3 a := ast.New()4 a.Add("Hello")5 a.Add("World")6 fmt.Println(a.Serialize())7}8import "fmt"9type AST struct {10}11func New() *AST {12 return &AST{}13}14func (a *AST) Add(node string) {15 a.nodes = append(a.nodes, node)16}17func (a *AST) Serialize() string {18 return fmt.Sprintf("%s", a.nodes)19}20The ast package will be imported by the parser package. The parser package will be able to use
serialize
Using AI Code Generation
1func main() {2 println("Hello, World!")3}4import (5func main() {6 b, err := ioutil.ReadFile("1.go")7 if err != nil {8 fmt.Print(err)9 }10 err = ioutil.WriteFile("3.go", b, 0644)11 if err != nil {12 panic(err)13 }14 b, err = ioutil.ReadFile("3.go")15 if err != nil {16 fmt.Print(err)17 }18 str := string(b)19 fmt.Println(str)20}21func main() {22 println("Hello, World!")23}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!