github.com/ipld/go-ipld-prime@v0.21.0/schema/gen/go/genpartsMap.go (about)

     1  package gengo
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // FIXME docs: these methods all say "-oid" but I think that was overoptimistic and not actually that applicable, really.
     8  //  AssignNode?  Okay, that one's fine.
     9  //  The rest?  They're all *very* emphatic about knowing either:
    10  //   - that na.w.t and na.w.m are fields; or,
    11  //   - that there's only one 'ka' and 'va' (one type each; and that it's reused).
    12  //   The reuse level for those two traits is pretty minimal.
    13  
    14  func emitNodeAssemblerMethodBeginMap_mapoid(w io.Writer, adjCfg *AdjunctCfg, data interface{}) {
    15  	// This method contains a branch to support MaybeUsesPtr because new memory may need to be allocated.
    16  	//  This allocation only happens if the 'w' ptr is nil, which means we're being used on a Maybe;
    17  	//  otherwise, the 'w' ptr should already be set, and we fill that memory location without allocating, as usual.
    18  	doTemplate(`
    19  		func (na *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) BeginMap(sizeHint int64) (datamodel.MapAssembler, error) {
    20  			switch *na.m {
    21  			case schema.Maybe_Value, schema.Maybe_Null:
    22  				panic("invalid state: cannot assign into assembler that's already finished")
    23  			case midvalue:
    24  				panic("invalid state: it makes no sense to 'begin' twice on the same assembler!")
    25  			}
    26  			*na.m = midvalue
    27  			if sizeHint < 0 {
    28  				sizeHint = 0
    29  			}
    30  			{{- if .Type | MaybeUsesPtr }}
    31  			if na.w == nil {
    32  				na.w = &_{{ .Type | TypeSymbol }}{}
    33  			}
    34  			{{- end}}
    35  			na.w.m = make(map[_{{ .Type.KeyType | TypeSymbol }}]{{if .Type.ValueIsNullable }}Maybe{{else}}*_{{end}}{{ .Type.ValueType | TypeSymbol }}, sizeHint)
    36  			na.w.t = make([]_{{ .Type | TypeSymbol }}__entry, 0, sizeHint)
    37  			return na, nil
    38  		}
    39  	`, w, adjCfg, data)
    40  }
    41  
    42  func emitNodeAssemblerMethodAssignNode_mapoid(w io.Writer, adjCfg *AdjunctCfg, data interface{}) {
    43  	// AssignNode goes through three phases:
    44  	// 1. is it null?  Jump over to AssignNull (which may or may not reject it).
    45  	// 2. is it our own type?  Handle specially -- we might be able to do efficient things.
    46  	// 3. is it the right kind to morph into us?  Do so.
    47  	//
    48  	// We do not set m=midvalue in phase 3 -- it shouldn't matter unless you're trying to pull off concurrent access, which is wrong and unsafe regardless.
    49  	//
    50  	// This works easily for both type-level and representational nodes because
    51  	//  any divergences that have to do with the child value are nicely hidden behind  `AssembleKey` and `AssembleValue`.
    52  	doTemplate(`
    53  		func (na *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) AssignNode(v datamodel.Node) error {
    54  			if v.IsNull() {
    55  				return na.AssignNull()
    56  			}
    57  			if v2, ok := v.(*_{{ .Type | TypeSymbol }}); ok {
    58  				switch *na.m {
    59  				case schema.Maybe_Value, schema.Maybe_Null:
    60  					panic("invalid state: cannot assign into assembler that's already finished")
    61  				case midvalue:
    62  					panic("invalid state: cannot assign null into an assembler that's already begun working on recursive structures!")
    63  				}
    64  				{{- if .Type | MaybeUsesPtr }}
    65  				if na.w == nil {
    66  					na.w = v2
    67  					*na.m = schema.Maybe_Value
    68  					return nil
    69  				}
    70  				{{- end}}
    71  				*na.w = *v2
    72  				*na.m = schema.Maybe_Value
    73  				return nil
    74  			}
    75  			if v.Kind() != datamodel.Kind_Map {
    76  				return datamodel.ErrWrongKind{TypeName: "{{ .PkgName }}.{{ .Type.Name }}{{ if .IsRepr }}.Repr{{end}}", MethodName: "AssignNode", AppropriateKind: datamodel.KindSet_JustMap, ActualKind: v.Kind()}
    77  			}
    78  			itr := v.MapIterator()
    79  			for !itr.Done() {
    80  				k, v, err := itr.Next()
    81  				if err != nil {
    82  					return err
    83  				}
    84  				if err := na.AssembleKey().AssignNode(k); err != nil {
    85  					return err
    86  				}
    87  				if err := na.AssembleValue().AssignNode(v); err != nil {
    88  					return err
    89  				}
    90  			}
    91  			return na.Finish()
    92  		}
    93  	`, w, adjCfg, data)
    94  }
    95  
    96  func emitNodeAssemblerHelper_mapoid_keyTidyHelper(w io.Writer, adjCfg *AdjunctCfg, data interface{}) {
    97  	// This function attempts to clean up the state machine to acknolwedge key assembly finish.
    98  	//  If the child was finished and we just collected it, return true and update state to maState_expectValue.
    99  	//   Collecting the child includes updating the 'ma.w.m' to point into the relevant row of 'ma.w.t', since that couldn't be done earlier,
   100  	//    AND initializing the 'ma.va' (since we're already holding relevant offsets into 'ma.w.t').
   101  	//  Otherwise, if it wasn't done, return false;
   102  	//   and the caller is almost certain to emit an error momentarily.
   103  	// The function will only be called when the current state is maState_midKey.
   104  	//  (In general, the idea is that if the user is doing things correctly,
   105  	//   this function will only be called when the child is in fact finished.)
   106  	// Completion info always comes via 'cm', and we reset it to its initial condition of Maybe_Absent here.
   107  	//  At the same time, we nil the 'w' pointer for the child assembler; otherwise its own state machine would probably let it modify 'w' again!
   108  	//
   109  	// DRY(nope): Can this be extracted to be a shared function between repr and type level nodes?
   110  	//  It is textually identical, so... yeah, that'd be nice.  But...
   111  	//  Nope.  It touches `ma.ka` and `ma.va` directly.
   112  	//   Attempting to extract or hide those behind an interface would create virtual function calls in a very tight spot, and we don't want the execution time cost.
   113  	doTemplate(`
   114  		func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) keyFinishTidy() bool {
   115  			switch ma.cm {
   116  			case schema.Maybe_Value:
   117  				ma.ka.w = nil
   118  				tz := &ma.w.t[len(ma.w.t)-1]
   119  				ma.cm = schema.Maybe_Absent
   120  				ma.state = maState_expectValue
   121  				ma.w.m[tz.k] = &tz.v
   122  				{{- if .Type.ValueIsNullable }}
   123  				{{- if not (MaybeUsesPtr .Type.ValueType) }}
   124  				ma.va.w = &tz.v.v
   125  				{{- end}}
   126  				ma.va.m = &tz.v.m
   127  				tz.v.m = allowNull
   128  				{{- else}}
   129  				ma.va.w = &tz.v
   130  				ma.va.m = &ma.cm
   131  				{{- end}}
   132  				ma.ka.reset()
   133  				return true
   134  			default:
   135  				return false
   136  			}
   137  		}
   138  	`, w, adjCfg, data)
   139  }
   140  
   141  func emitNodeAssemblerHelper_mapoid_valueTidyHelper(w io.Writer, adjCfg *AdjunctCfg, data interface{}) {
   142  	// This function attempts to clean up the state machine to acknolwedge child value assembly finish.
   143  	//  If the child was finished and we just collected it, return true and update state to maState_initial.
   144  	//  Otherwise, if it wasn't done, return false;
   145  	//   and the caller is almost certain to emit an error momentarily.
   146  	// The function will only be called when the current state is maState_midValue.
   147  	//  (In general, the idea is that if the user is doing things correctly,
   148  	//   this function will only be called when the child is in fact finished.)
   149  	// If 'cm' is used, we reset it to its initial condition of Maybe_Absent here.
   150  	//  At the same time, we nil the 'w' pointer for the child assembler; otherwise its own state machine would probably let it modify 'w' again!
   151  	//
   152  	// DRY(nope): Can this be extracted to be a shared function between repr and type level nodes?
   153  	//  Exact same story as the key tidy helper -- touches child assemblers concretely, and that blocks extraction.
   154  	doTemplate(`
   155  		func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) valueFinishTidy() bool {
   156  			{{- if .Type.ValueIsNullable }}
   157  			tz := &ma.w.t[len(ma.w.t)-1]
   158  			switch tz.v.m {
   159  			case schema.Maybe_Null:
   160  				ma.state = maState_initial
   161  				ma.va.reset()
   162  				return true
   163  			case schema.Maybe_Value:
   164  				{{- if (MaybeUsesPtr .Type.ValueType) }}
   165  				tz.v.v = ma.va.w
   166  				{{- end}}
   167  				ma.va.w = nil
   168  				ma.state = maState_initial
   169  				ma.va.reset()
   170  				return true
   171  			{{- else}}
   172  			switch ma.cm {
   173  			case schema.Maybe_Value:
   174  				ma.va.w = nil
   175  				ma.cm = schema.Maybe_Absent
   176  				ma.state = maState_initial
   177  				ma.va.reset()
   178  				return true
   179  			{{- end}}
   180  			default:
   181  				return false
   182  			}
   183  		}
   184  	`, w, adjCfg, data)
   185  }
   186  
   187  func emitNodeAssemblerHelper_mapoid_mapAssemblerMethods(w io.Writer, adjCfg *AdjunctCfg, data interface{}) {
   188  	// FUTURE: some of the setup of the child assemblers could probably be DRY'd up.
   189  	//
   190  	// REVIEW: there's a copy-by-value of k2 that's avoidable.  But it simplifies the error path.  Worth working on?
   191  	//
   192  	// REVIEW: processing the key via the reprPrototype of the key even when we're at the type level if it's type kind isn't string is currently supported, but should it be?  or is that more confusing than valuable?
   193  	//  Very possible that it shouldn't be supported: the full-on keyAssembler route won't accept this, so consistency with that might be best.
   194  	//  On the other hand, lookups by string *do* support this kind of processing (and it must, or PathSegment utility becomes unacceptably damaged), so either way, something feels surprising.
   195  	//
   196  	// DRY(nope): Can this be extracted to a shared function in the output?
   197  	//  Same story as the tidy helpers -- it touches `va` and `ka` concretely in several places, and that blocks extraction.
   198  	//
   199  	// DRY: a lot of the state transition fences again are common for all mapoids, and could probably even be a function over '*state'...
   200  	//   except for the fact they need to call the valueFinishTidy function, which is another one of those points that blocks extraction because we strongly don't want virtual functions calls there.
   201  	//   Maybe the templates can be textually dedup'd more, though, at least.
   202  	doTemplate(`
   203  		func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) AssembleEntry(k string) (datamodel.NodeAssembler, error) {
   204  			switch ma.state {
   205  			case maState_initial:
   206  				// carry on
   207  			case maState_midKey:
   208  				panic("invalid state: AssembleEntry cannot be called when in the middle of assembling another key")
   209  			case maState_expectValue:
   210  				panic("invalid state: AssembleEntry cannot be called when expecting start of value assembly")
   211  			case maState_midValue:
   212  				if !ma.valueFinishTidy() {
   213  					panic("invalid state: AssembleEntry cannot be called when in the middle of assembling a value")
   214  				} // if tidy success: carry on
   215  			case maState_finished:
   216  				panic("invalid state: AssembleEntry cannot be called on an assembler that's already finished")
   217  			}
   218  
   219  			var k2 _{{ .Type.KeyType | TypeSymbol }}
   220  			{{- if or (not (eq .Type.KeyType.TypeKind.String "String")) .IsRepr }}
   221  			if err := (_{{ .Type.KeyType | TypeSymbol }}__ReprPrototype{}).fromString(&k2, k); err != nil {
   222  				return nil, err // TODO wrap in some kind of ErrInvalidKey
   223  			}
   224  			{{- else}}
   225  			if err := (_{{ .Type.KeyType | TypeSymbol }}__Prototype{}).fromString(&k2, k); err != nil {
   226  				return nil, err // TODO wrap in some kind of ErrInvalidKey
   227  			}
   228  			{{- end}}
   229  			if _, exists := ma.w.m[k2]; exists {
   230  				return nil, datamodel.ErrRepeatedMapKey{Key: &k2}
   231  			}
   232  			ma.w.t = append(ma.w.t, _{{ .Type | TypeSymbol }}__entry{k: k2})
   233  			tz := &ma.w.t[len(ma.w.t)-1]
   234  			ma.state = maState_midValue
   235  
   236  			ma.w.m[k2] = &tz.v
   237  			{{- if .Type.ValueIsNullable }}
   238  			{{- if not (MaybeUsesPtr .Type.ValueType) }}
   239  			ma.va.w = &tz.v.v
   240  			{{- end}}
   241  			ma.va.m = &tz.v.m
   242  			tz.v.m = allowNull
   243  			{{- else}}
   244  			ma.va.w = &tz.v
   245  			ma.va.m = &ma.cm
   246  			{{- end}}
   247  			return &ma.va, nil
   248  		}
   249  	`, w, adjCfg, data)
   250  	doTemplate(`
   251  		func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) AssembleKey() datamodel.NodeAssembler {
   252  			switch ma.state {
   253  			case maState_initial:
   254  				// carry on
   255  			case maState_midKey:
   256  				panic("invalid state: AssembleKey cannot be called when in the middle of assembling another key")
   257  			case maState_expectValue:
   258  				panic("invalid state: AssembleKey cannot be called when expecting start of value assembly")
   259  			case maState_midValue:
   260  				if !ma.valueFinishTidy() {
   261  					panic("invalid state: AssembleKey cannot be called when in the middle of assembling a value")
   262  				} // if tidy success: carry on
   263  			case maState_finished:
   264  				panic("invalid state: AssembleKey cannot be called on an assembler that's already finished")
   265  			}
   266  			ma.w.t = append(ma.w.t, _{{ .Type | TypeSymbol }}__entry{})
   267  			ma.state = maState_midKey
   268  			ma.ka.m = &ma.cm
   269  			ma.ka.w = &ma.w.t[len(ma.w.t)-1].k
   270  			return &ma.ka
   271  		}
   272  	`, w, adjCfg, data)
   273  	doTemplate(`
   274  		func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) AssembleValue() datamodel.NodeAssembler {
   275  			switch ma.state {
   276  			case maState_initial:
   277  				panic("invalid state: AssembleValue cannot be called when no key is primed")
   278  			case maState_midKey:
   279  				if !ma.keyFinishTidy() {
   280  					panic("invalid state: AssembleValue cannot be called when in the middle of assembling a key")
   281  				} // if tidy success: carry on
   282  			case maState_expectValue:
   283  				// carry on
   284  			case maState_midValue:
   285  				panic("invalid state: AssembleValue cannot be called when in the middle of assembling another value")
   286  			case maState_finished:
   287  				panic("invalid state: AssembleValue cannot be called on an assembler that's already finished")
   288  			}
   289  			ma.state = maState_midValue
   290  			return &ma.va
   291  		}
   292  	`, w, adjCfg, data)
   293  	doTemplate(`
   294  		func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) Finish() error {
   295  			switch ma.state {
   296  			case maState_initial:
   297  				// carry on
   298  			case maState_midKey:
   299  				panic("invalid state: Finish cannot be called when in the middle of assembling a key")
   300  			case maState_expectValue:
   301  				panic("invalid state: Finish cannot be called when expecting start of value assembly")
   302  			case maState_midValue:
   303  				if !ma.valueFinishTidy() {
   304  					panic("invalid state: Finish cannot be called when in the middle of assembling a value")
   305  				} // if tidy success: carry on
   306  			case maState_finished:
   307  				panic("invalid state: Finish cannot be called on an assembler that's already finished")
   308  			}
   309  			ma.state = maState_finished
   310  			*ma.m = schema.Maybe_Value
   311  			return nil
   312  		}
   313  	`, w, adjCfg, data)
   314  	doTemplate(`
   315  		func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) KeyPrototype() datamodel.NodePrototype {
   316  			return _{{ .Type.KeyType | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Prototype{}
   317  		}
   318  		func (ma *_{{ .Type | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Assembler) ValuePrototype(_ string) datamodel.NodePrototype {
   319  			return _{{ .Type.ValueType | TypeSymbol }}__{{ if .IsRepr }}Repr{{end}}Prototype{}
   320  		}
   321  	`, w, adjCfg, data)
   322  }